Skip to content

Commit bd46da6

Browse files
committed
fix(error-handling): If an error contains a code or requestId property, pass it through
1 parent 13c1b2d commit bd46da6

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/defaultErrorFormatter.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { formatError } from 'graphql'
2+
import { GraphQLFormattedError } from 'graphql/error/formatError'
3+
4+
export interface PrismaErrorProps {
5+
code?: number
6+
requestId?: string
7+
}
8+
9+
export function defaultErrorFormatter(
10+
error,
11+
): GraphQLFormattedError & PrismaErrorProps {
12+
const data: GraphQLFormattedError & PrismaErrorProps = formatError(error)
13+
14+
if (
15+
error.originalError &&
16+
error.originalError.result &&
17+
error.originalError.result.errors &&
18+
error.originalError.result.errors.length === 1
19+
) {
20+
const originalError = error.originalError.result.errors[0]
21+
if (originalError.message === error.message) {
22+
if (originalError.code) {
23+
data.code = originalError.code
24+
}
25+
if (originalError.requestId) {
26+
data.requestId = originalError.requestId
27+
}
28+
}
29+
}
30+
31+
return data
32+
}

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import * as path from 'path'
1818
import { SubscriptionServer } from 'subscriptions-transport-ws'
1919

2020
import { Options, Props } from './types'
21+
import { defaultErrorFormatter } from './defaultErrorFormatter';
2122

2223
export { PubSub, withFilter } from 'graphql-subscriptions'
2324
export { Options }
@@ -192,7 +193,7 @@ export class GraphQLServer {
192193
schema: this.executableSchema,
193194
tracing: tracing(request),
194195
cacheControl: this.options.cacheControl,
195-
formatError: this.options.formatError,
196+
formatError: this.options.formatError || defaultErrorFormatter,
196197
logFunction: this.options.logFunction,
197198
rootValue: this.options.rootValue,
198199
validationRules: this.options.validationRules,

0 commit comments

Comments
 (0)