-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adjust toError wrapping to be more generic
- Loading branch information
Showing
4 changed files
with
19 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,21 @@ | ||
import { inspect } from './inspect.js'; | ||
|
||
/** | ||
* Sometimes a non-error is thrown, wrap it as an Error instance to ensure a consistent Error interface. | ||
* Sometimes a non-error is provided, either thrown within a resolver or as an rejection/abort reason, | ||
* wrap it as an Error instance to ensure a consistent Error interface. | ||
*/ | ||
export function toError(thrownValue: unknown): Error { | ||
return thrownValue instanceof Error | ||
? thrownValue | ||
: new NonErrorThrown(thrownValue); | ||
export function toError(rawError: unknown): Error { | ||
return rawError instanceof Error | ||
? rawError | ||
: new WrappedNonErrorValueError(rawError); | ||
} | ||
|
||
class NonErrorThrown extends Error { | ||
thrownValue: unknown; | ||
class WrappedNonErrorValueError extends Error { | ||
rawError: unknown; | ||
|
||
constructor(thrownValue: unknown) { | ||
super('Unexpected error value: ' + inspect(thrownValue)); | ||
this.name = 'NonErrorThrown'; | ||
this.thrownValue = thrownValue; | ||
constructor(rawError: unknown) { | ||
super('Encountered error: ' + inspect(rawError)); | ||
this.name = 'WrappedNonErrorValueError'; | ||
this.rawError = rawError; | ||
} | ||
} |