diff --git a/CHANGES b/CHANGES index e7c8cd7b2..042308b6f 100644 --- a/CHANGES +++ b/CHANGES @@ -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) diff --git a/src/core/lwt_result.ml b/src/core/lwt_result.ml index 61333e716..b4b7a3d78 100644 --- a/src/core/lwt_result.ml +++ b/src/core/lwt_result.ml @@ -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 = diff --git a/src/core/lwt_result.mli b/src/core/lwt_result.mli index b2df2328a..948da10df 100644 --- a/src/core/lwt_result.mli +++ b/src/core/lwt_result.mli @@ -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, diff --git a/test/core/test_lwt_result.ml b/test/core/test_lwt_result.ml index 03f3adb14..8e354b630 100644 --- a/test/core/test_lwt_result.ml +++ b/test/core/test_lwt_result.ml @@ -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) );