Skip to content

Commit

Permalink
adjust toError wrapping to be more generic
Browse files Browse the repository at this point in the history
  • Loading branch information
yaacovCR committed Oct 27, 2024
1 parent 45d9e4c commit f6ed24a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/error/__tests__/locatedError-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ describe('locatedError', () => {

expect(error).to.be.instanceOf(GraphQLError);
expect(error.originalError).to.include({
name: 'NonErrorThrown',
thrownValue: testObject,
name: 'WrappedNonErrorValueError',
rawError: testObject,
});
});

Expand Down
2 changes: 1 addition & 1 deletion src/execution/__tests__/abort-signal-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ describe('Execute: Cancellation', () => {
},
errors: [
{
message: 'Unexpected error value: "Custom abort error message"',
message: 'Encountered error: "Custom abort error message"',
path: ['todo'],
locations: [{ line: 3, column: 9 }],
},
Expand Down
8 changes: 4 additions & 4 deletions src/execution/__tests__/executor-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ describe('Execute: Handles basic execution tasks', () => {
path: ['syncError'],
},
{
message: 'Unexpected error value: "Error getting syncRawError"',
message: 'Encountered error: "Error getting syncRawError"',
locations: [{ line: 5, column: 9 }],
path: ['syncRawError'],
},
Expand All @@ -519,12 +519,12 @@ describe('Execute: Handles basic execution tasks', () => {
path: ['asyncReject'],
},
{
message: 'Unexpected error value: "Error getting asyncRawReject"',
message: 'Encountered error: "Error getting asyncRawReject"',
locations: [{ line: 10, column: 9 }],
path: ['asyncRawReject'],
},
{
message: 'Unexpected error value: undefined',
message: 'Encountered error: undefined',
locations: [{ line: 11, column: 9 }],
path: ['asyncEmptyReject'],
},
Expand All @@ -534,7 +534,7 @@ describe('Execute: Handles basic execution tasks', () => {
path: ['asyncError'],
},
{
message: 'Unexpected error value: "Error getting asyncRawError"',
message: 'Encountered error: "Error getting asyncRawError"',
locations: [{ line: 13, column: 9 }],
path: ['asyncRawError'],
},
Expand Down
23 changes: 12 additions & 11 deletions src/jsutils/toError.ts
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;
}
}

0 comments on commit f6ed24a

Please sign in to comment.