Skip to content
Open
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
31 changes: 21 additions & 10 deletions src/error/GraphQLError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,22 @@ export interface GraphQLErrorOptions {
source?: Maybe<Source>;
positions?: Maybe<ReadonlyArray<number>>;
path?: Maybe<ReadonlyArray<string | number>>;
/** @deprecated Prefer `cause` instead */
originalError?: Maybe<Error & { readonly extensions?: unknown }>;
cause?: unknown;
extensions?: Maybe<GraphQLErrorExtensions>;
}

type BackwardsCompatibleArgs =
| [options?: GraphQLErrorOptions]
| [
nodes?: GraphQLErrorOptions['nodes'],
source?: GraphQLErrorOptions['source'],
positions?: GraphQLErrorOptions['positions'],
path?: GraphQLErrorOptions['path'],
originalError?: GraphQLErrorOptions['originalError'],
extensions?: GraphQLErrorOptions['extensions'],
];
nodes?: GraphQLErrorOptions['nodes'],
source?: GraphQLErrorOptions['source'],
positions?: GraphQLErrorOptions['positions'],
path?: GraphQLErrorOptions['path'],
originalError?: GraphQLErrorOptions['originalError'],
extensions?: GraphQLErrorOptions['extensions'],
];

function toNormalizedOptions(
args: BackwardsCompatibleArgs,
Expand Down Expand Up @@ -118,6 +120,7 @@ export class GraphQLError extends Error {

/**
* The original error thrown from a field resolver during execution.
* @deprecated Prefer using {@link Error.cause} instead
*/
readonly originalError: Error | undefined;

Expand All @@ -140,13 +143,21 @@ export class GraphQLError extends Error {
extensions?: Maybe<GraphQLErrorExtensions>,
);
constructor(message: string, ...rawArgs: BackwardsCompatibleArgs) {
const { nodes, source, positions, path, originalError, extensions } =
const { nodes, source, positions, path, originalError, extensions, cause } =
toNormalizedOptions(rawArgs);
super(message);
super(message, { cause: cause ?? originalError });

this.name = 'GraphQLError';
this.path = path ?? undefined;
this.originalError = originalError ?? undefined;
if (originalError) {
this.originalError = originalError;
} else if (cause instanceof Error) {
// If we guide users to migrate to `cause` instead of `originalError`,
// better not to break any downstream usages that still rely on `originalError`
this.originalError = cause;
} else {
this.originalError = undefined;
}

// Compute list of blame nodes.
this.nodes = undefinedIfEmpty(
Expand Down