File tree 7 files changed +56
-11
lines changed
7 files changed +56
-11
lines changed Original file line number Diff line number Diff line change 1
1
# Changelog
2
2
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
+
3
8
## v1.1.1 - 2024-01-16
4
9
5
10
- Relaxed the version constraint for ` gleam_stdlib ` to permit 1.x or 0.x
Original file line number Diff line number Diff line change 1
1
name = " exception"
2
- version = " 1.1.1 "
2
+ version = " 2.0.0 "
3
3
gleam = " >= 0.32.0"
4
4
description = " A tiny package for dealing with exceptions"
5
5
licences = [" Apache-2.0" ]
Original file line number Diff line number Diff line change 1
1
import gleam/dynamic . { type Dynamic }
2
2
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
+
6
19
/// This function will catch any crash and convert it into a result rather than
7
20
/// crashing the process.
8
21
///
@@ -13,10 +26,8 @@ import gleam/dynamic.{type Dynamic}
13
26
///
14
27
@ external ( erlang , "exception_ffi" , "rescue" )
15
28
@ 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 )
17
30
18
- // TODO: implement
19
- // TODO: test
20
31
/// This function will run a cleanup function after the given body function, even
21
32
/// if the body function crashes.
22
33
///
Original file line number Diff line number Diff line change 4
4
5
5
rescue (F ) ->
6
6
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 }}
8
11
end .
9
12
10
13
defer (Cleanup , Body ) ->
Original file line number Diff line number Diff line change 1
1
import { Ok , Error as GError } from "./gleam.mjs" ;
2
+ import { Errored , Thrown } from "./exception.mjs" ;
2
3
3
4
export function rescue ( f ) {
4
5
try {
5
6
return new Ok ( f ( ) ) ;
6
7
} 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
+ }
8
13
}
9
14
}
10
15
Original file line number Diff line number Diff line change @@ -10,8 +10,26 @@ pub fn rescue_ok_test() {
10
10
let assert Ok ( 1 ) = exception . rescue ( fn ( ) { 1 } )
11
11
}
12
12
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" ) } )
15
33
}
16
34
17
35
pub fn defer_ok_test ( ) {
Original file line number Diff line number Diff line change
1
+ export function throw_ ( x ) {
2
+ throw x ;
3
+ }
You can’t perform that action at this time.
0 commit comments