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);