File tree Expand file tree Collapse file tree 2 files changed +34
-1
lines changed Expand file tree Collapse file tree 2 files changed +34
-1
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -18,6 +18,7 @@ import * as path from 'path'
18
18
import { SubscriptionServer } from 'subscriptions-transport-ws'
19
19
20
20
import { Options , Props } from './types'
21
+ import { defaultErrorFormatter } from './defaultErrorFormatter' ;
21
22
22
23
export { PubSub , withFilter } from 'graphql-subscriptions'
23
24
export { Options }
@@ -192,7 +193,7 @@ export class GraphQLServer {
192
193
schema : this . executableSchema ,
193
194
tracing : tracing ( request ) ,
194
195
cacheControl : this . options . cacheControl ,
195
- formatError : this . options . formatError ,
196
+ formatError : this . options . formatError || defaultErrorFormatter ,
196
197
logFunction : this . options . logFunction ,
197
198
rootValue : this . options . rootValue ,
198
199
validationRules : this . options . validationRules ,
You can’t perform that action at this time.
0 commit comments