JsExn
Provide utilities for dealing with JS exceptions.
JS exceptions can be of any type, even though they should be of type Error
of one of its subclasses.
See throw
on MDN.
t
type t = unknown
Represents a JS exception
fromException
let fromException: exn => option<t>
anyToExnInternal
let anyToExnInternal: 'a => exn
anyToExnInternal(obj)
will take any value obj
and wrap it
in a JsExn if given value is not an exn already. If
obj
is an exn, it will return obj
without any changes.
This function is mostly useful for cases where you want to unify a type of a value that potentially is either exn, a JS error, or any other JS value really (e.g. for a value passed to a Promise.catch callback)
IMPORTANT: This is an internal API and may be changed / removed any time in the future.
stack
let stack: t => option<string>
stack(jsExn)
retrieves the stack
property of the exception, if it exists. The stack is a list of what functions were called, and what files they are defined in, prior to the error happening.
See Error.prototype.stack
on MDN.
Example
RESCRIPTtry {JsError.make("error")->JsError.throw} catch {
| JsExn(e) => Console.log(JsExn.stack(e)) // Logs `stack`
| _ => assert(false)
}
message
let message: t => option<string>
message(error)
retrieves the message
property of the error, if it exists.
See Error.prototype.message
on MDN.
Example
RESCRIPTtry {JsError.SyntaxError.throwWithMessage("Some message here")} catch {
| JsExn(e) => e->JsExn.message->Option.getExn == "Some message here"
| _ => assert(false)
}
name
let name: t => option<string>
name(error)
retrieves the name
property of the error, if it exists.
See Error.prototype.name
on MDN.
Example
RESCRIPTtry {JsError.SyntaxError.throwWithMessage("Some message here")} catch {
| JsExn(e) => e->JsExn.name->Option.getExn == "SyntaxError"
| _ => assert(false)
}
fileName
let fileName: t => option<string>
fileName(error)
retrieves the fileName
property of the error, if it exists.
See Error.prototype.fileName
on MDN.
throw
let throw: 'a => 'b
Throws the given value, terminating execution unless caught by a surrounding try/catch block.
This is meant to be used when a JS API is based on throwing values that are not of type Error
or its subclasses.
ignore
let ignore: t => unit
ignore(jsExn)
ignores the provided JS exception and returns unit.
This helper is useful when you want to discard a value (for example, the result of an operation with side effects) without having to store or process it further.