SuppressedError
Limited availability
This feature is not Baseline because it does not work in some of the most widely-used browsers.
TheSuppressedError
object represents an error generated while handing another error. It is generated during resource disposal usingusing
orawait using
.
Compared toAggregateError
,SuppressedError
is used to represent a single error that is suppressed by another error, whileAggregateError
represents a list of unrelated errors. It is possible, though, for aSuppressedError
to contain a chain of suppressed errors (e.suppressed.suppressed.suppressed...
). It is also semantically different fromcause
because the error is notcaused by another error, buthappens when handling another error.
SuppressedError
is a subclass ofError
.
In this article
Constructor
SuppressedError()
Creates a new
SuppressedError
object.
Instance properties
Also inherits instance properties from its parentError
.
These properties are defined onSuppressedError.prototype
and shared by allSuppressedError
instances.
SuppressedError.prototype.constructor
The constructor function that created the instance object. For
SuppressedError
instances, the initial value is theSuppressedError
constructor.SuppressedError.prototype.name
Represents the name for the type of error. For
SuppressedError.prototype.name
, the initial value is"SuppressedError"
.
Note:SuppressedError
never has thecause
property, because the semantics ofcause
overlaps withsuppressed
.
These properties are own properties of eachSuppressedError
instance.
error
A reference to the error that results in the suppression.
suppressed
A reference to the error that is suppressed by
error
.
Instance methods
Inherits instance methods from its parentError
.
Examples
>Catching an SuppressedError
ASuppressedError
is thrown when an error occurs duringresource disposal. Throwing an error causes scope cleanup, and each disposer during the cleanup can throw its own error. All these errors are collected into a chain ofSuppressedError
instances, with the original error as thesuppressed
property and the new error thrown by the next disposer as theerror
property.
try { using resource1 = { [Symbol.dispose]() { throw new Error("Error while disposing resource1"); }, }; using resource2 = { [Symbol.dispose]() { throw new Error("Error while disposing resource2"); }, }; throw new Error("Original error");} catch (e) { console.log(e instanceof SuppressedError); // true console.log(e.message); // "An error was suppressed during disposal" console.log(e.name); // "SuppressedError" console.log(e.error); // Error: Error while disposing resource1 console.log(e.suppressed); // SuppressedError: An error was suppressed during disposal console.log(e.suppressed.error); // Error: Error while disposing resource2 console.log(e.suppressed.suppressed); // Error: Original error}
The chain looks like this:
SuppressedError --suppressed--> SuppressedError --suppressed--> Original error | | error error v vError while disposing resource1 Error while disposing resource2 (Disposal happens later) (Disposal happens earlier)
Creating an SuppressedError
try { throw new SuppressedError( new Error("New error"), new Error("Original error"), "Hello", );} catch (e) { console.log(e instanceof SuppressedError); // true console.log(e.message); // "Hello" console.log(e.name); // "SuppressedError" console.log(e.error); // Error: "New error" console.log(e.suppressed); // Error: "Original error"}
Specifications
Specification |
---|
ECMAScript Async Explicit Resource Management> # sec-suppressederror-objects> |
Browser compatibility
Loading…