Skip to content

Commit

Permalink
Use failwith instead of Lwt.fail_with
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
MisterDA committed Sep 3, 2024
1 parent 546b0ce commit ae18e09
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 9 deletions.
7 changes: 2 additions & 5 deletions lib/btrfs_store.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions lib/os.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -300,4 +300,3 @@ let read_lines name process =
| Some s -> loop ((process s) :: acc)
| None -> close_in ic; acc in
loop []

0 comments on commit ae18e09

Please sign in to comment.