You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
Copy file name to clipboardExpand all lines: pages/docs/manual/latest/exception.mdx
+45-55
Original file line number
Diff line number
Diff line change
@@ -12,9 +12,9 @@ Exceptions are just a special kind of variant, thrown in **exceptional** cases (
12
12
13
13
<CodeTablabels={["ReScript", "JS Output"]}>
14
14
15
-
```res
16
-
let getItem = (items) =>
17
-
if callSomeFunctionThatThrows() {
15
+
```res prelude
16
+
let getItem = (item: int) =>
17
+
if (item === 3) {
18
18
// return the found item here
19
19
1
20
20
} else {
@@ -23,14 +23,16 @@ let getItem = (items) =>
23
23
24
24
let result =
25
25
try {
26
-
getItem([1, 2, 3])
26
+
getItem(2)
27
27
} catch {
28
28
| Not_found => 0 // Default value if getItem throws
29
29
}
30
30
```
31
31
```js
32
-
functiongetItem(items) {
33
-
if (callSomeFunctionThatThrows()) {
32
+
var Caml_js_exceptions =require("./stdlib/caml_js_exceptions.js");
33
+
34
+
functiongetItem(item) {
35
+
if (item ===3) {
34
36
return1;
35
37
}
36
38
throw {
@@ -42,8 +44,9 @@ function getItem(items) {
42
44
var result;
43
45
44
46
try {
45
-
result =getItem([1, 2, 3]);
46
-
} catch (raw_exn) {
47
+
result =getItem(2);
48
+
}
49
+
catch (raw_exn){
47
50
var exn =Caml_js_exceptions.internalToOCamlException(raw_exn);
48
51
if (exn.RE_EXN_ID==="Not_found") {
49
52
result =0;
@@ -61,21 +64,33 @@ You can directly match on exceptions _while_ getting another return value from a
61
64
62
65
<CodeTablabels={["ReScript", "JS Output"]}>
63
66
64
-
```res
65
-
switch List.find(i => i === theItem, myItems) {
67
+
```res prelude
68
+
switch List.find(i => i === 4, list{1, 2, 3}) {
66
69
| item => Js.log(item)
67
70
| exception Not_found => Js.log("No such item found!")
68
71
}
69
72
```
70
73
```js
74
+
var List =require("./stdlib/list.js");
75
+
var Caml_js_exceptions =require("./stdlib/caml_js_exceptions.js");
76
+
71
77
var exit =0;
72
78
73
79
var item;
74
80
75
81
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
+
});
79
94
exit =1;
80
95
}
81
96
catch (raw_exn){
@@ -121,10 +136,26 @@ throw {
121
136
122
137
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:
123
138
124
-
<CodeTablabels={["ReScript", "JS Output"]}>
139
+
140
+
Throw an exception from JS:
141
+
142
+
```js
143
+
// Example.js
144
+
145
+
exports.someJsFunctionThatThrows= () => {
146
+
thrownewError("A Glitch in the Matrix!");
147
+
}
148
+
```
149
+
150
+
Then catch it from ReScript:
125
151
126
152
```res
153
+
// import the method in Example.js
154
+
@module("./Example")
155
+
external someJsFunctionThatThrows: () => unit = "someJsFunctionThatThrows"
156
+
127
157
try {
158
+
// call the external method
128
159
someJSFunctionThatThrows()
129
160
} catch {
130
161
| Js.Exn.Error(obj) =>
@@ -134,26 +165,6 @@ try {
134
165
}
135
166
}
136
167
```
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>
157
168
158
169
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.
159
170
@@ -240,8 +251,6 @@ When you have ordinary variants, you often don't **need** exceptions. For exampl
240
251
241
252
### Catch Both ReScript and JS Exceptions in the Same `catch` Clause
242
253
243
-
<CodeTablabels={["ReScript", "JS Output"]}>
244
-
245
254
```res
246
255
try {
247
256
someOtherJSFunctionThatThrows()
@@ -251,24 +260,5 @@ try {
251
260
| Js.Exn.Error(obj) => ... // catch the JS exception
252
261
}
253
262
```
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>
273
263
274
264
This technically works, but hopefully you don't ever have to work with such code...
0 commit comments