SuppressedError() constructor

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

The SuppressedError() constructor creates SuppressedError objects.

Syntax

js
new SuppressedError(error, suppressed)
new SuppressedError(error, suppressed, message)

SuppressedError(error, suppressed)
SuppressedError(error, suppressed, message)

Note: SuppressedError() can be called with or without new. Both create a new SuppressedError instance.

Parameters

error

The new error that results in the suppression of suppressed.

suppressed

The error that was originally thrown and is now suppressed.

message Optional

An optional human-readable description of the aggregate error.

Note: SuppressedError() does not accept options like Error() and other subclasses do, because the semantics of cause overlaps with suppressed.

Examples

Creating a SuppressedError

js
try {
  throw new SuppressedError(
    new Error("New error"),
    new Error("Original error"),
    "Hello",
  );
} catch (e) {
  console.log(e.suppressed); // Error: "Original error"
  console.log(e.error); // Error: "New error"
}

Specifications

Specification
ECMAScript Async Explicit Resource Management
# sec-suppressederror-constructor

Browser compatibility

See also