Skip to content

Commit 8c06a60

Browse files
committed
feat(error): add panic/Error.panic
refactor(error): less magic needed Co-authored-by: Christoph Knittel <christoph@knittel.cc> docs(error): panic fixes Co-authored-by: Christoph Knittel <christoph@knittel.cc> docs(error): ReScript, not OCaml
1 parent 847e916 commit 8c06a60

File tree

5 files changed

+27
-0
lines changed

5 files changed

+27
-0
lines changed

src/Core__Error.mjs

Lines changed: 5 additions & 0 deletions
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 */

src/Core__Error.res

Lines changed: 2 additions & 0 deletions
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

src/Core__Error.resi

Lines changed: 14 additions & 0 deletions
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

src/RescriptCore.mjs

Lines changed: 4 additions & 0 deletions
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 */

src/RescriptCore.res

Lines changed: 2 additions & 0 deletions
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

0 commit comments

Comments
 (0)