apollo-server-errors
Advanced tools
Comparing version 3.2.0 to 3.3.0
import { ASTNode, GraphQLError, GraphQLFormattedError, Source, SourceLocation } from 'graphql'; | ||
declare module 'graphql' { | ||
interface GraphQLErrorExtensions { | ||
exception?: { | ||
code?: string; | ||
stacktrace?: ReadonlyArray<string>; | ||
}; | ||
} | ||
} | ||
export declare class ApolloError extends Error implements GraphQLError { | ||
@@ -10,5 +18,8 @@ extensions: Record<string, any>; | ||
readonly nodes: ReadonlyArray<ASTNode> | undefined; | ||
originalError: Error | null | undefined; | ||
originalError: Error | undefined; | ||
[key: string]: any; | ||
constructor(message: string, code?: string, extensions?: Record<string, any>); | ||
toJSON(): GraphQLFormattedError; | ||
toString(): string; | ||
get [Symbol.toStringTag](): string; | ||
} | ||
@@ -15,0 +26,0 @@ export declare function toApolloError(error: Error & { |
@@ -18,4 +18,16 @@ "use strict"; | ||
} | ||
toJSON() { | ||
return (0, graphql_1.formatError)(toGraphQLError(this)); | ||
} | ||
toString() { | ||
return (0, graphql_1.printError)(toGraphQLError(this)); | ||
} | ||
get [Symbol.toStringTag]() { | ||
return this.name; | ||
} | ||
} | ||
exports.ApolloError = ApolloError; | ||
function toGraphQLError(error) { | ||
return new graphql_1.GraphQLError(error.message, error.nodes, error.source, error.positions, error.path, error.originalError, error.extensions); | ||
} | ||
function enrichError(error, debug = false) { | ||
@@ -87,2 +99,5 @@ var _a, _b, _c; | ||
Object.entries(error).forEach(([key, value]) => { | ||
if (key === 'extensions') { | ||
return; | ||
} | ||
copy[key] = value; | ||
@@ -89,0 +104,0 @@ }); |
{ | ||
"name": "apollo-server-errors", | ||
"version": "3.2.0", | ||
"author": "Apollo <opensource@apollographql.com>", | ||
"version": "3.3.0", | ||
"author": "Apollo <packages@apollographql.com>", | ||
"license": "MIT", | ||
@@ -21,5 +21,5 @@ "repository": { | ||
"peerDependencies": { | ||
"graphql": "^15.3.0" | ||
"graphql": "^15.3.0 || ^16.0.0" | ||
}, | ||
"gitHead": "7861ba22c9e4caf037e7a1cec71191b9b0deb71b" | ||
"gitHead": "e6c3cbbf820b3ffff1e7e98d41b3bfc08f99b9b0" | ||
} |
@@ -7,4 +7,17 @@ import { | ||
SourceLocation, | ||
printError, | ||
formatError, | ||
} from 'graphql'; | ||
declare module 'graphql' { | ||
export interface GraphQLErrorExtensions { | ||
exception?: { | ||
code?: string; | ||
stacktrace?: ReadonlyArray<string>; | ||
}; | ||
} | ||
} | ||
// Note: We'd like to switch to `extends GraphQLError` and look forward to doing so | ||
// as soon as we drop support for `graphql` bellow `v15.7.0`. | ||
export class ApolloError extends Error implements GraphQLError { | ||
@@ -18,3 +31,3 @@ public extensions: Record<string, any>; | ||
readonly nodes: ReadonlyArray<ASTNode> | undefined; | ||
public originalError: Error | null | undefined; | ||
public originalError: Error | undefined; | ||
@@ -45,4 +58,28 @@ [key: string]: any; | ||
} | ||
toJSON(): GraphQLFormattedError { | ||
return formatError(toGraphQLError(this)); | ||
} | ||
override toString(): string { | ||
return printError(toGraphQLError(this)); | ||
} | ||
get [Symbol.toStringTag](): string { | ||
return this.name; | ||
} | ||
} | ||
function toGraphQLError(error: ApolloError): GraphQLError { | ||
return new GraphQLError( | ||
error.message, | ||
error.nodes, | ||
error.source, | ||
error.positions, | ||
error.path, | ||
error.originalError, | ||
error.extensions, | ||
); | ||
} | ||
function enrichError(error: Partial<GraphQLError>, debug: boolean = false) { | ||
@@ -135,6 +172,9 @@ // follows similar structure to https://github.com/graphql/graphql-js/blob/main/src/error/GraphQLError.ts#L127-L176 | ||
Object.entries(error).forEach(([key, value]) => { | ||
if (key === 'extensions') { | ||
return; // extensions are handled bellow | ||
} | ||
copy[key] = value; | ||
}); | ||
// extensions are non enumerable, so copy them directly | ||
// merge extensions instead of just copying them | ||
copy.extensions = { | ||
@@ -141,0 +181,0 @@ ...copy.extensions, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
29663
550