-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
45 additions
and
45 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Ensure both the prototype and `error.name` are correct, by creating a new | ||
// instance with the right constructor. | ||
// The parent error's type has priority unless: | ||
// - It is Error|AggregateError, which allows wrapping an error message or | ||
// properties without changing its type | ||
// - Its constructor throws | ||
// If both parent and child constructors throw, we default to Error. | ||
// If both parent and child are Error|AggregateError, we privilege | ||
// AggregateError if any is an instance, since the `errors` property should only | ||
// be used on AggregateError. | ||
export const createError = function (parent, child, message) { | ||
if (isSimpleErrorType(parent)) { | ||
return createCauseError(parent, child, message) | ||
} | ||
|
||
try { | ||
return new parent.constructor(message) | ||
} catch { | ||
return createCauseError(parent, child, message) | ||
} | ||
} | ||
|
||
const createCauseError = function (parent, child, message) { | ||
if (isSimpleErrorType(child)) { | ||
return createSimpleErrorType(parent, child, message) | ||
} | ||
|
||
try { | ||
return new child.constructor(message) | ||
} catch { | ||
return createSimpleErrorType(parent, child, message) | ||
} | ||
} | ||
|
||
const isSimpleErrorType = function (error) { | ||
return error.constructor === Error || error.constructor === AggregateError | ||
} | ||
|
||
const createSimpleErrorType = function (parent, child, message) { | ||
return parent.constructor === AggregateError || | ||
child.constructor === AggregateError | ||
? new AggregateError([], message) | ||
: new Error(message) | ||
} |
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