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

ensure "cause" is rendered in Data.Error output #3188

Merged
merged 1 commit into from
Jul 7, 2024
Merged
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/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() {
Copy link
Contributor

Choose a reason for hiding this comment

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

What effect is this change supposed to have? Is it supposed to change the default arguments to a Data.TaggedError for example?

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