Skip to content

Commit 7a2c329

Browse files
authored
Doc: Clear type errors and warnings in the rescript playground where posible (#603)
* - clear type errors and warnings in the rescript playground where possible - do not link to playground for some examples * - add `prelude` in one more place
1 parent cf24956 commit 7a2c329

File tree

1 file changed

+45
-55
lines changed

1 file changed

+45
-55
lines changed

pages/docs/manual/latest/exception.mdx

+45-55
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ Exceptions are just a special kind of variant, thrown in **exceptional** cases (
1212

1313
<CodeTab labels={["ReScript", "JS Output"]}>
1414

15-
```res
16-
let getItem = (items) =>
17-
if callSomeFunctionThatThrows() {
15+
```res prelude
16+
let getItem = (item: int) =>
17+
if (item === 3) {
1818
// return the found item here
1919
1
2020
} else {
@@ -23,14 +23,16 @@ let getItem = (items) =>
2323
2424
let result =
2525
try {
26-
getItem([1, 2, 3])
26+
getItem(2)
2727
} catch {
2828
| Not_found => 0 // Default value if getItem throws
2929
}
3030
```
3131
```js
32-
function getItem(items) {
33-
if (callSomeFunctionThatThrows()) {
32+
var Caml_js_exceptions = require("./stdlib/caml_js_exceptions.js");
33+
34+
function getItem(item) {
35+
if (item === 3) {
3436
return 1;
3537
}
3638
throw {
@@ -42,8 +44,9 @@ function getItem(items) {
4244
var result;
4345

4446
try {
45-
result = getItem([1, 2, 3]);
46-
} catch (raw_exn) {
47+
result = getItem(2);
48+
}
49+
catch (raw_exn){
4750
var exn = Caml_js_exceptions.internalToOCamlException(raw_exn);
4851
if (exn.RE_EXN_ID === "Not_found") {
4952
result = 0;
@@ -61,21 +64,33 @@ You can directly match on exceptions _while_ getting another return value from a
6164

6265
<CodeTab labels={["ReScript", "JS Output"]}>
6366

64-
```res
65-
switch List.find(i => i === theItem, myItems) {
67+
```res prelude
68+
switch List.find(i => i === 4, list{1, 2, 3}) {
6669
| item => Js.log(item)
6770
| exception Not_found => Js.log("No such item found!")
6871
}
6972
```
7073
```js
74+
var List = require("./stdlib/list.js");
75+
var Caml_js_exceptions = require("./stdlib/caml_js_exceptions.js");
76+
7177
var exit = 0;
7278

7379
var item;
7480

7581
try {
76-
item = List.find(function(i) {
77-
return i === theItem;
78-
}, myItems);
82+
item = List.find((function (i) {
83+
return i === 4;
84+
}), {
85+
hd: 1,
86+
tl: {
87+
hd: 2,
88+
tl: {
89+
hd: 3,
90+
tl: /* [] */0
91+
}
92+
}
93+
});
7994
exit = 1;
8095
}
8196
catch (raw_exn){
@@ -121,10 +136,26 @@ throw {
121136

122137
To distinguish between JavaScript exceptions and ReScript exceptions, ReScript namespaces JS exceptions under the `Js.Exn.Error(payload)` variant. To catch an exception thrown from the JS side:
123138

124-
<CodeTab labels={["ReScript", "JS Output"]}>
139+
140+
Throw an exception from JS:
141+
142+
```js
143+
// Example.js
144+
145+
exports.someJsFunctionThatThrows = () => {
146+
throw new Error("A Glitch in the Matrix!");
147+
}
148+
```
149+
150+
Then catch it from ReScript:
125151

126152
```res
153+
// import the method in Example.js
154+
@module("./Example")
155+
external someJsFunctionThatThrows: () => unit = "someJsFunctionThatThrows"
156+
127157
try {
158+
// call the external method
128159
someJSFunctionThatThrows()
129160
} catch {
130161
| Js.Exn.Error(obj) =>
@@ -134,26 +165,6 @@ try {
134165
}
135166
}
136167
```
137-
```js
138-
var Js_exn = require("./stdlib/js_exn.js");
139-
var Caml_js_exceptions = require("./stdlib/caml_js_exceptions.js");
140-
141-
try {
142-
someJSFunctionThatThrows();
143-
} catch (raw_obj) {
144-
var obj = Caml_js_exceptions.internalToOCamlException(raw_obj);
145-
if (obj.RE_EXN_ID === Js_exn.$$Error) {
146-
var m = obj._1.message;
147-
if (m !== undefined) {
148-
console.log("Caught a JS exception! Message: " + m);
149-
}
150-
} else {
151-
throw obj;
152-
}
153-
}
154-
```
155-
156-
</CodeTab>
157168

158169
The `obj` here is of type `Js.Exn.t`, intentionally opaque to disallow illegal operations. To operate on `obj`, do like the code above by using the standard library's [`Js.Exn`](api/js/exn) module's helpers.
159170

@@ -240,8 +251,6 @@ When you have ordinary variants, you often don't **need** exceptions. For exampl
240251

241252
### Catch Both ReScript and JS Exceptions in the Same `catch` Clause
242253

243-
<CodeTab labels={["ReScript", "JS Output"]}>
244-
245254
```res
246255
try {
247256
someOtherJSFunctionThatThrows()
@@ -251,24 +260,5 @@ try {
251260
| Js.Exn.Error(obj) => ... // catch the JS exception
252261
}
253262
```
254-
```js
255-
var Js_exn = require("./stdlib/js_exn.js");
256-
var Caml_js_exceptions = require("./stdlib/caml_js_exceptions.js");
257-
258-
try {
259-
someOtherJSFunctionThatThrows();
260-
} catch (raw_obj) {
261-
var obj = Caml_js_exceptions.internalToOCamlException(raw_obj);
262-
if (
263-
obj.RE_EXN_ID !== "Not_found" &&
264-
obj.RE_EXN_ID !== "Invalid_argument" &&
265-
obj.RE_EXN_ID !== Js_exn.$$Error
266-
) {
267-
throw obj;
268-
}
269-
}
270-
```
271-
272-
</CodeTab>
273263

274264
This technically works, but hopefully you don't ever have to work with such code...

0 commit comments

Comments
 (0)