Skip to content

Commit

Permalink
allow message property on Data.TaggedError (#1507)
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart committed Oct 10, 2023
1 parent a4fbb70 commit 2397b55
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/eighty-islands-sell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

allow message property on Data YieldableError
5 changes: 5 additions & 0 deletions .changeset/wild-otters-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

add name getter to YieldableError
34 changes: 18 additions & 16 deletions src/Data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import type * as Channel from "./Channel"
import * as Effect from "./Effect"
import * as Effectable from "./Effectable"
import type * as Equal from "./Equal"
import type * as Inspectable from "./Inspectable"
import * as internal from "./internal/Data"
import { type Pipeable } from "./Pipeable"
import type * as Sink from "./Sink"
Expand Down Expand Up @@ -303,7 +302,7 @@ export const taggedEnum: {
* @since 2.0.0
* @category models
*/
export interface YieldableError extends Case, Pipeable, Inspectable.Inspectable {
export interface YieldableError extends Case, Pipeable, Error {
readonly [Effectable.EffectTypeId]: Effect.Effect.VarianceStruct<never, this, never>
readonly [Effectable.StreamTypeId]: Effect.Effect.VarianceStruct<never, this, never>
readonly [Effectable.SinkTypeId]: Sink.Sink.VarianceStruct<never, this, unknown, never, never>
Expand All @@ -318,21 +317,23 @@ export interface YieldableError extends Case, Pipeable, Inspectable.Inspectable
>
}

const YieldableErrorProto = Object.setPrototypeOf(
{
...Effectable.StructuralCommitPrototype,
toString() {
return `${this.name}: ${this.message}`
},
get message() {
return JSON.stringify(this)
},
commit() {
return Effect.fail(this)
}
const YieldableErrorMessage = Symbol.for("effect/Data/YieldableError/message")
const YieldableErrorProto = {
...Effectable.StructuralCommitPrototype,
__proto__: globalThis.Error.prototype,
commit() {
return Effect.fail(this)
},
toString(this: globalThis.Error) {
return `${this.name}: ${this.message}`
},
get message() {
return (this as any)[YieldableErrorMessage] ?? JSON.stringify(this)
},
globalThis.Error.prototype
)
set message(value: string) {
;(this as any)[YieldableErrorMessage] = value
}
}

/**
* Provides a constructor for a Case Class.
Expand Down Expand Up @@ -363,5 +364,6 @@ export const TaggedError = <Tag extends string>(tag: Tag): new<A extends Record<
class Base extends Error<{}> {
readonly _tag = tag
}
Base.prototype.name = tag
return Base as any
}
24 changes: 23 additions & 1 deletion test/Effect/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe.concurrent("Effect", () => {
Effect.gen(function*($) {
const cause = yield* $(Effect.flip(Effect.sandbox(Effect.withSpan("A")(new TestError()))))
const log = Cause.pretty(cause)
expect(log).includes("TestError: ")
expect(log).includes("test/Effect/error.ts:12:78")
expect(log).includes("at A")
}))
Expand All @@ -27,7 +28,28 @@ describe.concurrent("Effect", () => {
Effect.flip
)
const log = Cause.pretty(cause)
expect(log).includes("test/Effect/error.ts:23")
expect(log).includes("test/Effect/error.ts:24")
expect(log).includes("at A")
}))

it.effect("allow message prop", () =>
Effect.gen(function*($) {
class MessageError extends Data.TaggedError("MessageError")<{
readonly name: string
readonly message: string
}> {}
const cause = yield* $(
Effect.tryPromise({
try: () => Promise.reject("fail"),
catch: () => new MessageError({ name: "Failure", message: "some message" })
}),
Effect.withSpan("A"),
Effect.sandbox,
Effect.flip
)
const log = Cause.pretty(cause)
expect(log).includes("Failure: some message")
expect(log).includes("test/Effect/error.ts:44")
expect(log).includes("at A")
}))
})

0 comments on commit 2397b55

Please sign in to comment.