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