Skip to content

Commit

Permalink
Merge pull request #965 from ocsigen/fix-lwt-result-catch-interface
Browse files Browse the repository at this point in the history
Lwt_result.catch takes a function as parameter now
  • Loading branch information
raphael-proust committed Oct 27, 2022
2 parents fb47b31 + 7e8e07e commit f7d95ac
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
6 changes: 5 additions & 1 deletion CHANGES
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
===== dev =====

===== Build =====
====== Breaking API changes ======

* Lwt_result.catch now takes a function (unit -> 'a t) rather than a promise ('a t) (#965)

====== Build ======

* Remove unused dependency in dune file. (#969, Kate Deplaix)

Expand Down
2 changes: 1 addition & 1 deletion src/core/lwt_result.ml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ let map_err f e = map_error f e

let catch e =
Lwt.catch
(fun () -> ok e)
(fun () -> ok (e ()))
fail

let get_exn e =
Expand Down
6 changes: 3 additions & 3 deletions src/core/lwt_result.mli
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ val ok : 'a Lwt.t -> ('a, _) t
val error : 'b Lwt.t -> (_, 'b) t
(** @since 5.6.0 *)

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

val get_exn : ('a, exn) t -> 'a Lwt.t
(** [get_exn] is the opposite of {!catch}: it unwraps the result type,
Expand Down
16 changes: 14 additions & 2 deletions test/core/test_lwt_result.ml
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,25 @@ let suite =

test "catch"
(fun () ->
let x = Lwt.return 0 in
let x () = Lwt.return 0 in
Lwt.return (Lwt_result.catch x = Lwt_result.return 0)
);

test "catch, error case"
(fun () ->
let x = Lwt.fail Dummy_error in
let x () = Lwt.fail Dummy_error in
Lwt.return (Lwt_result.catch x = Lwt_result.fail Dummy_error)
);

test "catch, bound raise"
(fun () ->
let x () = Lwt.bind Lwt.return_unit (fun () -> raise Dummy_error) in
Lwt.return (Lwt_result.catch x = Lwt_result.fail Dummy_error)
);

test "catch, immediate raise"
(fun () ->
let x () = raise Dummy_error in
Lwt.return (Lwt_result.catch x = Lwt_result.fail Dummy_error)
);

Expand Down

0 comments on commit f7d95ac

Please sign in to comment.