Skip to content

Commit 7424dfa

Browse files
committed
Give a nice message & appropriate codes for EdgeDB errors out the way out
1 parent 0f7665c commit 7424dfa

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

src/core/exception/exception.normalizer.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@ import { ApolloServerErrorCode as ApolloCode } from '@apollo/server/errors';
22
import { ArgumentsHost, Inject, Injectable } from '@nestjs/common';
33
// eslint-disable-next-line no-restricted-imports
44
import * as Nest from '@nestjs/common';
5+
import {
6+
GqlContextType as ContextKey,
7+
GqlExecutionContext,
8+
} from '@nestjs/graphql';
59
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';
814
import {
915
AbstractClassType,
1016
DuplicateException,
@@ -94,6 +100,20 @@ export class ExceptionNormalizer {
94100

95101
if (ex instanceof ExclusivityViolationError) {
96102
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+
};
97117
}
98118

99119
if (ex instanceof Exception) {
@@ -180,6 +200,13 @@ export class ExceptionNormalizer {
180200
if (type === Nest.HttpException) {
181201
return (ex as Nest.HttpException).getStatus() < 500 ? 'Client' : 'Server';
182202
}
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+
}
183210

184211
return type.name.replace(/(Exception|Error)$/, '');
185212
}

0 commit comments

Comments
 (0)