Skip to content

Commit

Permalink
fix YieldableError.toString crashing on react-native (#1687)
Browse files Browse the repository at this point in the history
Co-authored-by: Tim <hello@timsmart.co>
  • Loading branch information
matheuspuel and tim-smart authored Nov 22, 2023
1 parent 9f4d287 commit e4d90ed
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/hot-crabs-boil.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

fix YieldableError.toString crashing on react-native
6 changes: 5 additions & 1 deletion src/internal/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2199,12 +2199,16 @@ export const YieldableError: new(message?: string) => Cause.YieldableError = (fu
return fail(this)
}
toString() {
return this.stack ?? `${this.name}: ${this.message}`
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")}`
}
return this.toString()
}
}
Expand Down
3 changes: 2 additions & 1 deletion test/Cause.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as internal from "effect/internal/cause"
import * as Option from "effect/Option"
import * as Predicate from "effect/Predicate"
import * as fc from "fast-check"
import { inspect } from "node:util"
import { assert, describe, expect, it } from "vitest"

describe.concurrent("Cause", () => {
Expand Down Expand Up @@ -348,7 +349,7 @@ describe.concurrent("Cause", () => {
const ex = new Cause.InterruptedException("my message")
expect(ex.toString()).include("InterruptedException: my message")
if (typeof window === "undefined") {
expect(ex.toString()).include("Cause.test.ts:348")
expect(inspect(ex)).include("Cause.test.ts:349")
}
})
})
Expand Down
14 changes: 7 additions & 7 deletions test/Effect/error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe.concurrent("Effect", () => {
const log = Cause.pretty(cause)
expect(log).includes("TestError")
if (typeof window === "undefined") {
expect(log).includes("test/Effect/error.test.ts:13:78")
expect(log.replaceAll("\\", "/")).includes("test/Effect/error.test.ts:13:78")
}
expect(log).includes("at A")
}))
Expand All @@ -32,7 +32,7 @@ describe.concurrent("Effect", () => {
)
const log = Cause.pretty(cause)
if (typeof window === "undefined") {
expect(log).includes("test/Effect/error.test.ts:27")
expect(log.replaceAll("\\", "/")).includes("test/Effect/error.test.ts:27")
}
expect(log).includes("at A")
}))
Expand All @@ -55,30 +55,30 @@ describe.concurrent("Effect", () => {
const log = Cause.pretty(cause)
expect(log).includes("Failure: some message")
if (typeof window === "undefined") {
expect(log).includes("test/Effect/error.test.ts:49")
expect(log.replaceAll("\\", "/")).includes("test/Effect/error.test.ts:49")
}
expect(log).includes("at A")
}))

if (typeof window === "undefined") {
it.it("inspect", () => {
class MessageError extends Data.TaggedError("MessageError")<{}> {
class MessageError extends Data.TaggedError("MessageError") {
get message() {
return "fail"
}
}
const err = new MessageError()
expect(inspect(err)).include("MessageError: fail")
expect(inspect(err)).include("test/Effect/error.test.ts:70")
expect(inspect(err).replaceAll("\\", "/")).include("test/Effect/error.test.ts:70")
})

it.it("toString", () => {
class MessageError extends Data.TaggedError("MessageError")<{}> {
class MessageError extends Data.TaggedError("MessageError") {
toString() {
return "fail"
}
}
expect(inspect(new MessageError())).equal("fail")
expect(inspect(new MessageError()).startsWith("fail\n")).toBe(true)
assert.deepStrictEqual(new MessageError().toJSON(), { _tag: "MessageError" })
})
}
Expand Down

0 comments on commit e4d90ed

Please sign in to comment.