From 771a19875d066ad975eb8fc34e1ad8d51326858e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonin=20D=C3=A9cimo?= Date: Tue, 3 Sep 2024 15:43:34 +0200 Subject: [PATCH] Use failwith instead of Lwt.fail_with Lwt's documentation reads: > In most cases, it is better to use `failwith s` from the standard > library. and > Whenever possible, it is recommended to use `raise exn` instead, as > raise captures a backtrace, while `Lwt.fail` does not. If you call > `raise exn` in a callback that is expected by Lwt to return a > promise, Lwt will automatically wrap `exn` in a rejected promise, > but the backtrace will have been recorded by the OCaml runtime. > > For example, `bind`'s second argument is a callback which returns a > promise. And so it is recommended to use `raise` in the body of that > callback. > > Use `Lwt.fail` only when you specifically want to create a rejected > promise, to pass to another function, or store in a data structure. Prefer to capture backtraces to improve debugability. --- lib/btrfs_store.ml | 7 ++----- lib/os.ml | 7 +++---- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/lib/btrfs_store.ml b/lib/btrfs_store.ml index bab2763f..5974dffc 100644 --- a/lib/btrfs_store.ml +++ b/lib/btrfs_store.ml @@ -90,11 +90,8 @@ let check_kernel_version () = | Some maj, Some min when (maj, min) >= (5, 8) -> Lwt.return_unit | Some maj, Some min -> - Lwt.fail_with - (Fmt.str - "You need at least linux 5.8 to use the btrfs backend, \ - but current kernel version is '%d.%d'" - maj min) + Fmt.failwith "You need at least linux 5.8 to use the btrfs backend, \ + but current kernel version is '%d.%d'" maj min | _, _ -> Fmt.failwith "Could not parse kernel version %S" kver end diff --git a/lib/os.ml b/lib/os.ml index 3887fa3a..cd722fcc 100644 --- a/lib/os.ml +++ b/lib/os.ml @@ -105,8 +105,8 @@ let exec ?timeout ?cwd ?stdin ?stdout ?stderr ?(is_success=((=) 0)) ?(cmd="") ar let pp f = pp_cmd f (cmd, argv) in !lwt_process_exec ?timeout ?cwd ?stdin ?stdout ?stderr ~pp (cmd, Array.of_list argv) >>= function | Ok n when is_success n -> Lwt.return_unit - | Ok n -> Lwt.fail_with (Fmt.str "%t failed with exit status %d" pp n) - | Error (`Msg m) -> Lwt.fail (Failure m) + | Ok n -> Fmt.failwith "%t failed with exit status %d" pp n + | Error (`Msg m) -> failwith m let running_as_root = not (Sys.unix) || Unix.getuid () = 0 @@ -205,7 +205,7 @@ let pread_all ?stdin ~pp ?(cmd="") argv = >>= fun (stdin, stdout) -> child >>= function | Ok i -> Lwt.return (i, stdin, stdout) - | Error (`Msg m) -> Lwt.fail (Failure m) + | Error (`Msg m) -> failwith m let check_dir x = match Unix.lstat x with @@ -300,4 +300,3 @@ let read_lines name process = | Some s -> loop ((process s) :: acc) | None -> close_in ic; acc in loop [] -