Skip to content

Commit

Permalink
Lib: expose API to attached and retrieve js errors to/from ocaml exce…
Browse files Browse the repository at this point in the history
…ptions (#1071)
  • Loading branch information
hhugo authored Nov 12, 2020
1 parent eb95b63 commit 468b920
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# ??? (??) - ??
## Features/Changes
* Compiler: complete support for OCaml 4.12
* Lib: expose API to attached and retrieve js errors to/from ocaml exceptions

# 3.8.0 (2020-10-21) - London
## Features/Changes
Expand Down
4 changes: 4 additions & 0 deletions lib/js_of_ocaml/js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,10 @@ let _ = Callback.register_exception "jsError" (Error (Unsafe.obj [||]))

let raise_js_error : error t -> 'a = Unsafe.js_expr "(function (exn) { throw exn })"

external exn_with_js_backtrace : exn -> force:bool -> exn = "caml_exn_with_js_backtrace"

external js_error_of_exn : exn -> error t opt = "caml_js_error_of_exception"

class type json =
object
method parse : js_string t -> 'a meth
Expand Down
13 changes: 13 additions & 0 deletions lib/js_of_ocaml/js.mli
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,19 @@ val string_of_error : error t -> string

val raise_js_error : error t -> 'a

val exn_with_js_backtrace : exn -> force:bool -> exn
(** Attach a JavasScript error to an OCaml exception. if [force = false] and a
JavasScript error is already attached, it will do nothing. This function is useful to
store and retrieve information about JavaScript stack traces.
Attaching JavasScript errors will happen automatically when compiling with
[--enable with-js-error].
*)

val js_error_of_exn : exn -> error t opt
(** Extract a JavaScript error attached to an OCaml exception, if any. This is useful to
inspect an eventual stack strace, especially when sourcemap is enabled. *)

exception Error of error t
(** The [Error] exception wrap javascript exceptions when caught by OCaml code.
In case the javascript exception is not an instance of javascript [Error],
Expand Down
8 changes: 8 additions & 0 deletions lib/js_of_ocaml/js_of_ocaml_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ void caml_create_file () {
fprintf(stderr, "Unimplemented Javascript primitive caml_create_file!\n");
exit(1);
}
void caml_exn_with_js_backtrace () {
fprintf(stderr, "Unimplemented Javascript primitive caml_exn_with_js_backtrace!\n");
exit(1);
}
void caml_int64_create_lo_mi_hi () {
fprintf(stderr, "Unimplemented Javascript primitive caml_int64_create_lo_mi_hi!\n");
exit(1);
Expand All @@ -48,6 +52,10 @@ void caml_js_equals () {
fprintf(stderr, "Unimplemented Javascript primitive caml_js_equals!\n");
exit(1);
}
void caml_js_error_of_exception () {
fprintf(stderr, "Unimplemented Javascript primitive caml_js_error_of_exception!\n");
exit(1);
}
void caml_js_eval_string () {
fprintf(stderr, "Unimplemented Javascript primitive caml_js_eval_string!\n");
exit(1);
Expand Down

0 comments on commit 468b920

Please sign in to comment.