Skip to content

Commit a48ceda

Browse files
authored
Add optional message to Option.getExn (#212)
* add optional message to Option.getExn * changelog * commit regenerated JS code * change error text * note in changelog as breaking * commit genrated files
1 parent 12df7e0 commit a48ceda

File tree

6 files changed

+22
-15
lines changed

6 files changed

+22
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- BREAKING: Use new native `bigint` type. This requires ReScript compiler version "11.1.0-rc.6" or higher. https://github.com/rescript-association/rescript-core/pull/207
66
- `Int`, `Float`, `BigInt`: use optional args and deprecate `xxxWithRadix`, `xxxWithPrecision` etc. https://github.com/rescript-association/rescript-core/pull/209
7+
- BREAKING: Add optional `~message: string=?` to `Option.getExn`. This also changes the error raised by `Option.getExn` from `Not_found` to a regular JS error. https://github.com/rescript-association/rescript-core/pull/212
78

89
## 1.2.0
910

src/Core__Option.mjs

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

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

56
function filter(opt, p) {
67
if (opt !== undefined && p(Caml_option.valFromOption(opt))) {
@@ -16,14 +17,12 @@ function forEach(opt, f) {
1617

1718
}
1819

19-
function getExn(x) {
20+
function getExn(x, message) {
2021
if (x !== undefined) {
2122
return Caml_option.valFromOption(x);
23+
} else {
24+
return Core__Error.panic(message !== undefined ? message : "Option.getExn called for None value");
2225
}
23-
throw {
24-
RE_EXN_ID: "Not_found",
25-
Error: new Error()
26-
};
2726
}
2827

2928
function mapOr(opt, $$default, f) {

src/Core__Option.res

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,16 @@ let forEach = (opt, f) =>
3434
| None => ()
3535
}
3636

37-
let getExn = x =>
37+
let getExn = (x, ~message=?) =>
3838
switch x {
3939
| Some(x) => x
40-
| None => raise(Not_found)
40+
| None =>
41+
Core__Error.panic(
42+
switch message {
43+
| None => "Option.getExn called for None value"
44+
| Some(message) => message
45+
},
46+
)
4147
}
4248

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

src/Core__Option.resi

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,19 @@ Option.forEach(None, x => Console.log(x)) // returns ()
6666
let forEach: (option<'a>, 'a => unit) => unit
6767

6868
/**
69-
`getExn(opt)` returns `value` if `opt` is `Some(value)`, otherwise raises an exception.
69+
`getExn(opt, ~message=?)` returns `value` if `opt` is `Some(value)`, otherwise raises an exception with the message provided, or a generic message if no message was provided.
7070

7171
```rescript
7272
Option.getExn(Some(3)) // 3
7373
Option.getExn(None) /* Raises an Error */
74+
Option.getExn(None, ~message="was None!") /* Raises an Error with the message "was None!" */
7475
```
7576

7677
## Exceptions
7778

7879
- Raises an error if `opt` is `None`
7980
*/
80-
let getExn: option<'a> => 'a
81+
let getExn: (option<'a>, ~message: string=?) => 'a
8182

8283
/**
8384
`getUnsafe(opt)` returns `value` if `opt` is `Some(value)`, otherwise `undefined`.

test/Test.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function print(value) {
1313
if (match === "object" || match === "bigint") {
1414
return Util.inspect(value);
1515
} else if (match === "string") {
16-
return Core__Option.getExn(JSON.stringify(value));
16+
return Core__Option.getExn(JSON.stringify(value), undefined);
1717
} else {
1818
return String(value);
1919
}

test/TypedArrayTests.mjs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,31 +56,31 @@ assertTrue("fromArray", (function () {
5656
return areSame(Core__Option.getExn(new BigInt64Array([
5757
num1,
5858
num2
59-
])[1]), num2);
59+
])[1], undefined), num2);
6060
}));
6161

6262
assertTrue("fromBuffer", (function () {
6363
var x = new BigInt64Array(new ArrayBuffer(16));
6464
x[1] = num2;
65-
return areSame(Core__Option.getExn(x[1]), num2);
65+
return areSame(Core__Option.getExn(x[1], undefined), num2);
6666
}));
6767

6868
assertWillThrow("fromBuffer when too short can throw when used", (function () {
6969
var x = new BigInt64Array(new ArrayBuffer(1));
7070
x[0] = num1;
71-
areSame(Core__Option.getExn(x[0]), num1);
71+
areSame(Core__Option.getExn(x[0], undefined), num1);
7272
}));
7373

7474
assertTrue("fromBufferWithRange", (function () {
7575
var x = new BigInt64Array(new ArrayBuffer(16), 0, 1);
7676
x[0] = num1;
77-
return areSame(Core__Option.getExn(x[0]), num1);
77+
return areSame(Core__Option.getExn(x[0], undefined), num1);
7878
}));
7979

8080
assertWillThrow("fromBufferWithRange is unsafe, out of range", (function () {
8181
var x = new BigInt64Array(new ArrayBuffer(16), 13, 1);
8282
x[0] = num1;
83-
areSame(Core__Option.getExn(x[0]), num1);
83+
areSame(Core__Option.getExn(x[0], undefined), num1);
8484
}));
8585

8686
assertTrue("fromLength is NOT in bytes", (function () {

0 commit comments

Comments
 (0)