mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
add CCResult.add_ctx{,f} for replacing stack traces
This commit is contained in:
parent
e6221d7e50
commit
f27f7757de
2 changed files with 30 additions and 0 deletions
|
|
@ -31,6 +31,22 @@ let fail_fprintf format =
|
||||||
(fun out -> Format.pp_print_flush out (); fail (Buffer.contents buf))
|
(fun out -> Format.pp_print_flush out (); fail (Buffer.contents buf))
|
||||||
out format
|
out format
|
||||||
|
|
||||||
|
let add_ctx msg x = match x with
|
||||||
|
| Error e -> Error (e ^ "\ncontext:" ^ msg)
|
||||||
|
| Ok x -> Ok x
|
||||||
|
|
||||||
|
let add_ctxf msg =
|
||||||
|
let buf = Buffer.create 64 in
|
||||||
|
let out = Format.formatter_of_buffer buf in
|
||||||
|
Format.kfprintf
|
||||||
|
(fun out e -> Format.pp_print_flush out (); add_ctx (Buffer.contents buf) e)
|
||||||
|
out msg
|
||||||
|
|
||||||
|
(*$=
|
||||||
|
(Error "error\ncontext:message(number 42, foo: true)") \
|
||||||
|
(add_ctxf "message(number %d, foo: %B)" 42 true (Error "error"))
|
||||||
|
*)
|
||||||
|
|
||||||
let of_exn e =
|
let of_exn e =
|
||||||
let msg = Printexc.to_string e in
|
let msg = Printexc.to_string e in
|
||||||
Error msg
|
Error msg
|
||||||
|
|
|
||||||
|
|
@ -42,6 +42,20 @@ val fail_fprintf : ('a, Format.formatter, unit, ('a, string) t) format4 -> 'a
|
||||||
(** [fail_printf format] uses [format] to obtain an error message
|
(** [fail_printf format] uses [format] to obtain an error message
|
||||||
and then returns [Error msg] *)
|
and then returns [Error msg] *)
|
||||||
|
|
||||||
|
val add_ctx : string -> ('a, string) t -> ('a, string) t
|
||||||
|
(** [add_ctx msg] leaves [Ok x] untouched, but transforms
|
||||||
|
[Error s] into [Error s'] where [s'] contains the additional
|
||||||
|
context given by [msg]
|
||||||
|
@since NEXT_RELEASE *)
|
||||||
|
|
||||||
|
val add_ctxf : ('a, Format.formatter, unit, ('b, string) t -> ('b, string) t) format4 -> 'a
|
||||||
|
(** [add_ctxf format_message] is similar to {!add_ctx} but with
|
||||||
|
{!Format} for printing the message (eagerly).
|
||||||
|
Example: {[
|
||||||
|
add_ctxf "message(number %d, foo: %B)" 42 true (Error "error)"
|
||||||
|
]}
|
||||||
|
@since NEXT_RELEASE *)
|
||||||
|
|
||||||
val map : ('a -> 'b) -> ('a, 'err) t -> ('b, 'err) t
|
val map : ('a -> 'b) -> ('a, 'err) t -> ('b, 'err) t
|
||||||
(** Map on success *)
|
(** Map on success *)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue