Skip to content

Commit

Permalink
Refactor errorFromResponse (#246)
Browse files Browse the repository at this point in the history
Make `request` and `url` optional parameters in the `errorFromResponse`
method and clean up the implementation.

Co-authored-by: lotmek <lotfi.meklati@polymtl.ca>
  • Loading branch information
lotmek and lotmek authored Sep 12, 2023
1 parent 59eb969 commit c6ac292
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/tricky-keys-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@apollo/datasource-rest': patch
---

Make `request` and `url` optional parameters in the `errorFromResponse` method and clean up the implementation.
16 changes: 9 additions & 7 deletions src/RESTDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,18 +356,20 @@ export abstract class RESTDataSource {
response,
parsedBody,
}: {
url: URL;
request: RequestOptions;
url?: URL;
request?: RequestOptions;
response: FetcherResponse;
parsedBody: unknown;
}) {
const codeByStatus = new Map<number, string>([
[401, 'UNAUTHENTICATED'],
[403, 'FORBIDDEN'],
]);
const code = codeByStatus.get(response.status);

return new GraphQLError(`${response.status}: ${response.statusText}`, {
extensions: {
...(response.status === 401
? { code: 'UNAUTHENTICATED' }
: response.status === 403
? { code: 'FORBIDDEN' }
: {}),
...(code && { code }),
response: {
url: response.url,
status: response.status,
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/RESTDataSource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1638,6 +1638,7 @@ describe('RESTDataSource', () => {
},
},
});
await expect(result).rejects.not.toHaveProperty('extensions.code');
});

it('puts JSON error responses on the error as an object', async () => {
Expand Down

0 comments on commit c6ac292

Please sign in to comment.