Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Preserve non-error values thrown from resolvers
Fixes #3379
- Loading branch information
1 parent
cce8a85
commit bce5fbc
Showing
2 changed files
with
22 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { inspect } from './inspect'; | ||
|
||
/** | ||
* Sometimes a non-error is thrown, 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); | ||
} | ||
|
||
class NonErrorThrown extends Error { | ||
thrownValue: unknown; | ||
|
||
constructor(thrownValue: unknown) { | ||
super('Unexpected error value: ' + inspect(thrownValue)); | ||
this.name = 'NonErrorThrown'; | ||
this.thrownValue = thrownValue; | ||
} | ||
} |