Skip to content

Commit a0d6f13

Browse files
authored
[#55] Provide a better error message in case of failure while cloning the repository (#56)
Fixes #55 Before this commit, the function that handled invoking the cloning command did not perform any error handling. This PR attempts to fix that ; this means that `Shell.proc` now returns a `result` instead of `unit`, which is a breaking change. As such, the code that depends on this function - such as `clone_repo` in `Tui.Init` was updated to handle the result and exit with a better error message (well, at least one that is more useful). Things that I am still not convinced about in this fix: - The return type of `Shell.proc` is `(unit, string) result`. I am not really sure about crafting the error message there. - `clone_repo` currently exits, but since it is an "internal" function (it is not exposed in the interface of the module), it is probably safer to make it return a `result` as well (as it is fallible) and let a public function exit on failure.
1 parent 464691e commit a0d6f13

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

lib/shell/shell.ml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
let proc cmd =
22
Printf.eprintf "🐚 %s\n%!" cmd;
3-
let _exit_code = Unix.system cmd in
4-
()
3+
match Unix.system cmd with
4+
| Unix.WEXITED 0 -> Ok ()
5+
| Unix.WEXITED return_code ->
6+
Error
7+
(Printf.sprintf "command '%s' failed with code %d" (String.escaped cmd)
8+
return_code)
9+
| Unix.WSIGNALED number | Unix.WSTOPPED number ->
10+
Error
11+
(Printf.sprintf "command '%s' was stopped by signal (%d)"
12+
(String.escaped cmd) number)
513

614
let collect_chan (channel : in_channel) : string =
715
let rec loop acc =

lib/shell/shell.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Redirect the called process stdout and stderr to the current process stdout.
44
55
Also print the command with a pretty prompt. *)
6-
val proc : string -> unit
6+
val proc : string -> (unit, string) result
77

88
(** Run the given CLI command as external process and collect its stdout to the
99
resulting string. *)

lib/tui/init/init.ml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ let clone_repo ~owner_repo ~local_path =
2222
Printf.sprintf "git clone [email protected]:%s/%s.git %s" owner repo
2323
temp_dir
2424
in
25-
Shell.proc cmd;
25+
Shell.proc cmd
26+
|> Result.iter_error (fun message ->
27+
Printf.eprintf "❌ %s\n" message;
28+
exit 1);
29+
2630
temp_dir
2731

2832
let read_root_tree ~root_dir_path =

0 commit comments

Comments
 (0)