Skip to content

Commit 8b10bd8

Browse files
committed
feat: use panic where appropriate
1 parent 56886f0 commit 8b10bd8

File tree

9 files changed

+37
-43
lines changed

9 files changed

+37
-43
lines changed

src/Core__List.mjs

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as Curry from "rescript/lib/es6/curry.js";
44
import * as Belt_Array from "rescript/lib/es6/belt_Array.js";
55
import * as Caml_option from "rescript/lib/es6/caml_option.js";
66
import * as Core__Array from "./Core__Array.mjs";
7+
import * as Core__Error from "./Core__Error.mjs";
78

89
function head(x) {
910
if (x) {
@@ -15,11 +16,9 @@ function head(x) {
1516
function headExn(x) {
1617
if (x) {
1718
return x.hd;
19+
} else {
20+
return Core__Error.panic("List.headExn: list is empty");
1821
}
19-
throw {
20-
RE_EXN_ID: "Not_found",
21-
Error: new Error()
22-
};
2322
}
2423

2524
function tail(x) {
@@ -32,11 +31,9 @@ function tail(x) {
3231
function tailExn(x) {
3332
if (x) {
3433
return x.tl;
34+
} else {
35+
return Core__Error.panic("List.tailExn: list is empty");
3536
}
36-
throw {
37-
RE_EXN_ID: "Not_found",
38-
Error: new Error()
39-
};
4037
}
4138

4239
function add(xs, x) {
@@ -70,29 +67,24 @@ function get(x, n) {
7067

7168
function getExn(x, n) {
7269
if (n < 0) {
73-
throw {
74-
RE_EXN_ID: "Not_found",
75-
Error: new Error()
76-
};
77-
}
78-
var _x = x;
79-
var _n = n;
80-
while(true) {
81-
var n$1 = _n;
82-
var x$1 = _x;
83-
if (x$1) {
70+
return Core__Error.panic("List.getExn: n < 0");
71+
} else {
72+
var _x = x;
73+
var _n = n;
74+
while(true) {
75+
var n$1 = _n;
76+
var x$1 = _x;
77+
if (!x$1) {
78+
return Core__Error.panic("List.nthAuxAssert: list is empty");
79+
}
8480
if (n$1 === 0) {
8581
return x$1.hd;
8682
}
8783
_n = n$1 - 1 | 0;
8884
_x = x$1.tl;
8985
continue ;
90-
}
91-
throw {
92-
RE_EXN_ID: "Not_found",
93-
Error: new Error()
94-
};
95-
};
86+
};
87+
}
9688
}
9789

9890
function partitionAux(p, _cell, _precX, _precY) {

src/Core__List.res

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ let head = x =>
9494

9595
let headExn = x =>
9696
switch x {
97-
| list{} => raise(Not_found)
97+
| list{} => Core__Error.panic("List.headExn: list is empty")
9898
| list{x, ..._} => x
9999
}
100100

@@ -106,7 +106,7 @@ let tail = x =>
106106

107107
let tailExn = x =>
108108
switch x {
109-
| list{} => raise(Not_found)
109+
| list{} => Core__Error.panic("List.tailExn: list is empty")
110110
| list{_, ...t} => t
111111
}
112112

@@ -132,7 +132,7 @@ let rec nthAuxAssert = (x, n) =>
132132
} else {
133133
nthAuxAssert(t, n - 1)
134134
}
135-
| _ => raise(Not_found)
135+
| list{} => Core__Error.panic("List.nthAuxAssert: list is empty")
136136
}
137137

138138
let get = (x, n) =>
@@ -144,7 +144,7 @@ let get = (x, n) =>
144144

145145
let getExn = (x, n) =>
146146
if n < 0 {
147-
raise(Not_found)
147+
Core__Error.panic("List.getExn: n < 0")
148148
} else {
149149
nthAuxAssert(x, n)
150150
}

src/Core__List.resi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ List.headExn(list{}) // Raises an Error
8282
8383
## Exceptions
8484
85-
- Raises an Error if list is empty.
85+
- Panics if list is empty.
8686
8787
*/
8888
let headExn: t<'a> => 'a
@@ -114,7 +114,7 @@ List.tailExn(list{}) // Raises an Error
114114
115115
## Exceptions
116116
117-
- Raises an Error if list is empty.
117+
- Panics if list is empty.
118118
*/
119119
let tailExn: t<'a> => t<'a>
120120

@@ -162,7 +162,7 @@ abc->List.getExn(4) // Raises an Error
162162
163163
## Exceptions
164164
165-
- Raises an Error if `index` is larger than the length of list.
165+
- Panics if `index` is less than 0 or larger than the length of list.
166166
*/
167167
let getExn: (t<'a>, int) => 'a
168168

src/Core__Option.mjs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import * as Curry from "rescript/lib/es6/curry.js";
44
import * as Caml_option from "rescript/lib/es6/caml_option.js";
5+
import * as Core__Error from "./Core__Error.mjs";
56

67
function filter(opt, p) {
78
var p$1 = Curry.__1(p);
@@ -22,11 +23,9 @@ function forEach(opt, f) {
2223
function getExn(x) {
2324
if (x !== undefined) {
2425
return Caml_option.valFromOption(x);
26+
} else {
27+
return Core__Error.panic("List.headExn: option is None");
2528
}
26-
throw {
27-
RE_EXN_ID: "Not_found",
28-
Error: new Error()
29-
};
3029
}
3130

3231
function mapWithDefault(opt, $$default, f) {

src/Core__Option.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ let forEach = (opt, f) => forEachU(opt, (. x) => f(x))
4141
let getExn = x =>
4242
switch x {
4343
| Some(x) => x
44-
| None => raise(Not_found)
44+
| None => Core__Error.panic("List.headExn: option is None")
4545
}
4646

4747
external getUnsafe: option<'a> => 'a = "%identity"

src/Core__Option.resi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ Option.getExn(None) /* Raises an Error */
7575
7676
## Exceptions
7777
78-
- Raises an error if `opt` is `None`
78+
- Panics if `opt` is `None`
7979
*/
8080
let getExn: option<'a> => 'a
8181

src/Core__Result.mjs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
// Generated by ReScript, PLEASE EDIT WITH CARE
22

33
import * as Curry from "rescript/lib/es6/curry.js";
4+
import * as Core__Error from "./Core__Error.mjs";
45

56
function getExn(x) {
67
if (x.TAG === /* Ok */0) {
78
return x._0;
9+
} else {
10+
return Core__Error.panic("Result.getExn: result is Error");
811
}
9-
throw {
10-
RE_EXN_ID: "Not_found",
11-
Error: new Error()
12-
};
1312
}
1413

1514
function mapWithDefault(opt, $$default, f) {

src/Core__Result.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ type t<'a, 'b> = result<'a, 'b> = Ok('a) | Error('b)
2727
let getExn = x =>
2828
switch x {
2929
| Ok(x) => x
30-
| Error(_) => raise(Not_found)
30+
| Error(_) => Core__Error.panic("Result.getExn: result is Error")
3131
}
3232

3333
let mapWithDefaultU = (opt, default, f) =>

src/Core__Result.resi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ type t<'a, 'b> = result<'a, 'b> = Ok('a) | Error('b)
5555
5656
Result.getExn(Result.Error("Invalid data")) /* raises exception */
5757
```
58+
59+
## Exceptions
60+
61+
- Panics if `res` is `Error(_)`
5862
*/
5963
let getExn: t<'a, 'b> => 'a
6064

0 commit comments

Comments
 (0)