Skip to content

Commit 001a4ca

Browse files
authored
Include stderr in error reports for easier debugging (#320)
Fixes #318
1 parent d5d3c0b commit 001a4ca

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

src/git_utils.ml

+14-5
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ let gitlab_repo ~bot_info ~gitlab_domain ~gitlab_full_name =
1111
|> Result.map ~f:(fun token ->
1212
f "https://oauth2:%s@%s/%s.git" token gitlab_domain gitlab_full_name )
1313

14-
let report_status ?(mask = []) command report code =
14+
let report_status ?(mask = []) ?(stderr_content = "") command report code =
15+
let stderr =
16+
if String.is_empty stderr_content then "" else stderr_content ^ "\n"
17+
in
1518
Error
1619
(List.fold_left
17-
~init:(f {|Command "%s" %s %d%s|} command report code "\n")
20+
~init:(f {|Command "%s" %s %d%s%s|} command report code "\n" stderr)
1821
~f:(fun acc m -> Str.global_replace (Str.regexp_string m) "XXXXX" acc)
1922
mask )
2023

@@ -89,12 +92,18 @@ let ( |&& ) command1 command2 = command1 ^ " && " ^ command2
8992
let execute_cmd ?(mask = []) command =
9093
Lwt_io.printf "Executing command: %s\n" command
9194
>>= fun () ->
92-
Lwt_unix.system command
93-
>|= fun status ->
95+
let process = Lwt_process.open_process_full (Lwt_process.shell command) in
96+
let stdout_pipe = copy_stream ~src:process#stdout ~dst:Lwt_io.stdout in
97+
let stderr_pipe = copy_stream ~src:process#stderr ~dst:Lwt_io.stderr in
98+
(* Capture stdout and stderr in parallel *)
99+
(* Wait for the process to finish *)
100+
let+ _stdout_content = stdout_pipe
101+
and+ stderr_content = stderr_pipe
102+
and+ status = process#status in
94103
match status with
95104
| Unix.WEXITED code ->
96105
if Int.equal code 0 then Ok ()
97-
else report_status ~mask command "exited with status" code
106+
else report_status ~mask ~stderr_content command "exited with status" code
98107
| Unix.WSIGNALED signal ->
99108
report_status ~mask command "was killed by signal number" signal
100109
| Unix.WSTOPPED signal ->

src/helpers.ml

+14
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,17 @@ let download ~uri dest =
169169

170170
let download_to ~uri chan =
171171
download_cps ~uri ~with_file:(fun write_to -> write_to chan)
172+
173+
let copy_stream ~src ~dst =
174+
let open Lwt.Infix in
175+
let buffer = Buffer.create 1024 in
176+
let rec aux () =
177+
Lwt_io.read_char_opt src
178+
>>= function
179+
| Some c ->
180+
Buffer.add_char buffer c ;
181+
Lwt_io.write_char dst c >>= aux
182+
| None ->
183+
Lwt.return (Buffer.contents buffer)
184+
in
185+
aux ()

src/helpers.mli

+3
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,6 @@ val download : uri:Uri.t -> string -> (unit, string) Lwt_result.t
4040

4141
val download_to :
4242
uri:Uri.t -> Lwt_io.output_channel -> (unit, string) Lwt_result.t
43+
44+
val copy_stream :
45+
src:Lwt_io.input_channel -> dst:Lwt_io.output_channel -> string Lwt.t

0 commit comments

Comments
 (0)