mirror of
https://github.com/CatalaLang/catala.git
synced 2024-11-08 07:51:43 +03:00
Add delayed errors in Message
This commit is contained in:
parent
01b03b69a0
commit
421d281fc1
@ -327,6 +327,7 @@ open Content
|
||||
(** {1 Error exception} *)
|
||||
|
||||
exception CompilerError of Content.t
|
||||
exception CompilerErrors of Content.t list
|
||||
|
||||
(** {1 Error printing} *)
|
||||
|
||||
@ -404,3 +405,31 @@ let result = make ~level:Result ~cont:emit
|
||||
let results r = emit (List.flatten (List.map of_result r)) Result
|
||||
let warning = make ~level:Warning ~cont:emit
|
||||
let error = make ~level:Error ~cont:(fun m _ -> raise (CompilerError m))
|
||||
|
||||
(* Multiple errors handling *)
|
||||
let global_errors = ref None
|
||||
|
||||
let delayed_error x =
|
||||
make ~level:Error ~cont:(fun m _ ->
|
||||
match !global_errors with
|
||||
| None ->
|
||||
failwith
|
||||
"delayed error called outside scope: encapsulate using \
|
||||
'with_delayed_errors' first"
|
||||
| Some l ->
|
||||
global_errors := Some (m :: l);
|
||||
x)
|
||||
|
||||
let with_delayed_errors (f : unit -> 'a) : 'a =
|
||||
(match !global_errors with
|
||||
| None -> global_errors := Some []
|
||||
| Some _ -> failwith "delayed error scope already initialized");
|
||||
let r = f () in
|
||||
match !global_errors with
|
||||
| None -> assert false
|
||||
| Some [] ->
|
||||
global_errors := None;
|
||||
r
|
||||
| Some errs ->
|
||||
global_errors := None;
|
||||
raise (CompilerErrors (List.rev errs))
|
||||
|
@ -60,9 +60,10 @@ end
|
||||
(** This functions emits the message according to the emission type defined by
|
||||
[Cli.message_format_flag]. *)
|
||||
|
||||
(** {1 Error exception} *)
|
||||
(** {1 Error exceptions} *)
|
||||
|
||||
exception CompilerError of Content.t
|
||||
exception CompilerErrors of Content.t list
|
||||
|
||||
(** {1 Some formatting helpers}*)
|
||||
|
||||
@ -98,5 +99,15 @@ val log : ('a, unit) emitter
|
||||
val debug : ('a, unit) emitter
|
||||
val result : ('a, unit) emitter
|
||||
val warning : ('a, unit) emitter
|
||||
val error : ('a, 'b) emitter
|
||||
val error : ('a, 'exn) emitter
|
||||
val results : Content.message list -> unit
|
||||
|
||||
(** Multiple errors *)
|
||||
|
||||
val with_delayed_errors : (unit -> 'a) -> 'a
|
||||
(** [with_delayed_errors f] calls [f] and registers each error triggered using
|
||||
[delayed_error].
|
||||
|
||||
@raise CompilerErrors when delayed errors were registered. *)
|
||||
|
||||
val delayed_error : 'b -> ('a, 'b) emitter
|
||||
|
Loading…
Reference in New Issue
Block a user