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
    • URIError
    • TypeError
    • SyntaxError
    • ReferenceError
    • RangeError
    • EvalError
  • JsExn
    • t
      t
    • v
      fromException
    • v
      anyToExnInternal
    • v
      stack
    • v
      message
    • v
      name
    • v
      fileName
    • v
      throw
    • v
      ignore
  • 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 / Jsexn

    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

    RESCRIPT
    type t = unknown

    Represents a JS exception

    fromException

    RESCRIPT
    let fromException: exn => option<t>

    anyToExnInternal

    RESCRIPT
    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

    RESCRIPT
    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

    RESCRIPT
    try {JsError.make("error")->JsError.throw} catch { | JsExn(e) => Console.log(JsExn.stack(e)) // Logs `stack` | _ => assert(false) }

    message

    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
    try {JsError.SyntaxError.throwWithMessage("Some message here")} catch { | JsExn(e) => e->JsExn.message->Option.getExn == "Some message here" | _ => assert(false) }

    name

    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
    try {JsError.SyntaxError.throwWithMessage("Some message here")} catch { | JsExn(e) => e->JsExn.name->Option.getExn == "SyntaxError" | _ => assert(false) }

    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.

    throw

    RESCRIPT
    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

    RESCRIPT
    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.

    Types and values
    • t
      t
    • v
      fromException
    • v
      anyToExnInternal
    • v
      stack
    • v
      message
    • v
      name
    • v
      fileName
    • v
      throw
    • v
      ignore

    © 2025 The ReScript Project

    About
    • Community
    • ReScript Association
    Find us on