Error

Functions for working with JavaScript exceptions.

See Error on MDN.

fileName

Deprecated

RESCRIPT
let fileName: t => option<string>

fileName(error) retrieves the fileName property of the error, if it exists.

See Error.prototype.fileName on MDN.

fromException

Deprecated

RESCRIPT
let fromException: exn => option<t>

ignore

Deprecated

RESCRIPT
let ignore: t => unit

ignore(error) ignores the provided error 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.

make

Deprecated

RESCRIPT
let make: string => t

make(message) creates a new error, setting its message to the provided value.

See Error on MDN.

Example

RESCRIPT
let error = Error.make("Some message here") Console.log(error->Error.message) // Logs "Some message here" to the console Console.log(error->Error.name) // Logs "Error" to the console, because this is a regular error

message

Deprecated

RESCRIPT
let message: t => option<string>

message(error) retrieves the message property of the error, if it exists.

See Error.prototype.message on MDN.

Example

RESCRIPT
let error = Error.SyntaxError.make("Some message here") Console.log(error->Error.message) // Logs "Some message here" to the console

name

Deprecated

RESCRIPT
let name: t => option<string>

name(error) retrieves the name property of the error, if it exists.

See Error.prototype.name on MDN.

Example

RESCRIPT
let error = Error.SyntaxError.make("Some message here") Console.log(error->Error.name) // Logs "SyntaxError" to the console

panic

Deprecated

RESCRIPT
let panic: string => 'a

Throws a panic exception with the given message.

A panic exception is a native JavaScript exception that is not intended to be caught and handled. Compared to a ReScript exception this will give a better stack trace and debugging experience.

Examples

RESCRIPT
try { Error.panic("Uh oh. This was unexpected!") } catch { | Exn.Error(obj) => switch Exn.message(obj) { | Some(m) => assert(m == "Panic! Uh oh. This was unexpected!") | None => assert(false) } | _ => assert(false) }

raise

Deprecated

RESCRIPT
let raise: t => 'a

Raises (throws in JavaScript language) the provided Error.t, which will stop execution.

Examples

RESCRIPT
let error = Error.make("Everything is upside down.") if 5 > 10 { error->Error.raise } else { Console.log("Phew, sanity still rules.") }

stack

Deprecated

RESCRIPT
let stack: t => option<string>

stack(error) retrieves the stack property of the error, 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

RESCRIPT
let error = Error.make("error") Console.log(error->Error.stack) // Logs `stack` if it exists on `someError`

t

Deprecated

RESCRIPT
type t = Exn.t

Represents a JavaScript exception.

throw

Deprecated

RESCRIPT
let throw: t => 'a

Throws the given exception, terminating execution unless caught by a surrounding try/catch block.

Examples

RESCRIPT
let error = Error.make("Everything is upside down.") if 5 > 10 { Error.throw(error) } else { Console.log("Phew, sanity still rules.") }

toException

Deprecated

Use functions from JsExn instead

RESCRIPT
let toException: t => exn

Turns an Error.t into an exn.

Examples

RESCRIPT
let error = Error.make("Something went wrong.") let asExn = error->Error.toException // `asExn` is now type `exn`