Skip to content

Commit c6ac292

Browse files
lotmeklotmek
andauthored
Refactor errorFromResponse (#246)
Make `request` and `url` optional parameters in the `errorFromResponse` method and clean up the implementation. Co-authored-by: lotmek <[email protected]>
1 parent 59eb969 commit c6ac292

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

.changeset/tricky-keys-press.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@apollo/datasource-rest': patch
3+
---
4+
5+
Make `request` and `url` optional parameters in the `errorFromResponse` method and clean up the implementation.

src/RESTDataSource.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -356,18 +356,20 @@ export abstract class RESTDataSource {
356356
response,
357357
parsedBody,
358358
}: {
359-
url: URL;
360-
request: RequestOptions;
359+
url?: URL;
360+
request?: RequestOptions;
361361
response: FetcherResponse;
362362
parsedBody: unknown;
363363
}) {
364+
const codeByStatus = new Map<number, string>([
365+
[401, 'UNAUTHENTICATED'],
366+
[403, 'FORBIDDEN'],
367+
]);
368+
const code = codeByStatus.get(response.status);
369+
364370
return new GraphQLError(`${response.status}: ${response.statusText}`, {
365371
extensions: {
366-
...(response.status === 401
367-
? { code: 'UNAUTHENTICATED' }
368-
: response.status === 403
369-
? { code: 'FORBIDDEN' }
370-
: {}),
372+
...(code && { code }),
371373
response: {
372374
url: response.url,
373375
status: response.status,

src/__tests__/RESTDataSource.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1638,6 +1638,7 @@ describe('RESTDataSource', () => {
16381638
},
16391639
},
16401640
});
1641+
await expect(result).rejects.not.toHaveProperty('extensions.code');
16411642
});
16421643

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

0 commit comments

Comments
 (0)