Skip to content

Allow verbose printing of cause #5132

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

Open
wants to merge 2 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/busy-animals-open.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

Allow verbose printing of cause
48 changes: 27 additions & 21 deletions packages/effect/src/internal/cause.ts
Original file line number Diff line number Diff line change
Expand Up @@ -871,11 +871,12 @@ export const reduceWithContext = dual<
/** @internal */
export const pretty = <E>(cause: Cause.Cause<E>, options?: {
readonly renderErrorCause?: boolean | undefined
readonly verbose?: boolean
}): string => {
if (isInterruptedOnly(cause)) {
return "All fibers interrupted without errors."
}
return prettyErrors<E>(cause).map(function(e) {
return prettyErrors<E>(cause, options?.verbose).map(function(e) {
if (options?.renderErrorCause !== true || e.cause === undefined) {
return e.stack
}
Expand All @@ -898,14 +899,14 @@ const renderErrorCause = (cause: PrettyError, prefix: string) => {
/** @internal */
export class PrettyError extends globalThis.Error implements Cause.PrettyError {
span: undefined | Span = undefined
constructor(originalError: unknown) {
constructor(originalError: unknown, readonly verbose: boolean) {
const originalErrorIsObject = typeof originalError === "object" && originalError !== null
const prevLimit = Error.stackTraceLimit
Error.stackTraceLimit = 1
super(
prettyErrorMessage(originalError),
originalErrorIsObject && "cause" in originalError && typeof originalError.cause !== "undefined"
? { cause: new PrettyError(originalError.cause) }
? { cause: new PrettyError(originalError.cause, verbose) }
: undefined
)
if (this.message === "") {
Expand All @@ -929,6 +930,7 @@ export class PrettyError extends globalThis.Error implements Cause.PrettyError {
originalError instanceof Error && originalError.stack
? originalError.stack
: "",
this.verbose,
this.span
)
}
Expand Down Expand Up @@ -978,26 +980,30 @@ const locationRegex = /\((.*)\)/g
/** @internal */
export const spanToTrace = globalValue("effect/Tracer/spanToTrace", () => new WeakMap())

const prettyErrorStack = (message: string, stack: string, span?: Span | undefined): string => {
const prettyErrorStack = (message: string, stack: string, verbose: boolean, span?: Span | undefined): string => {
const out: Array<string> = [message]
const lines = stack.startsWith(message) ? stack.slice(message.length).split("\n") : stack.split("\n")

for (let i = 1; i < lines.length; i++) {
if (lines[i].includes(" at new BaseEffectError") || lines[i].includes(" at new YieldableError")) {
i++
continue
}
if (lines[i].includes("Generator.next")) {
break
}
if (lines[i].includes("effect_internal_function")) {
break
if (verbose === true) {
out.push(lines[i])
} else {
if (lines[i].includes(" at new BaseEffectError") || lines[i].includes(" at new YieldableError")) {
i++
continue
}
if (lines[i].includes("Generator.next")) {
break
}
if (lines[i].includes("effect_internal_function")) {
break
}
out.push(
lines[i]
.replace(/at .*effect_instruction_i.*\((.*)\)/, "at $1")
.replace(/EffectPrimitive\.\w+/, "<anonymous>")
)
}
out.push(
lines[i]
.replace(/at .*effect_instruction_i.*\((.*)\)/, "at $1")
.replace(/EffectPrimitive\.\w+/, "<anonymous>")
)
}

if (span) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe this needs to be updated as well, to insert spans at a specific position in the stack when verbose=true

Expand Down Expand Up @@ -1035,14 +1041,14 @@ const prettyErrorStack = (message: string, stack: string, span?: Span | undefine
export const spanSymbol = Symbol.for("effect/SpanAnnotation")

/** @internal */
export const prettyErrors = <E>(cause: Cause.Cause<E>): Array<PrettyError> =>
export const prettyErrors = <E>(cause: Cause.Cause<E>, verbose = true): Array<PrettyError> =>
Copy link
Contributor

Choose a reason for hiding this comment

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

This function is exported as Cause.prettyErrors, so this change does change the default behaviour of it

reduceWithContext(cause, void 0, {
emptyCase: (): Array<PrettyError> => [],
dieCase: (_, unknownError) => {
return [new PrettyError(unknownError)]
return [new PrettyError(unknownError, verbose)]
},
failCase: (_, error) => {
return [new PrettyError(error)]
return [new PrettyError(error, verbose)]
},
interruptCase: () => [],
parallelCase: (_, l, r) => [...l, ...r],
Expand Down
12 changes: 6 additions & 6 deletions packages/effect/test/Cause.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -926,16 +926,16 @@ describe("Cause", () => {
describe("Formatting", () => {
it("prettyErrors", () => {
deepStrictEqual(Cause.prettyErrors(empty), [])
deepStrictEqual(Cause.prettyErrors(failure), [new internal.PrettyError("error")])
deepStrictEqual(Cause.prettyErrors(defect), [new internal.PrettyError("defect")])
deepStrictEqual(Cause.prettyErrors(failure), [new internal.PrettyError("error", true)])
deepStrictEqual(Cause.prettyErrors(defect), [new internal.PrettyError("defect", true)])
deepStrictEqual(Cause.prettyErrors(interruption), [])
deepStrictEqual(Cause.prettyErrors(sequential), [
new internal.PrettyError("error"),
new internal.PrettyError("defect")
new internal.PrettyError("error", true),
new internal.PrettyError("defect", true)
])
deepStrictEqual(Cause.prettyErrors(parallel), [
new internal.PrettyError("error"),
new internal.PrettyError("defect")
new internal.PrettyError("error", true),
new internal.PrettyError("defect", true)
])
})

Expand Down