js_of_ocaml icon indicating copy to clipboard operation
js_of_ocaml copied to clipboard

[FEATURE REQUEST] Documentation for --enable=with-js-error along with caml_js_error_of_exception

Open Lupus opened this issue 5 years ago • 2 comments

Enabling with-js-error attaches fresh Error to all exceptions, and gives one ability to extract backtrace from JS error object via caml_js_error_of_exception. It's not exported from the jsoo API currently, and needs to be declared as an extern:

external js_error_of_exception :
  exn -> Js.error Js.t Js.opt = "caml_js_error_of_exception"

I've used it like this in Lwt async exception hook to log js backtrace when unhandled exception happens:

Lwt.async_exception_hook :=
    (fun e ->
       let js_backtrace =
         match Js.Opt.to_option (js_error_of_exception e) with
         | Some js_error ->
             (match Js.Optdef.to_option (js_error ##. stack) with
              | Some str -> str |> Js.to_string
              | None -> "(no stack)")
         | None -> "(no js_error attached)" in
       let err = Error.of_exn e in
       Logs.err
         (fun m ->
            m "async promise exception: %s, JS backtrace: %s"
              (Error.to_string_hum err) js_backtrace);
       exit 127)

Would be awesome to have caml_js_error_of_exception exposed in public API and have this way to get backtraces documented.

Lupus avatar Jul 27 '20 06:07 Lupus

There is Js.js_error_of_exn

hhugo avatar Jan 06 '22 08:01 hhugo

Introduced in https://github.com/ocsigen/js_of_ocaml/pull/1071

hhugo avatar Jan 06 '22 08:01 hhugo