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

Forward incoming error when decoding fails #36

Merged
merged 6 commits into from
Aug 22, 2024
Merged

Conversation

victor-villar
Copy link
Collaborator

Summary

Fixes: #35

When error decoding fails, forward incoming error.

Details and comments

@CLAassistant
Copy link

CLAassistant commented Aug 12, 2024

CLA assistant check
All committers have signed the CLA.

Copy link
Collaborator

@y4izus y4izus left a comment

Choose a reason for hiding this comment

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

Thank you Víctor! Could you review my suggestion?

Comment on lines 183 to 200
try:
resp = exc.response.json()
detail = resp.get("detail")
# Default message
msg = "Internal error."

if isinstance(detail, str):
msg = detail
elif isinstance(detail, list):
detail_input = detail[0]["input"]
detail_msg = detail[0]["msg"]

if detail_input and detail_msg:
msg = f"Wrong input '{detail_input}'. {detail_msg}"
except:
# If something fails decoding the error
# just show the incoming error
msg = f"Internal error: {str(exc)}"
Copy link
Collaborator

@y4izus y4izus Aug 19, 2024

Choose a reason for hiding this comment

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

After reading this, I think we should avoid to have an except without possible exceptions. I would suggest to do:

Suggested change
try:
resp = exc.response.json()
detail = resp.get("detail")
# Default message
msg = "Internal error."
if isinstance(detail, str):
msg = detail
elif isinstance(detail, list):
detail_input = detail[0]["input"]
detail_msg = detail[0]["msg"]
if detail_input and detail_msg:
msg = f"Wrong input '{detail_input}'. {detail_msg}"
except:
# If something fails decoding the error
# just show the incoming error
msg = f"Internal error: {str(exc)}"
# Default message
msg = "Internal error."
try:
resp = exc.response.json()
detail = resp.get("detail")
if isinstance(detail, str):
msg = detail
elif isinstance(detail, list):
detail_input = detail[0]["input"]
detail_msg = detail[0]["msg"]
if detail_input and detail_msg:
msg = f"Wrong input '{detail_input}'. {detail_msg}"
except (KeyError, TypeError, ValueError) as e:
# If something fails decoding the error
# just show the incoming error
msg = f"Internal error: {str(e)}"

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Actually it is not but I would put a plain Exception there as I do not want anything masking the real error. In any case I had to work on other urgent stuff and leave this as it is. I need to fix some tests, etc. 'll retake it when I have time!

Copy link
Contributor

@jesus-talavera-ibm jesus-talavera-ibm left a comment

Choose a reason for hiding this comment

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

LGTM!

@victor-villar victor-villar merged commit 8d39738 into main Aug 22, 2024
4 checks passed
@victor-villar victor-villar deleted the vv-fix-err-bug branch August 22, 2024 15:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Handle HTTPErrors without a response in json format
4 participants