Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor errorFromResponse #246

Merged
merged 4 commits into from
Sep 12, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 }),
Comment on lines +364 to +372
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change looks correct to me but without a test validating your change I'd rather just leave it as is. I don't think the ternaries here are so complex that it necessitates cleanup.

My preference is to revert this, but if you insist then I'd require a test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @trevor-scheer the codes are already tested in those two cases:

I am just adding an extra assertion in this third test case (throws an ApolloError when the response status is 500), just to make sure the property is not being added:

await expect(result).rejects.not.toHaveProperty('extensions.code');

response: {
url: response.url,
status: response.status,
Expand Down