Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve: Enhance pretty cause #3853

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/sharp-masks-jam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

improve: Pretty cause printing
2 changes: 2 additions & 0 deletions packages/effect/src/Inspectable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ export const stringifyCircular = (obj: unknown, whitespace?: number | string | u
: cache.push(value) && (redactableState.fiberRefs !== undefined && isRedactable(value)
? value[symbolRedactable](redactableState.fiberRefs)
: value)
: typeof value === "bigint"
? value.toString()
: value,
whitespace
)
Expand Down
10 changes: 8 additions & 2 deletions packages/effect/src/internal/cause.ts
Original file line number Diff line number Diff line change
Expand Up @@ -977,10 +977,16 @@ export const pretty = <E>(cause: Cause.Cause<E>, options?: {
return "All fibers interrupted without errors."
}
return prettyErrors<E>(cause).map(function(e) {
if (options?.renderErrorCause !== true || e.cause === undefined) {
if (options?.renderErrorCause !== true) {
return e.stack
}
return `${e.stack} {\n${renderErrorCause(e.cause as PrettyError, " ")}\n}`
const { cause, message: _, name: __, stack, ...rest } = e
const json = stringifyCircular(toJSON(rest), 2)
return !cause && !!Object.keys(rest).length ?
stack :
`${stack} {${json.substring(1, json.length - 1).split("\n").join("\n").trimEnd()}${
cause ? ",\n" + renderErrorCause(cause as PrettyError, " ") : ""
Copy link
Contributor

@tim-smart tim-smart Nov 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You will need to move this logic to renderErrorCause to support nested errors.

}\n}`
}).join("\n")
}

Expand Down
16 changes: 15 additions & 1 deletion packages/effect/test/Cause.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as Predicate from "effect/Predicate"
import { causes, equalCauses, errorCauseFunctions, errors } from "effect/test/utils/cause"
import * as fc from "fast-check"
import { assert, describe, expect, it } from "vitest"
import { Data } from "../src/index.js"

describe("Cause", () => {
it("[internal] prettyErrorMessage", () => {
Expand Down Expand Up @@ -81,6 +82,19 @@ describe("Cause", () => {
}
}
expect(Cause.pretty(Cause.fail(new Error5()))).toEqual(`Error: my string`)

class Error6 extends Data.TaggedError("Error6")<{ message: string; code: number; cause: Error }> {
}
expect(
Cause.pretty(Cause.fail(new Error6({ message: "test", code: 10, cause: new Error("cause") })), {
renderErrorCause: true
})
).includes(
`{
"code": 10,
"_tag": "Error6",
[cause]: Error: cause`
)
})

it("Interrupt", () => {
Expand Down Expand Up @@ -350,7 +364,7 @@ describe("Cause", () => {
if (typeof window === "undefined") {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { inspect } = require("node:util")
expect(inspect(ex)).include("Cause.test.ts:348")
expect(inspect(ex)).include("Cause.test.ts:362")
}
})
})
Expand Down