-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lib: aggregate errors to avoid error swallowing
Uses `AggregateError` if there are more than one error with the message of the outer error to preserve the current behaviour, or returns the logical OR comparison of the two parameters. PR-URL: #37460 Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Robert Nagy <ronagy@icloud.com>
- Loading branch information
Showing
11 changed files
with
147 additions
and
17 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Flags: --expose-internals | ||
'use strict'; | ||
|
||
require('../common'); | ||
const { aggregateTwoErrors } = require('internal/errors'); | ||
|
||
const originalError = new Error('original'); | ||
const err = new Error('second error'); | ||
|
||
originalError.code = 'ERR0'; | ||
err.code = 'ERR1'; | ||
|
||
throw aggregateTwoErrors(err, originalError); |
This file contains 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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
*error_aggregateTwoErrors.js:* | ||
throw aggregateTwoErrors(err, originalError); | ||
^ | ||
AggregateError: original | ||
at Object.<anonymous> (*test*message*error_aggregateTwoErrors.js:*:*) | ||
at Module._compile (node:internal/modules/cjs/loader:*:*) | ||
at Object.Module._extensions..js (node:internal/modules/cjs/loader:*:*) | ||
at Module.load (node:internal/modules/cjs/loader:*:*) | ||
at Function.Module._load (node:internal/modules/cjs/loader:*:*) | ||
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:*:*) | ||
at node:internal/main/run_main_module:*:* { | ||
code: 'ERR0' | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Flags: --expose-internals | ||
'use strict'; | ||
|
||
require('../common'); | ||
const assert = require('assert'); | ||
const { aggregateTwoErrors } = require('internal/errors'); | ||
|
||
assert.strictEqual(aggregateTwoErrors(null, null), null); | ||
|
||
{ | ||
const err = new Error(); | ||
assert.strictEqual(aggregateTwoErrors(null, err), err); | ||
} | ||
|
||
{ | ||
const err = new Error(); | ||
assert.strictEqual(aggregateTwoErrors(err, null), err); | ||
} | ||
|
||
{ | ||
const err0 = new Error('original'); | ||
const err1 = new Error('second error'); | ||
|
||
err0.code = 'ERR0'; | ||
err1.code = 'ERR1'; | ||
|
||
const chainedError = aggregateTwoErrors(err1, err0); | ||
assert.strictEqual(chainedError.message, err0.message); | ||
assert.strictEqual(chainedError.code, err0.code); | ||
assert.deepStrictEqual(chainedError.errors, [err0, err1]); | ||
} | ||
|
||
{ | ||
const err0 = new Error('original'); | ||
const err1 = new Error('second error'); | ||
const err2 = new Error('third error'); | ||
|
||
err0.code = 'ERR0'; | ||
err1.code = 'ERR1'; | ||
err2.code = 'ERR2'; | ||
|
||
const chainedError = aggregateTwoErrors(err2, aggregateTwoErrors(err1, err0)); | ||
assert.strictEqual(chainedError.message, err0.message); | ||
assert.strictEqual(chainedError.code, err0.code); | ||
assert.deepStrictEqual(chainedError.errors, [err0, err1, err2]); | ||
} | ||
|
||
{ | ||
const err0 = new Error('original'); | ||
const err1 = new Error('second error'); | ||
|
||
err0.code = 'ERR0'; | ||
err1.code = 'ERR1'; | ||
|
||
const chainedError = aggregateTwoErrors(null, aggregateTwoErrors(err1, err0)); | ||
assert.strictEqual(chainedError.message, err0.message); | ||
assert.strictEqual(chainedError.code, err0.code); | ||
assert.deepStrictEqual(chainedError.errors, [err0, err1]); | ||
} |
This file contains 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