-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Add Error.prototype.cause
#47020
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add Error.prototype.cause
#47020
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,55 @@ | ||
interface ErrorOptions { | ||
cause?: Error; | ||
cause?: Error; | ||
} | ||
|
||
interface Error { | ||
cause?: Error; | ||
} | ||
|
||
interface ErrorConstructor { | ||
new(message?: string, options?: ErrorOptions): Error; | ||
(message?: string, options?: ErrorOptions): Error; | ||
new (message?: string, options?: ErrorOptions): Error; | ||
(message?: string, options?: ErrorOptions): Error; | ||
} | ||
|
||
interface EvalErrorConstructor { | ||
new (message?: string, options?: ErrorOptions): EvalError; | ||
(message?: string, options?: ErrorOptions): EvalError; | ||
} | ||
|
||
interface RangeErrorConstructor { | ||
new (message?: string, options?: ErrorOptions): RangeError; | ||
(message?: string, options?: ErrorOptions): RangeError; | ||
} | ||
|
||
interface ReferenceErrorConstructor { | ||
new (message?: string, options?: ErrorOptions): ReferenceError; | ||
(message?: string, options?: ErrorOptions): ReferenceError; | ||
} | ||
|
||
interface SyntaxErrorConstructor { | ||
new (message?: string, options?: ErrorOptions): SyntaxError; | ||
(message?: string, options?: ErrorOptions): SyntaxError; | ||
} | ||
|
||
interface TypeErrorConstructor { | ||
new (message?: string, options?: ErrorOptions): TypeError; | ||
(message?: string, options?: ErrorOptions): TypeError; | ||
} | ||
|
||
interface URIErrorConstructor { | ||
new (message?: string, options?: ErrorOptions): URIError; | ||
(message?: string, options?: ErrorOptions): URIError; | ||
} | ||
|
||
interface AggregateErrorConstructor { | ||
new ( | ||
errors: Iterable<any>, | ||
message?: string, | ||
options?: ErrorOptions | ||
): AggregateError; | ||
( | ||
errors: Iterable<any>, | ||
message?: string, | ||
options?: ErrorOptions | ||
): AggregateError; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 37 additions & 4 deletions
41
tests/baselines/reference/errorCause(target=es2021).errors.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,41 @@ | ||
tests/cases/compiler/errorCause.ts(1,18): error TS2554: Expected 0-1 arguments, but got 2. | ||
tests/cases/compiler/errorCause.ts(1,28): error TS2554: Expected 0-1 arguments, but got 2. | ||
tests/cases/compiler/errorCause.ts(2,5): error TS2550: Property 'cause' does not exist on type 'Error'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2022' or later. | ||
tests/cases/compiler/errorCause.ts(4,22): error TS2554: Expected 0-1 arguments, but got 2. | ||
tests/cases/compiler/errorCause.ts(5,23): error TS2554: Expected 0-1 arguments, but got 2. | ||
tests/cases/compiler/errorCause.ts(6,27): error TS2554: Expected 0-1 arguments, but got 2. | ||
tests/cases/compiler/errorCause.ts(7,24): error TS2554: Expected 0-1 arguments, but got 2. | ||
tests/cases/compiler/errorCause.ts(8,22): error TS2554: Expected 0-1 arguments, but got 2. | ||
tests/cases/compiler/errorCause.ts(9,21): error TS2554: Expected 0-1 arguments, but got 2. | ||
tests/cases/compiler/errorCause.ts(10,31): error TS2554: Expected 1-2 arguments, but got 3. | ||
|
||
|
||
==== tests/cases/compiler/errorCause.ts (1 errors) ==== | ||
new Error("foo", { cause: new Error("bar") }); | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
==== tests/cases/compiler/errorCause.ts (9 errors) ==== | ||
let err = new Error("foo", { cause: new Error("bar") }); | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2554: Expected 0-1 arguments, but got 2. | ||
err.cause; | ||
~~~~~ | ||
!!! error TS2550: Property 'cause' does not exist on type 'Error'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2022' or later. | ||
|
||
new EvalError("foo", { cause: new Error("bar") }); | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2554: Expected 0-1 arguments, but got 2. | ||
new RangeError("foo", { cause: new Error("bar") }); | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2554: Expected 0-1 arguments, but got 2. | ||
new ReferenceError("foo", { cause: new Error("bar") }); | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2554: Expected 0-1 arguments, but got 2. | ||
new SyntaxError("foo", { cause: new Error("bar") }); | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2554: Expected 0-1 arguments, but got 2. | ||
new TypeError("foo", { cause: new Error("bar") }); | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2554: Expected 0-1 arguments, but got 2. | ||
new URIError("foo", { cause: new Error("bar") }); | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2554: Expected 0-1 arguments, but got 2. | ||
new AggregateError([], "foo", { cause: new Error("bar") }); | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2554: Expected 1-2 arguments, but got 3. | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,23 @@ | ||
//// [errorCause.ts] | ||
new Error("foo", { cause: new Error("bar") }); | ||
let err = new Error("foo", { cause: new Error("bar") }); | ||
err.cause; | ||
|
||
new EvalError("foo", { cause: new Error("bar") }); | ||
new RangeError("foo", { cause: new Error("bar") }); | ||
new ReferenceError("foo", { cause: new Error("bar") }); | ||
new SyntaxError("foo", { cause: new Error("bar") }); | ||
new TypeError("foo", { cause: new Error("bar") }); | ||
new URIError("foo", { cause: new Error("bar") }); | ||
new AggregateError([], "foo", { cause: new Error("bar") }); | ||
|
||
|
||
//// [errorCause.js] | ||
new Error("foo", { cause: new Error("bar") }); | ||
let err = new Error("foo", { cause: new Error("bar") }); | ||
err.cause; | ||
new EvalError("foo", { cause: new Error("bar") }); | ||
new RangeError("foo", { cause: new Error("bar") }); | ||
new ReferenceError("foo", { cause: new Error("bar") }); | ||
new SyntaxError("foo", { cause: new Error("bar") }); | ||
new TypeError("foo", { cause: new Error("bar") }); | ||
new URIError("foo", { cause: new Error("bar") }); | ||
new AggregateError([], "foo", { cause: new Error("bar") }); |
43 changes: 41 additions & 2 deletions
43
tests/baselines/reference/errorCause(target=es2021).symbols
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,45 @@ | ||
=== tests/cases/compiler/errorCause.ts === | ||
new Error("foo", { cause: new Error("bar") }); | ||
let err = new Error("foo", { cause: new Error("bar") }); | ||
>err : Symbol(err, Decl(errorCause.ts, 0, 3)) | ||
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
>cause : Symbol(cause, Decl(errorCause.ts, 0, 18)) | ||
>cause : Symbol(cause, Decl(errorCause.ts, 0, 28)) | ||
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
|
||
err.cause; | ||
>err : Symbol(err, Decl(errorCause.ts, 0, 3)) | ||
|
||
new EvalError("foo", { cause: new Error("bar") }); | ||
>EvalError : Symbol(EvalError, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
>cause : Symbol(cause, Decl(errorCause.ts, 3, 22)) | ||
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
|
||
new RangeError("foo", { cause: new Error("bar") }); | ||
>RangeError : Symbol(RangeError, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
>cause : Symbol(cause, Decl(errorCause.ts, 4, 23)) | ||
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
|
||
new ReferenceError("foo", { cause: new Error("bar") }); | ||
>ReferenceError : Symbol(ReferenceError, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
>cause : Symbol(cause, Decl(errorCause.ts, 5, 27)) | ||
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
|
||
new SyntaxError("foo", { cause: new Error("bar") }); | ||
>SyntaxError : Symbol(SyntaxError, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
>cause : Symbol(cause, Decl(errorCause.ts, 6, 24)) | ||
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
|
||
new TypeError("foo", { cause: new Error("bar") }); | ||
>TypeError : Symbol(TypeError, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
>cause : Symbol(cause, Decl(errorCause.ts, 7, 22)) | ||
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
|
||
new URIError("foo", { cause: new Error("bar") }); | ||
>URIError : Symbol(URIError, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
>cause : Symbol(cause, Decl(errorCause.ts, 8, 21)) | ||
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
|
||
new AggregateError([], "foo", { cause: new Error("bar") }); | ||
>AggregateError : Symbol(AggregateError, Decl(lib.es2021.promise.d.ts, --, --), Decl(lib.es2021.promise.d.ts, --, --)) | ||
>cause : Symbol(cause, Decl(errorCause.ts, 9, 31)) | ||
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,23 @@ | ||
//// [errorCause.ts] | ||
new Error("foo", { cause: new Error("bar") }); | ||
let err = new Error("foo", { cause: new Error("bar") }); | ||
err.cause; | ||
|
||
new EvalError("foo", { cause: new Error("bar") }); | ||
new RangeError("foo", { cause: new Error("bar") }); | ||
new ReferenceError("foo", { cause: new Error("bar") }); | ||
new SyntaxError("foo", { cause: new Error("bar") }); | ||
new TypeError("foo", { cause: new Error("bar") }); | ||
new URIError("foo", { cause: new Error("bar") }); | ||
new AggregateError([], "foo", { cause: new Error("bar") }); | ||
|
||
|
||
//// [errorCause.js] | ||
new Error("foo", { cause: new Error("bar") }); | ||
let err = new Error("foo", { cause: new Error("bar") }); | ||
err.cause; | ||
new EvalError("foo", { cause: new Error("bar") }); | ||
new RangeError("foo", { cause: new Error("bar") }); | ||
new ReferenceError("foo", { cause: new Error("bar") }); | ||
new SyntaxError("foo", { cause: new Error("bar") }); | ||
new TypeError("foo", { cause: new Error("bar") }); | ||
new URIError("foo", { cause: new Error("bar") }); | ||
new AggregateError([], "foo", { cause: new Error("bar") }); |
49 changes: 45 additions & 4 deletions
49
tests/baselines/reference/errorCause(target=es2022).symbols
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,47 @@ | ||
=== tests/cases/compiler/errorCause.ts === | ||
new Error("foo", { cause: new Error("bar") }); | ||
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
>cause : Symbol(cause, Decl(errorCause.ts, 0, 18)) | ||
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
let err = new Error("foo", { cause: new Error("bar") }); | ||
>err : Symbol(err, Decl(errorCause.ts, 0, 3)) | ||
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2022.error.d.ts, --, --)) | ||
>cause : Symbol(cause, Decl(errorCause.ts, 0, 28)) | ||
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2022.error.d.ts, --, --)) | ||
|
||
err.cause; | ||
>err.cause : Symbol(Error.cause, Decl(lib.es2022.error.d.ts, --, --)) | ||
>err : Symbol(err, Decl(errorCause.ts, 0, 3)) | ||
>cause : Symbol(Error.cause, Decl(lib.es2022.error.d.ts, --, --)) | ||
|
||
new EvalError("foo", { cause: new Error("bar") }); | ||
>EvalError : Symbol(EvalError, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
>cause : Symbol(cause, Decl(errorCause.ts, 3, 22)) | ||
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2022.error.d.ts, --, --)) | ||
|
||
new RangeError("foo", { cause: new Error("bar") }); | ||
>RangeError : Symbol(RangeError, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
>cause : Symbol(cause, Decl(errorCause.ts, 4, 23)) | ||
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2022.error.d.ts, --, --)) | ||
|
||
new ReferenceError("foo", { cause: new Error("bar") }); | ||
>ReferenceError : Symbol(ReferenceError, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
>cause : Symbol(cause, Decl(errorCause.ts, 5, 27)) | ||
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2022.error.d.ts, --, --)) | ||
|
||
new SyntaxError("foo", { cause: new Error("bar") }); | ||
>SyntaxError : Symbol(SyntaxError, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
>cause : Symbol(cause, Decl(errorCause.ts, 6, 24)) | ||
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2022.error.d.ts, --, --)) | ||
|
||
new TypeError("foo", { cause: new Error("bar") }); | ||
>TypeError : Symbol(TypeError, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
>cause : Symbol(cause, Decl(errorCause.ts, 7, 22)) | ||
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2022.error.d.ts, --, --)) | ||
|
||
new URIError("foo", { cause: new Error("bar") }); | ||
>URIError : Symbol(URIError, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
>cause : Symbol(cause, Decl(errorCause.ts, 8, 21)) | ||
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2022.error.d.ts, --, --)) | ||
|
||
new AggregateError([], "foo", { cause: new Error("bar") }); | ||
>AggregateError : Symbol(AggregateError, Decl(lib.es2021.promise.d.ts, --, --), Decl(lib.es2021.promise.d.ts, --, --)) | ||
>cause : Symbol(cause, Decl(errorCause.ts, 9, 31)) | ||
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2022.error.d.ts, --, --)) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cause
should be typed asunknown
(orany
in a less strict way) as the value being thrown in JavaScript can be arbitrary values.