Skip to content

Commit 113b84f

Browse files
nojaftsnobip
andauthored
Update markdown-pages/blog/release-12-0-0.mdx
Co-authored-by: Paul Tsnobiladzé <paul.tsnobiladze@gmail.com>
1 parent 84c5e4b commit 113b84f

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

markdown-pages/blog/release-12-0-0.mdx

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,30 @@ APIs that previously ended with `Exn` now end with `OrThrow`.
4747
Examples include `Option.getOrThrow`, `List.headOrThrow`, `BigInt.fromStringOrThrow`, and `JSON.parseOrThrow`.
4848
The change clarifies that these functions throw JavaScript errors, aligning the naming with the language’s semantics.
4949
The deprecated `*Exn` variants remain available in v12 to ease the transition, and the codemod bundled with the migration tool can perform a mechanical rename.
50-
Be aware that `Result.getOrThrow` now throws a JavaScript `Error`, so update any exception handling logic that depended on OCaml exception names.
50+
Note that `Result.getOrThrow` now throws a JavaScript exception, so please update any exception-handling logic that relied on OCaml exception names. We also revamped the API around JS exceptions and errors.
51+
52+
We’ve renamed JS errors to `JsError.t` to better distinguish them from the `Result.Error` variant. Since JavaScript allows throwing anything (not only proper Error objects), the previous way of catching JS exceptions was unsafe:
53+
```res
54+
let foo = switch myUnsafeJsResult() {
55+
| exception Exn.Error(e) => Error(e->Error.message)
56+
// ☝️ this will crash if `e` is undefined or null,
57+
// or return undefined if `e` has no `message` property
58+
| myRes => Ok(myRes)
59+
}
60+
```
61+
Additionally, the coexistence of `Result.Error` and the `Error` module caused confusion.
62+
63+
The new recommended pattern is:
64+
```res
65+
let foo = switch myUnsafeJsResult() {
66+
| exception JsExn(e) => Error(e->JsExn.message->Option.getOr("[no message]"))
67+
| myRes => Ok(myRes)
68+
}
69+
```
70+
Utility helpers for working with JS errors are now in `JsError`, eg:
71+
```res
72+
JsError.throw(JsError.RangeError.make("the payload should be below 16"))
73+
```
5174

5275
### JSX version requirement
5376

0 commit comments

Comments
 (0)