Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adjust prefix for toError #4260

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
Loading