Skip to content

Commit 94a7a45

Browse files
Lwt_result.catch takes a function as parameter now
1 parent fb47b31 commit 94a7a45

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

src/core/lwt_result.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ let map_err f e = map_error f e
3131

3232
let catch e =
3333
Lwt.catch
34-
(fun () -> ok e)
34+
(fun () -> ok (e ()))
3535
fail
3636

3737
let get_exn e =

src/core/lwt_result.mli

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ val ok : 'a Lwt.t -> ('a, _) t
2323
val error : 'b Lwt.t -> (_, 'b) t
2424
(** @since 5.6.0 *)
2525

26-
val catch : 'a Lwt.t -> ('a, exn) t
27-
(** [catch x] behaves like [return y] if [x] evaluates to [y],
28-
and like [fail e] if [x] raises [e] *)
26+
val catch : (unit -> 'a Lwt.t) -> ('a, exn) t
27+
(** [catch x] behaves like [return y] if [x ()] evaluates to [y],
28+
and like [fail e] if [x ()] raises [e] *)
2929

3030
val get_exn : ('a, exn) t -> 'a Lwt.t
3131
(** [get_exn] is the opposite of {!catch}: it unwraps the result type,

test/core/test_lwt_result.ml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,25 @@ let suite =
8787

8888
test "catch"
8989
(fun () ->
90-
let x = Lwt.return 0 in
90+
let x () = Lwt.return 0 in
9191
Lwt.return (Lwt_result.catch x = Lwt_result.return 0)
9292
);
9393

9494
test "catch, error case"
9595
(fun () ->
96-
let x = Lwt.fail Dummy_error in
96+
let x () = Lwt.fail Dummy_error in
97+
Lwt.return (Lwt_result.catch x = Lwt_result.fail Dummy_error)
98+
);
99+
100+
test "catch, bound raise"
101+
(fun () ->
102+
let x () = Lwt.bind Lwt.return_unit (fun () -> raise Dummy_error) in
103+
Lwt.return (Lwt_result.catch x = Lwt_result.fail Dummy_error)
104+
);
105+
106+
test "catch, immediate raise"
107+
(fun () ->
108+
let x () = raise Dummy_error in
97109
Lwt.return (Lwt_result.catch x = Lwt_result.fail Dummy_error)
98110
);
99111

0 commit comments

Comments
 (0)