Skip to content

Commit

Permalink
ensure "cause" is rendered in Data.Error output (#3188)
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart committed Jul 9, 2024
1 parent bb239da commit 5dc8406
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/spotty-beds-arrive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

ensure "cause" is rendered in Data.Error output
5 changes: 5 additions & 0 deletions .changeset/swift-dodos-double.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": minor
---

support ErrorOptions in YieldableError constructor
2 changes: 1 addition & 1 deletion packages/effect/src/Data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ export const Error: new<A extends Record<string, any> = {}>(
) => Cause.YieldableError & Readonly<A> = (function() {
return class Base extends core.YieldableError {
constructor(args: any) {
super()
super(args?.message, { cause: args?.cause })
if (args) {
Object.assign(this, args)
}
Expand Down
14 changes: 5 additions & 9 deletions packages/effect/src/internal/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2174,23 +2174,19 @@ export const causeSquashWith = dual<
// -----------------------------------------------------------------------------

/** @internal */
export const YieldableError: new(message?: string) => Cause.YieldableError = (function() {
export const YieldableError: new(message?: string, options?: ErrorOptions) => Cause.YieldableError = (function() {
class YieldableError extends globalThis.Error {
commit() {
return fail(this)
}
toString() {
return this.message ? `${this.name}: ${this.message}` : this.name
}
toJSON() {
return { ...this }
}
[NodeInspectSymbol](): string {
const stack = this.stack
if (stack) {
return `${this.toString()}\n${stack.split("\n").slice(1).join("\n")}`
[NodeInspectSymbol]() {
if (this.toString !== globalThis.Error.prototype.toString) {
return this.stack ? `${this.toString()}\n${this.stack.split("\n").slice(1).join("\n")}` : this.toString()
}
return this.toString()
return this
}
}
Object.assign(YieldableError.prototype, StructuralCommitPrototype)
Expand Down
7 changes: 7 additions & 0 deletions packages/effect/test/Effect/error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ describe("Effect", () => {
expect(inspect(new MessageError()).startsWith("fail\n")).toBe(true)
assert.deepStrictEqual(new MessageError().toJSON(), { _tag: "MessageError" })
})

it.it("cause", () => {
class MessageError extends Data.TaggedError("MessageError")<{
cause: unknown
}> {}
expect(inspect(new MessageError({ cause: new Error("boom") }))).includes("[cause]: Error: boom")
})
}

it.it("toJSON", () => {
Expand Down

0 comments on commit 5dc8406

Please sign in to comment.