Skip to content

Commit d7eac7f

Browse files
glennslcknitt
andauthored
Add panic function (#72)
* feat(error): add panic/Error.panic refactor(error): less magic needed Co-authored-by: Christoph Knittel <[email protected]> docs(error): panic fixes Co-authored-by: Christoph Knittel <[email protected]> docs(error): ReScript, not OCaml * test(error): test panic * docs(changelog): update changelog for panic/Error.panic * docs(changelog): adjust panic entry after splitting PR Co-authored-by: Christoph Knittel <[email protected]> --------- Co-authored-by: Christoph Knittel <[email protected]>
1 parent 23ace1f commit d7eac7f

10 files changed

+83
-0
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
- Add `make`, `fromInitializer`, `findMap`, `keepSome`, `reduceRight` and `reduceRightWithIndex`. https://github.com/rescript-association/rescript-core/pull/49
2323
- Remove `reduceReverse` in favor of `reduceRight`. https://github.com/rescript-association/rescript-core/pull/49
2424
- Fixed type signatures of `reduce` and `reduceWithIndex`. https://github.com/rescript-association/rescript-core/pull/49
25+
- Add `panic`/`Error.panic`. https://github.com/rescript-association/rescript-core/pull/72
2526

2627
### Documentation
2728

Diff for: src/Core__Error.mjs

+5
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,17 @@ var $$TypeError = {};
1313

1414
var $$URIError = {};
1515

16+
function panic(msg) {
17+
throw new Error("Panic! " + msg);
18+
}
19+
1620
export {
1721
$$EvalError ,
1822
$$RangeError ,
1923
$$ReferenceError ,
2024
$$SyntaxError ,
2125
$$TypeError ,
2226
$$URIError ,
27+
panic ,
2328
}
2429
/* No side effect */

Diff for: src/Core__Error.res

+2
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,5 @@ module URIError = {
3535
}
3636

3737
external raise: t => 'a = "%raise"
38+
39+
let panic = msg => make(j`Panic! $msg`)->raise

Diff for: src/Core__Error.resi

+14
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,17 @@ if 5 > 10 {
155155
```
156156
*/
157157
external raise: t => 'a = "%raise"
158+
159+
/**
160+
Raises a panic exception with the given message.
161+
162+
A panic exception is a native JavaScript exception that is not intended to be caught and
163+
handled. Compared to a ReScript exception this will give a better stack trace and
164+
debugging experience.
165+
166+
## Examples
167+
```rescript
168+
Error.panic("Uh oh. This was unexpected!")
169+
```
170+
*/
171+
let panic: string => 'a

Diff for: src/RescriptCore.mjs

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Generated by ReScript, PLEASE EDIT WITH CARE
22

3+
import * as Core__Error from "./Core__Error.mjs";
34

45
var $$Array;
56

@@ -93,6 +94,8 @@ var List;
9394

9495
var Result;
9596

97+
var panic = Core__Error.panic;
98+
9699
export {
97100
$$Array ,
98101
Console ,
@@ -140,5 +143,6 @@ export {
140143
$$Option ,
141144
List ,
142145
Result ,
146+
panic ,
143147
}
144148
/* No side effect */

Diff for: src/RescriptCore.res

+2
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,5 @@ type null<+'a> = Js.null<'a>
6565
type undefined<+'a> = Js.undefined<'a>
6666

6767
type nullable<+'a> = Js.nullable<'a>
68+
69+
let panic = Core__Error.panic

Diff for: test/ErrorTests.mjs

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Generated by ReScript, PLEASE EDIT WITH CARE
2+
3+
import * as Test from "./Test.mjs";
4+
import * as Js_exn from "rescript/lib/es6/js_exn.js";
5+
import * as RescriptCore from "../src/RescriptCore.mjs";
6+
import * as Caml_js_exceptions from "rescript/lib/es6/caml_js_exceptions.js";
7+
8+
function panicTest(param) {
9+
var caught;
10+
try {
11+
caught = RescriptCore.panic("uh oh");
12+
}
13+
catch (raw_err){
14+
var err = Caml_js_exceptions.internalToOCamlException(raw_err);
15+
if (err.RE_EXN_ID === Js_exn.$$Error) {
16+
caught = err._1.message;
17+
} else {
18+
throw err;
19+
}
20+
}
21+
Test.run([
22+
[
23+
"ErrorTests.res",
24+
8,
25+
22,
26+
43
27+
],
28+
"Should resolve test"
29+
], caught, (function (prim0, prim1) {
30+
return prim0 === prim1;
31+
}), "Panic! uh oh");
32+
}
33+
34+
panicTest(undefined);
35+
36+
export {
37+
panicTest ,
38+
}
39+
/* Not a pure module */

Diff for: test/ErrorTests.res

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
open RescriptCore
2+
3+
let panicTest = () => {
4+
let caught = try panic("uh oh") catch {
5+
| Exn.Error(err) => Error.message(err)
6+
}
7+
8+
Test.run(__POS_OF__("Should resolve test"), caught, \"==", Some("Panic! uh oh"))
9+
}
10+
11+
panicTest()

Diff for: test/TestSuite.mjs

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Generated by ReScript, PLEASE EDIT WITH CARE
22

3+
import * as ErrorTests from "./ErrorTests.mjs";
34
import * as ArrayTests from "./ArrayTests.mjs";
45
import * as PromiseTest from "./PromiseTest.mjs";
56

@@ -19,6 +20,8 @@ var Catching = PromiseTest.Catching;
1920

2021
var Concurrently = PromiseTest.Concurrently;
2122

23+
var panicTest = ErrorTests.panicTest;
24+
2225
var eq = ArrayTests.eq;
2326

2427
export {
@@ -30,6 +33,7 @@ export {
3033
Rejection ,
3134
Catching ,
3235
Concurrently ,
36+
panicTest ,
3337
eq ,
3438
}
3539
/* ArrayTests Not a pure module */

Diff for: test/TestSuite.res

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
include PromiseTest
2+
include ErrorTests
23
include ArrayTests

0 commit comments

Comments
 (0)