Skip to content

Commit

Permalink
exn: preserve backtraces
Browse files Browse the repository at this point in the history
  • Loading branch information
tatchi committed Aug 13, 2024
1 parent 8451877 commit 38c565f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
9 changes: 8 additions & 1 deletion exn.ml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,14 @@ let to_string exn =
let str = to_string

let fail ?exn fmt =
let fails s = match exn with None -> failwith s | Some exn -> failwith (s ^ " : " ^ to_string exn) in
let fails s =
match exn with
| None -> failwith s
| Some original_exn ->
let orig_bt = Printexc.get_raw_backtrace () in
let exn = Failure (s ^ " : " ^ to_string original_exn) in
Printexc.raise_with_backtrace exn orig_bt
in
ksprintf fails fmt

let invalid_arg fmt = ksprintf invalid_arg fmt
Expand Down
4 changes: 1 addition & 3 deletions exn_lwt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ open Printf
let catch f x = Lwt.try_bind (fun () -> f x) Lwt.return_some (fun _exn -> Lwt.return_none)
let map f x = Lwt.try_bind (fun () -> f x) (fun r -> Lwt.return (`Ok r)) (fun exn -> Lwt.return (`Exn exn))

let fail ?exn fmt =
let fails s = Lwt.fail_with @@ match exn with None -> s | Some exn -> s ^ " : " ^ Exn.to_string exn in
ksprintf fails fmt
let fail = Exn.fail

let invalid_arg fmt = ksprintf Lwt.fail_invalid_arg fmt

0 comments on commit 38c565f

Please sign in to comment.