From f6ed24a289023a0cd1f13cd37e0fa5f0f697aa3f Mon Sep 17 00:00:00 2001 From: Yaacov Rydzinski Date: Sun, 27 Oct 2024 12:14:12 +0200 Subject: [PATCH] adjust toError wrapping to be more generic --- src/error/__tests__/locatedError-test.ts | 4 ++-- src/execution/__tests__/abort-signal-test.ts | 2 +- src/execution/__tests__/executor-test.ts | 8 +++---- src/jsutils/toError.ts | 23 ++++++++++---------- 4 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/error/__tests__/locatedError-test.ts b/src/error/__tests__/locatedError-test.ts index c60e1c5873..2fe67d6ddd 100644 --- a/src/error/__tests__/locatedError-test.ts +++ b/src/error/__tests__/locatedError-test.ts @@ -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, }); }); diff --git a/src/execution/__tests__/abort-signal-test.ts b/src/execution/__tests__/abort-signal-test.ts index 6675b5c54d..2a72732674 100644 --- a/src/execution/__tests__/abort-signal-test.ts +++ b/src/execution/__tests__/abort-signal-test.ts @@ -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 }], }, diff --git a/src/execution/__tests__/executor-test.ts b/src/execution/__tests__/executor-test.ts index 173dcc9483..5dd11154ae 100644 --- a/src/execution/__tests__/executor-test.ts +++ b/src/execution/__tests__/executor-test.ts @@ -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'], }, @@ -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'], }, @@ -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'], }, diff --git a/src/jsutils/toError.ts b/src/jsutils/toError.ts index d4cb42005d..7ab439b7ad 100644 --- a/src/jsutils/toError.ts +++ b/src/jsutils/toError.ts @@ -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; } }