DocsPlaygroundBlogCommunityPackages
  • Playground
  • Blog
  • Community
  • Packages
  • X
  • Bluesky
  • GitHub
  • Forum
Language ManualAPISyntax LookupReact
Overview
Stdlib
submodules
  • Array
  • ArrayBuffer
  • AsyncIterator
  • BigInt
  • BigInt64Array
    • Constants
    BigUint64Array
    • Constants
  • Bool
  • Console
  • DataView
  • Date
    • UTC
  • Dict
  • Error
    • URIError
    • TypeError
    • SyntaxError
    • ReferenceError
    • RangeError
    • EvalError
  • Exn
  • Float
    • Constants
    Float32Array
    • Constants
    Float64Array
    • Constants
    Int
    • Ref
    • Bitwise
    • Constants
    Int16Array
    • Constants
    Int32Array
    • Constants
    Int8Array
    • Constants
  • IntervalId
  • Intl
    • Segments
    • Segmenter
    • RelativeTimeFormat
    • PluralRules
    • NumberFormat
      • Grouping
    • Locale
    • ListFormat
    • DateTimeFormat
    • Collator
    • Common
  • Iterator
  • JSON
    • Decode
    • Encode
    • Classify
    JsError
    • t
      t
    • v
      stack
    • v
      message
    • v
      name
    • v
      fileName
    • v
      make
    • v
      throw
    • v
      throwWithMessage
    • v
      panic
    • v
      toJsExn
    • v
      ignore
    • URIError
    • TypeError
    • SyntaxError
    • ReferenceError
    • RangeError
    • EvalError
  • JsExn
  • Lazy
  • List
  • Map
  • Math
    • Int
    • Constants
  • Null
  • Nullable
  • Object
  • Option
  • Ordering
  • Pair
  • Promise
  • RegExp
    • Result
  • Result
  • Set
  • String
  • Symbol
  • TimeoutId
  • Type
    • Classify
  • TypedArray
  • Uint16Array
    • Constants
    Uint32Array
    • Constants
    Uint8Array
    • Constants
    Uint8ClampedArray
    • Constants
  • WeakMap
  • WeakSet
  • Docs / API / Stdlib / Jserror

    JsError

    Functions for working with JavaScript errors.

    See Error on MDN.

    t

    RESCRIPT
    type t

    Represents a JavaScript error.

    stack

    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 = JsError.make("error") Console.log(error->JsError.stack) // Logs `stack` if it exists on `someError`

    message

    RESCRIPT
    let message: t => string

    message(error) retrieves the message property of the error.

    See Error.prototype.message on MDN.

    Example

    RESCRIPT
    let error = JsError.SyntaxError.make("Some message here") error->JsError.message == "Some message here"

    name

    RESCRIPT
    let name: t => string

    name(error) retrieves the name property of the error.

    See Error.prototype.name on MDN.

    Example

    RESCRIPT
    let error = JsError.SyntaxError.make("Some message here") error->JsError.name == "SyntaxError"

    fileName

    RESCRIPT
    let fileName: t => option<string>

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

    See Error.prototype.fileName on MDN.

    make

    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 = JsError.make("Some message here") error->JsError.message == "Some message here" error->JsError.name == "Error"

    throw

    RESCRIPT
    let throw: t => 'a

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

    Examples

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

    throwWithMessage

    RESCRIPT
    let throwWithMessage: string => 'a

    Creates a new Error with the provided message and throws it.

    JsError.throwWithMessage("message") is equivalent to JsError.make("message")->JsError.throw.

    panic

    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 { JsError.panic("Uh oh. This was unexpected!") } catch { | JsExn(obj) => switch JsExn.message(obj) { | Some(m) => assert(m == "Panic! Uh oh. This was unexpected!") | None => assert(false) } | _ => assert(false) }

    toJsExn

    RESCRIPT
    let toJsExn: t => JsExn.t

    Casts a JsError.t to a JsExn.t.

    This is useful when you want to compare a JS exception and a JS error.

    ignore

    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.

    Types and values
    • t
      t
    • v
      stack
    • v
      message
    • v
      name
    • v
      fileName
    • v
      make
    • v
      throw
    • v
      throwWithMessage
    • v
      panic
    • v
      toJsExn
    • v
      ignore

    © 2025 The ReScript Project

    About
    • Community
    • ReScript Association
    Find us on