Skip to content

Commit 5b17d41

Browse files
kommanderIvanGoncharov
authored andcommitted
Inspect non-error types to produce helpful error messages for failing resolvers (#1600)
Inspect non-error types to produce helpful error messages for failing resolvers (even though you should not throw literals, ever)
1 parent 4abedd3 commit 5b17d41

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

src/execution/__tests__/abstract-promise-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ describe('Execute: Handles execution of abstract types with promises', () => {
546546
it('resolveType can be caught', async () => {
547547
const PetType = new GraphQLInterfaceType({
548548
name: 'Pet',
549-
resolveType: () => Promise.reject('We are testing this error'),
549+
resolveType: () => Promise.reject(new Error('We are testing this error')),
550550
fields: {
551551
name: { type: GraphQLString },
552552
},

src/execution/__tests__/executor-test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ describe('Execute: Handles basic execution tasks', () => {
466466
path: ['syncError'],
467467
},
468468
{
469-
message: 'Error getting syncRawError',
469+
message: 'Unexpected error value: "Error getting syncRawError"',
470470
locations: [{ line: 5, column: 9 }],
471471
path: ['syncRawError'],
472472
},
@@ -491,12 +491,12 @@ describe('Execute: Handles basic execution tasks', () => {
491491
path: ['asyncReject'],
492492
},
493493
{
494-
message: 'Error getting asyncRawReject',
494+
message: 'Unexpected error value: "Error getting asyncRawReject"',
495495
locations: [{ line: 10, column: 9 }],
496496
path: ['asyncRawReject'],
497497
},
498498
{
499-
message: '',
499+
message: 'Unexpected error value: undefined',
500500
locations: [{ line: 11, column: 9 }],
501501
path: ['asyncEmptyReject'],
502502
},
@@ -506,7 +506,7 @@ describe('Execute: Handles basic execution tasks', () => {
506506
path: ['asyncError'],
507507
},
508508
{
509-
message: 'Error getting asyncRawError',
509+
message: 'Unexpected error value: "Error getting asyncRawError"',
510510
locations: [{ line: 13, column: 9 }],
511511
path: ['asyncRawError'],
512512
},

src/execution/execute.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,10 @@ export function resolveFieldValueOrError<TSource>(
740740
// Sometimes a non-error is thrown, wrap it as an Error instance to ensure a
741741
// consistent Error interface.
742742
function asErrorInstance(error: mixed): Error {
743-
return error instanceof Error ? error : new Error(error || undefined);
743+
if (error instanceof Error) {
744+
return error;
745+
}
746+
return new Error('Unexpected error value: ' + inspect(error));
744747
}
745748

746749
// This is a small wrapper around completeValue which detects and logs errors

0 commit comments

Comments
 (0)