File tree Expand file tree Collapse file tree 2 files changed +28
-0
lines changed Expand file tree Collapse file tree 2 files changed +28
-0
lines changed Original file line number Diff line number Diff line change @@ -45,6 +45,12 @@ external makeError : string -> error = "Error" [@@bs.new]
45
45
external isCamlExceptionOrOpenVariant :
46
46
'a -> bool = " caml_is_extension"
47
47
48
+ let anyToExnInternal obj =
49
+ if isCamlExceptionOrOpenVariant obj then
50
+ (Obj. magic obj : exn )
51
+ else
52
+ Error ((Obj. magic obj) : t)
53
+
48
54
let raiseError str =
49
55
raise (Obj. magic (makeError str : error ) : exn )
50
56
Original file line number Diff line number Diff line change @@ -43,6 +43,28 @@ external isCamlExceptionOrOpenVariant:
43
43
'a -> bool = " caml_is_extension"
44
44
(* * internal use only *)
45
45
46
+ val anyToExnInternal : 'a -> exn
47
+ (* *
48
+ * [anyToExnInternal obj] will take any value [obj] and wrap it
49
+ * in a Js.Exn.Error if given value is not an exn already. If
50
+ * [obj] is an exn, it will return [obj] without any changes.
51
+ *
52
+ * This function is mostly useful for cases where you want to unify a type of a value
53
+ * that potentially is either exn, a JS error, or any other JS value really (e.g. for
54
+ * a value passed to a Promise.catch callback)
55
+ *
56
+ * IMPORTANT: This is an internal API and may be changed / removed any time in the future.
57
+ *
58
+ * @example {[
59
+ * switch (Js.Exn.unsafeAnyToExn("test")) {
60
+ * | Js.Exn.Error(v) =>
61
+ * switch(Js.Exn.message(v)) {
62
+ * | Some(str) => Js.log("We won't end up here")
63
+ | None => Js.log2("We will land here: ", v)
64
+ * }
65
+ * }
66
+ * ]}
67
+ * **)
46
68
47
69
(* * Raise Js exception Error object with stacktrace *)
48
70
val raiseError : string -> 'a
You can’t perform that action at this time.
0 commit comments