Skip to content

Commit 5f08df9

Browse files
fix(apollo): fix errors formatting, mapping from http errors
1 parent 7b01710 commit 5f08df9

File tree

8 files changed

+34
-20
lines changed

8 files changed

+34
-20
lines changed

packages/apollo/lib/drivers/apollo-base.driver.ts

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { ApolloServer, type BaseContext } from '@apollo/server';
22
import { ApolloServerPluginLandingPageGraphQLPlayground } from '@apollo/server-plugin-landing-page-graphql-playground';
3-
import { ApolloServerErrorCode } from '@apollo/server/errors';
3+
import {
4+
ApolloServerErrorCode,
5+
unwrapResolverError,
6+
} from '@apollo/server/errors';
47
import { expressMiddleware } from '@apollo/server/express4';
58
import { ApolloServerPluginLandingPageDisabled } from '@apollo/server/plugin/disabled';
69
import { ApolloServerPluginDrainHttpServer } from '@apollo/server/plugin/drainHttpServer';
@@ -191,9 +194,12 @@ export abstract class ApolloBaseDriver<
191194
if (options.formatError) {
192195
const origFormatError = options.formatError;
193196
const transformHttpErrorFn = this.createTransformHttpErrorFn();
194-
(options as ApolloDriverConfig).formatError = (err) => {
195-
err = transformHttpErrorFn(err) as GraphQLError;
196-
return origFormatError(err);
197+
(options as ApolloDriverConfig).formatError = (
198+
formattedError: GraphQLFormattedError,
199+
err: any,
200+
) => {
201+
err = transformHttpErrorFn(formattedError, err) as GraphQLError;
202+
return origFormatError(formattedError, err);
197203
};
198204
} else {
199205
(options as ApolloDriverConfig).formatError =
@@ -202,33 +208,41 @@ export abstract class ApolloBaseDriver<
202208
}
203209

204210
private createTransformHttpErrorFn() {
205-
return (originalError: any): GraphQLFormattedError => {
206-
const exceptionRef = originalError?.extensions?.exception;
211+
return (
212+
formattedError: GraphQLFormattedError,
213+
originalError: any,
214+
): GraphQLFormattedError => {
215+
const exceptionRef = unwrapResolverError(originalError) as any;
207216
const isHttpException =
208217
exceptionRef?.response?.statusCode && exceptionRef?.status;
218+
209219
if (!isHttpException) {
210-
return originalError as GraphQLFormattedError;
220+
return formattedError;
211221
}
222+
212223
let error: GraphQLError;
213224

214225
const httpStatus = exceptionRef?.status;
215226
if (httpStatus in apolloPredefinedExceptions) {
216227
error = new GraphQLError(exceptionRef?.message, {
228+
path: formattedError.path,
217229
extensions: {
230+
...formattedError.extensions,
218231
code: apolloPredefinedExceptions[httpStatus],
219232
},
220233
});
221234
} else {
222235
error = new GraphQLError(exceptionRef.message, {
236+
path: formattedError.path,
223237
extensions: {
238+
...formattedError.extensions,
224239
code: ApolloServerErrorCode.INTERNAL_SERVER_ERROR,
225240
status: httpStatus,
226241
},
227242
});
228243
}
229244

230-
error.stack = exceptionRef?.stacktrace;
231-
error.extensions['response'] = exceptionRef?.response;
245+
(error as any).locations = formattedError.locations;
232246
return error;
233247
};
234248
}

packages/apollo/tests/code-first-federation/app.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { UserModule } from './user/user.module';
1515
RecipeModule,
1616
GraphQLModule.forRoot<ApolloDriverConfig>({
1717
driver: ApolloFederationDriver,
18-
debug: false,
18+
includeStacktraceInErrorResponses: false,
1919
autoSchemaFile: true,
2020
buildSchemaOptions: {
2121
orphanedTypes: [User],

packages/apollo/tests/code-first-federation/caching.module.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1+
import responseCachePlugin from '@apollo/server-plugin-response-cache';
2+
import { ApolloServerPluginInlineTraceDisabled } from '@apollo/server/plugin/disabled';
3+
import { Module } from '@nestjs/common';
4+
import { GraphQLModule } from '@nestjs/graphql';
15
import {
26
DirectiveLocation,
37
GraphQLBoolean,
48
GraphQLDirective,
59
GraphQLEnumType,
610
GraphQLInt,
711
} from 'graphql';
8-
import responseCachePlugin from '@apollo/server-plugin-response-cache';
9-
import { ApolloServerPluginInlineTraceDisabled } from '@apollo/server/plugin/disabled';
10-
import { Module } from '@nestjs/common';
11-
import { GraphQLModule } from '@nestjs/graphql';
1212
import { ApolloFederationDriverConfig } from '../../lib';
1313
import { ApolloFederationDriver } from '../../lib/drivers';
1414
import { PostModule } from './post/post.module';
@@ -23,7 +23,7 @@ import { UserModule } from './user/user.module';
2323
RecipeModule,
2424
GraphQLModule.forRoot<ApolloFederationDriverConfig>({
2525
driver: ApolloFederationDriver,
26-
debug: false,
26+
includeStacktraceInErrorResponses: false,
2727
autoSchemaFile: true,
2828
buildSchemaOptions: {
2929
orphanedTypes: [User],

packages/apollo/tests/code-first/app.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { RecipesModule } from './recipes/recipes.module';
1111
DirectionsModule,
1212
GraphQLModule.forRoot<ApolloDriverConfig>({
1313
driver: ApolloDriver,
14-
debug: false,
14+
includeStacktraceInErrorResponses: false,
1515
installSubscriptionHandlers: true,
1616
autoSchemaFile: true,
1717
}),

packages/apollo/tests/e2e/guards-filters.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describe('GraphQL - Guards', () => {
2929
message: 'Unauthorized error',
3030
locations: [{ line: 2, column: 3 }],
3131
path: ['recipe'],
32-
extensions: { code: 'INTERNAL_SERVER_ERROR' },
32+
extensions: { code: 'UNAUTHENTICATED' },
3333
},
3434
],
3535
data: null,

packages/apollo/tests/e2e/pipes.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ describe('GraphQL - Pipes', () => {
3232
message: 'Bad Request Exception',
3333
locations: [{ line: 2, column: 3 }],
3434
path: ['addRecipe'],
35-
extensions: { code: 'INTERNAL_SERVER_ERROR' },
35+
extensions: { code: 'BAD_REQUEST' },
3636
},
3737
],
3838
});

packages/apollo/tests/graphql-federation/gateway/gateway.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { supergraphSdl } from './supergraph-sdl';
88
GraphQLModule.forRoot({
99
driver: ApolloGatewayDriver,
1010
gateway: {
11-
debug: false,
11+
includeStacktraceInErrorResponses: false,
1212
supergraphSdl,
1313
},
1414
}),

packages/apollo/tests/subscriptions/app/app.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export class AppModule {
2020
NotificationModule,
2121
GraphQLModule.forRoot({
2222
driver: ApolloDriver,
23-
debug: false,
23+
includeStacktraceInErrorResponses: false,
2424
context: options?.context,
2525
autoSchemaFile: true,
2626
subscriptions: options?.subscriptions,

0 commit comments

Comments
 (0)