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 gql error handling #221

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

hillairet
Copy link
Member

This part of the code was rather hard to follow and I think error prone although it was thoroughly tested.
This is however a nice cleanup that allows one to easily add more error handling without too much headache, I think. 🙏
It's more lines of code but I believe that they are much more readable.

@hillairet hillairet self-assigned this Feb 21, 2024
@hillairet hillairet requested a review from lishanl as a code owner February 21, 2024 00:17
Copy link
Collaborator

@lishanl lishanl left a comment

Choose a reason for hiding this comment

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

Thanks Ant. It's a lot more readable.

Btw, python 3.9 doesn't support union with | that's what the python 3.9 codevalidation complained about.
add the following to enable future behavior?

from __future__ import annotations

Comment on lines 78 to 81
async def _check_errors_field(self, errors: list | str, jsondata: dict):
has_data_field = 'data' in jsondata
if has_data_field and not self.suppress_errors:
raise ShopifyGQLError(jsondata)
Copy link
Collaborator

Choose a reason for hiding this comment

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

thought/rhetoric question: would this scenario ever happen?

Copy link
Member Author

Choose a reason for hiding this comment

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

That's a nice safety net, just in case. I don't know if it's possible but better to have code to catch it.

Comment on lines +89 to +90
errorlist = '\n'.join([err['message'] for err in jsondata['errors'] if 'message' in err])
raise ValueError(f'GraphQL query is incorrect:\n{errorlist}')
Copy link
Collaborator

Choose a reason for hiding this comment

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

question: check suppress_errors before raising just to keep the original logic? though I don't remember the purpose of suppress_errors. I have never set it to True.

Copy link
Member Author

Choose a reason for hiding this comment

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

I guess sometimes one can get data and errors ... not sure. The point here is not to change functionality so I didn't touch it.

if 'extensions' in err and 'code' in err['extensions']:
error_code = err['extensions']['code']
self._handle_max_cost_exceeded_error_code(error_code=error_code)
await self._handle_throttled_error_code(error_code=error_code, jsondata=jsondata)
Copy link
Collaborator

Choose a reason for hiding this comment

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

thought: it should be safe to assume the throttle error is the only error when it happens

Copy link
Member Author

Choose a reason for hiding this comment

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

Again, I reproduced the same functionalities and avoided making any changes.

@lishanl
Copy link
Collaborator

lishanl commented Feb 21, 2024

yea refactoring should preserve the original functionality / behavior. 👍🏼 Thanks ant

@lishanl lishanl self-requested a review February 21, 2024 17:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants