Skip to content

Commit b3d991b

Browse files
committed
v2.0.0
1 parent 406643c commit b3d991b

7 files changed

+56
-11
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## v2.0.0 - 2024-02-17
4+
5+
- The `rescue` function now indicated whether the exception was an error, a
6+
throw, or an exit.
7+
38
## v1.1.1 - 2024-01-16
49

510
- Relaxed the version constraint for `gleam_stdlib` to permit 1.x or 0.x

gleam.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name = "exception"
2-
version = "1.1.1"
2+
version = "2.0.0"
33
gleam = ">= 0.32.0"
44
description = "A tiny package for dealing with exceptions"
55
licences = ["Apache-2.0"]

src/exception.gleam

+17-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
11
import gleam/dynamic.{type Dynamic}
22

3-
// TODO: implement
4-
// TODO: test
5-
// TODO: document
3+
pub type Exception {
4+
/// An error was raised.
5+
/// On Erlang this would be caused by calling the `erlang:error/1` function,
6+
/// or some other runtime error.
7+
/// On JavaScript this would be caused by throwing an `Error` object.
8+
Errored(Dynamic)
9+
/// A value was thrown.
10+
/// On Erlang this would be caused by calling the `erlang:throw/1` function.
11+
/// On JavaScript this would be caused by throwing any non-`Error` value.
12+
Thrown(Dynamic)
13+
/// A process exited.
14+
/// On Erlang this would be caused by calling the `erlang:exit/1` function.
15+
/// On JavaScript this variant is not used.
16+
Exited(Dynamic)
17+
}
18+
619
/// This function will catch any crash and convert it into a result rather than
720
/// crashing the process.
821
///
@@ -13,10 +26,8 @@ import gleam/dynamic.{type Dynamic}
1326
///
1427
@external(erlang, "exception_ffi", "rescue")
1528
@external(javascript, "./exception_ffi.mjs", "rescue")
16-
pub fn rescue(body: fn() -> a) -> Result(a, Dynamic)
29+
pub fn rescue(body: fn() -> a) -> Result(a, Exception)
1730

18-
// TODO: implement
19-
// TODO: test
2031
/// This function will run a cleanup function after the given body function, even
2132
/// if the body function crashes.
2233
///

src/exception_ffi.erl

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
rescue(F) ->
66
try {ok, F()}
7-
catch Tag:Term -> {error, {Tag, Term}}
7+
catch
8+
error:Term -> {error, {errored, Term}};
9+
throw:Term -> {error, {thrown, Term}};
10+
exit:Term -> {error, {exited, Term}}
811
end.
912

1013
defer(Cleanup, Body) ->

src/exception_ffi.mjs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import { Ok, Error as GError } from "./gleam.mjs";
2+
import { Errored, Thrown } from "./exception.mjs";
23

34
export function rescue(f) {
45
try {
56
return new Ok(f());
67
} catch (e) {
7-
return new GError(e);
8+
if (e instanceof Error) {
9+
return new GError(new Errored(e));
10+
} else {
11+
return new GError(new Thrown(e));
12+
}
813
}
914
}
1015

test/exception_test.gleam

+20-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,26 @@ pub fn rescue_ok_test() {
1010
let assert Ok(1) = exception.rescue(fn() { 1 })
1111
}
1212

13-
pub fn rescue_error_test() {
14-
let assert Error(_) = exception.rescue(fn() { panic })
13+
pub fn rescue_errored_test() {
14+
let assert Error(exception.Errored(_)) = exception.rescue(fn() { panic })
15+
}
16+
17+
@external(erlang, "erlang", "throw")
18+
@external(javascript, "./exception_test_ffi.mjs", "throw_")
19+
fn throw(a: whatever) -> Nil
20+
21+
pub fn rescue_thrown_test() {
22+
let assert Error(exception.Thrown(_)) =
23+
exception.rescue(fn() { throw("123") })
24+
}
25+
26+
@external(erlang, "erlang", "exit")
27+
@external(javascript, "./exception_test_ffi.mjs", "throw_")
28+
fn exit(a: whatever) -> Nil
29+
30+
@target(erlang)
31+
pub fn rescue_exited_test() {
32+
let assert Error(exception.Exited(_)) = exception.rescue(fn() { exit("123") })
1533
}
1634

1735
pub fn defer_ok_test() {

test/exception_test_ffi.mjs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function throw_(x) {
2+
throw x;
3+
}

0 commit comments

Comments
 (0)