@@ -2,9 +2,15 @@ import { ApolloServerErrorCode as ApolloCode } from '@apollo/server/errors';
2
2
import { ArgumentsHost , Inject , Injectable } from '@nestjs/common' ;
3
3
// eslint-disable-next-line no-restricted-imports
4
4
import * as Nest from '@nestjs/common' ;
5
+ import {
6
+ GqlContextType as ContextKey ,
7
+ GqlExecutionContext ,
8
+ } from '@nestjs/graphql' ;
5
9
import { isNotFalsy , setHas , setOf , simpleSwitch } from '@seedcompany/common' ;
6
- import { GraphQLError } from 'graphql' ;
7
- import { uniq } from 'lodash' ;
10
+ import * as Edge from 'edgedb' ;
11
+ import * as EdgeDBTags from 'edgedb/dist/errors/tags.js' ;
12
+ import { GraphQLError , GraphQLResolveInfo } from 'graphql' ;
13
+ import { lowerCase , uniq } from 'lodash' ;
8
14
import {
9
15
AbstractClassType ,
10
16
DuplicateException ,
@@ -94,6 +100,20 @@ export class ExceptionNormalizer {
94
100
95
101
if ( ex instanceof ExclusivityViolationError ) {
96
102
ex = DuplicateException . fromDB ( ex , context ) ;
103
+ } else if ( ex instanceof Edge . EdgeDBError ) {
104
+ // Mask actual DB error with a nicer user error message.
105
+ let message = 'Failed' ;
106
+ if ( context && context . getType < ContextKey > ( ) === 'graphql' ) {
107
+ const gqlContext = GqlExecutionContext . create ( context as any ) ;
108
+ const info = gqlContext . getInfo < GraphQLResolveInfo > ( ) ;
109
+ if ( info . operation . operation === 'mutation' ) {
110
+ message += ` to ${ lowerCase ( info . fieldName ) } ` ;
111
+ }
112
+ }
113
+ return {
114
+ message,
115
+ codes : this . errorToCodes ( ex ) ,
116
+ } ;
97
117
}
98
118
99
119
if ( ex instanceof Exception ) {
@@ -180,6 +200,13 @@ export class ExceptionNormalizer {
180
200
if ( type === Nest . HttpException ) {
181
201
return ( ex as Nest . HttpException ) . getStatus ( ) < 500 ? 'Client' : 'Server' ;
182
202
}
203
+ if ( type === Edge . EdgeDBError ) {
204
+ const transient =
205
+ ex instanceof Edge . EdgeDBError &&
206
+ ( ex . hasTag ( EdgeDBTags . SHOULD_RECONNECT ) ||
207
+ ex . hasTag ( EdgeDBTags . SHOULD_RETRY ) ) ;
208
+ return [ ...( transient ? [ 'Transient' ] : [ ] ) , 'Database' , 'Server' ] ;
209
+ }
183
210
184
211
return type . name . replace ( / ( E x c e p t i o n | E r r o r ) $ / , '' ) ;
185
212
}
0 commit comments