From 6d4f21ab0d9c7c9d69eed818b8655007bc608fd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Turin=CC=83o?= Date: Tue, 10 Mar 2020 16:42:25 +0000 Subject: [PATCH] [RESTDataSource] refactor to allow overridding the Error class MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit for example if we want to create a `NotFoundError` that will be used when receiving 404s. This way we need to override the bare minimum like ``` class MyDataSource extends RESTDataSource { protected errorBaseFromResponseAndMessage(response: Response, message: string): ApolloError { if (response.status === 401) return new NotFoundError(message); return super.errorBaseFromResponseAndMessage(response, message); } } ``` Also adding it in the RemoteGraphQLDataSource since it has the same code as RESTDataSource, for symmetry purposes. Signed-off-by: Eduardo Turiño --- .../apollo-datasource-rest/src/RESTDataSource.ts | 15 +++++++-------- .../src/datasources/RemoteGraphQLDataSource.ts | 15 +++++++-------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/packages/apollo-datasource-rest/src/RESTDataSource.ts b/packages/apollo-datasource-rest/src/RESTDataSource.ts index ce737ee7184..7ecb2299992 100644 --- a/packages/apollo-datasource-rest/src/RESTDataSource.ts +++ b/packages/apollo-datasource-rest/src/RESTDataSource.ts @@ -125,17 +125,16 @@ export abstract class RESTDataSource extends DataSource { } } + protected errorBaseFromResponseAndMessage(response: Response, message: string): ApolloError { + if (response.status === 401) return new AuthenticationError(message); + if (response.status === 403) return new ForbiddenError(message); + return new ApolloError(message); + } + protected async errorFromResponse(response: Response) { const message = `${response.status}: ${response.statusText}`; - let error: ApolloError; - if (response.status === 401) { - error = new AuthenticationError(message); - } else if (response.status === 403) { - error = new ForbiddenError(message); - } else { - error = new ApolloError(message); - } + const error = this.errorBaseFromResponseAndMessage(response, message); const body = await this.parseBody(response); diff --git a/packages/apollo-gateway/src/datasources/RemoteGraphQLDataSource.ts b/packages/apollo-gateway/src/datasources/RemoteGraphQLDataSource.ts index 700e78e2f47..e43fe73c848 100644 --- a/packages/apollo-gateway/src/datasources/RemoteGraphQLDataSource.ts +++ b/packages/apollo-gateway/src/datasources/RemoteGraphQLDataSource.ts @@ -200,17 +200,16 @@ export class RemoteGraphQLDataSource = Reco } } + protected errorBaseFromResponseAndMessage(response: Response, message: string): ApolloError { + if (response.status === 401) return new AuthenticationError(message); + if (response.status === 403) return new ForbiddenError(message); + return new ApolloError(message); + } + public async errorFromResponse(response: Response) { const message = `${response.status}: ${response.statusText}`; - let error: ApolloError; - if (response.status === 401) { - error = new AuthenticationError(message); - } else if (response.status === 403) { - error = new ForbiddenError(message); - } else { - error = new ApolloError(message); - } + const error = this.errorBaseFromResponseAndMessage(response, message); const body = await this.parseBody(response);