Source file ojs_exn.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
(* The gen_js_api is released under the terms of an MIT-like license.     *)
(* See the attached LICENSE file.                                         *)
(* Copyright 2015 by LexiFi.                                              *)

type t = Ojs.t

let name x = Ojs.string_of_js (Ojs.get_prop_ascii x "name")
let message x = Ojs.string_of_js (Ojs.get_prop_ascii x "message")
let stack x = Ojs.option_of_js Ojs.string_of_js (Ojs.get_prop_ascii x "stack")
let to_string x = Ojs.string_of_js (Ojs.call x "toString" [||])

exception Error of t

let () = Callback.register_exception "jsError" (Error (Ojs.obj [||]))

(* The js_of_ocaml runtime expects to have this registered.
   So it's probably a bad idea to use both this Ojs_exn module
   and the js_of_ocaml standard library. *)

let () =
  Printexc.register_printer (function
      | Error x -> Some (to_string x)
      | _ -> None
    )