Skip to content

Commit

Permalink
Reporter: improve error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
aantron committed Nov 7, 2021
1 parent e14d89f commit 6327d04
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 32 deletions.
11 changes: 9 additions & 2 deletions src/report/cobertura.ml
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,18 @@ let output
Util.find_source_file ~source_roots:source_paths ~ignore_missing_files in
let cobertura = cobertura ~resolver ~coverage in
let () = Util.mkdirs (Filename.dirname to_file) in
let oc = open_out to_file in
let oc =
try open_out to_file
with Sys_error message ->
Util.fatal "cannot open output file '%s': %s" to_file message
in
try
let fmt = Format.formatter_of_out_channel oc in
let () = pp_cobertura fmt cobertura in
close_out oc
with exn ->
with
| Sys_error message ->
Util.fatal "cannot write output file '%s': %s" to_file message
| exn ->
close_out_noerr oc;
raise exn
11 changes: 9 additions & 2 deletions src/report/coveralls.ml
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,11 @@ let output
in

Util.mkdirs (Filename.dirname to_file);
let ch = open_out to_file in
let ch =
try open_out to_file
with Sys_error message ->
Util.fatal "cannot open output file '%s': %s" to_file message
in
try
Printf.fprintf ch {|{
%s
Expand All @@ -129,7 +133,10 @@ let output
(String.concat ",\n" file_jsons);
close_out ch

with exn ->
with
| Sys_error message ->
Util.fatal "cannot write output file '%s': %s" to_file message
| exn ->
close_out_noerr ch;
raise exn

Expand Down
57 changes: 29 additions & 28 deletions src/report/html.ml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ let output_html_index title theme filename files =
files
in

let channel = open_out filename in
let channel =
try open_out filename
with Sys_error message ->
Util.fatal "cannot open output file '%s': %s" filename message
in
try
let write format = Printf.fprintf channel format in

Expand Down Expand Up @@ -97,7 +101,10 @@ let output_html_index title theme filename files =

close_out channel

with exn ->
with
| Sys_error message ->
Util.fatal "cannot write output file '%s': %s" filename message
| exn ->
close_out_noerr channel;
raise exn

Expand Down Expand Up @@ -132,28 +139,6 @@ let escape_line tab_size line offset points =
end;
Buffer.contents buff

let open_both in_file out_file =
let in_channel = open_in in_file in

try
let rec make_out_dir path =
if Sys.file_exists path then
()
else begin
let parent = Filename.dirname path in
make_out_dir parent;
Unix.mkdir path 0o755
end
in
make_out_dir (Filename.dirname out_file);

let out_channel = open_out out_file in
(in_channel, out_channel)

with e ->
close_in_noerr in_channel;
raise e



(* Individual HTML files corresponding to each source file. *)
Expand Down Expand Up @@ -188,8 +173,17 @@ let output_for_source_file
(offset, nb)))
in
let dirname, basename = split_filename filename in
let in_channel, out_channel =
open_both source_file_on_disk html_file_on_disk in
Util.mkdirs (Filename.dirname html_file_on_disk);
let in_channel =
try open_in source_file_on_disk
with Sys_error message ->
Util.fatal "cannot open source file '%s': %s" source_file_on_disk message
in
let out_channel =
try open_out html_file_on_disk
with Sys_error message ->
Util.fatal "cannot open output file '%s': %s" html_file_on_disk message
in
let rec make_path_to_report_root acc in_file_path_remaining =
if in_file_path_remaining = "" ||
in_file_path_remaining = Filename.current_dir_name ||
Expand Down Expand Up @@ -358,11 +352,18 @@ let output_for_source_file
(* Assets, such as CSS and JavaScript files. *)

let output_string_to_separate_file content filename =
let channel = open_out filename in
let channel =
try open_out filename
with Sys_error message ->
Util.fatal "cannot open output file '%s': %s" filename message
in
try
Printf.fprintf channel "%s" content;
close_out channel
with exn ->
with
| Sys_error message ->
Util.fatal "cannot write output file '%s': %s" filename message
| exn ->
close_out_noerr channel;
raise exn

Expand Down

0 comments on commit 6327d04

Please sign in to comment.