Skip to content

Commit

Permalink
add cause in errors thrown by asserts, closes #2729 (#2738)
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti authored May 13, 2024
1 parent 0d1f513 commit 89a3afb
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/fifty-clocks-fail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/schema": patch
---

add `cause` in errors thrown by `asserts`, closes #2729
2 changes: 1 addition & 1 deletion packages/schema/src/ParseResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@ export const asserts = <A, I, R>(schema: Schema.Schema<A, I, R>, options?: AST.P
isExact: true
}) as any
if (Either.isLeft(result)) {
throw new Error(TreeFormatter.formatIssueSync(result.left))
throw new Error(TreeFormatter.formatIssueSync(result.left), { cause: result.left })
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions packages/schema/test/Schema/asserts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ const expectAssertsFailure = <A, I>(schema: S.Schema<A, I>, input: unknown, mess
}

describe("asserts", () => {
it("the returned error should include a cause", () => {
const asserts: (u: unknown) => asserts u is string = S.asserts(S.String)
try {
asserts(1)
} catch (e: any) {
expect(e.cause).exist
}
})

it("should respect outer/inner options", () => {
const schema = S.Struct({ a: Util.NumberFromChar })
const input = { a: 1, b: "b" }
Expand Down
8 changes: 8 additions & 0 deletions packages/schema/test/Schema/decodeUnknownSync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ import * as Util from "@effect/schema/test/TestUtils"
import { describe, expect, it } from "vitest"

describe("decodeUnknownSync", () => {
it("the returned error should include a cause", () => {
try {
S.decodeUnknownSync(S.String)(1)
} catch (e: any) {
expect(e.cause).exist
}
})

it("should throw on async", () => {
expect(() => S.decodeUnknownSync(Util.AsyncString)("a")).toThrow(
new Error(
Expand Down

0 comments on commit 89a3afb

Please sign in to comment.