add CCError.of_exn_trace

This commit is contained in:
Simon Cruanes 2015-10-02 19:39:25 +02:00
parent fdcba1122d
commit 096948e712
2 changed files with 22 additions and 1 deletions

View file

@ -59,7 +59,7 @@ let register_printer p = _printers := p :: !_printers
(* FIXME: just use {!Printexc.register_printer} instead? *)
let of_exn e =
let buf = Buffer.create 15 in
let buf = Buffer.create 32 in
let rec try_printers l = match l with
| [] -> Buffer.add_string buf (Printexc.to_string e)
| p :: l' ->
@ -69,6 +69,20 @@ let of_exn e =
try_printers !_printers;
`Error (Buffer.contents buf)
let of_exn_trace e =
let buf = Buffer.create 128 in
let rec try_printers l = match l with
| [] -> Buffer.add_string buf (Printexc.to_string e)
| p :: l' ->
try p buf e
with _ -> try_printers l'
in
try_printers !_printers;
Buffer.add_string buf "\nstack trace:\n";
Buffer.add_string buf
(Printexc.raw_backtrace_to_string (Printexc.get_raw_backtrace ()));
`Error (Buffer.contents buf)
let map f e = match e with
| `Ok x -> `Ok (f x)
| `Error s -> `Error s

View file

@ -50,6 +50,11 @@ val fail : 'err -> ('a,'err) t
val of_exn : exn -> ('a, string) t
(** [of_exn e] uses {!Printexc} to print the exception as a string *)
val of_exn_trace : exn -> ('a, string) t
(** [of_exn_trace e] is similar to [of_exn e], but it adds the stacktrace
to the error message
@since NEXT_RELEASE *)
val fail_printf : ('a, Buffer.t, unit, ('a,string) t) format4 -> 'a
(** [fail_printf format] uses [format] to obtain an error message
and then returns [`Error msg]
@ -205,3 +210,5 @@ This way a printer that doesn't know how to deal with an exception will
let other printers do it. *)
val register_printer : exn printer -> unit
(* TODO: deprecate, should use {!Printexc} *)