-
Notifications
You must be signed in to change notification settings - Fork 6
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
Conversation
333e87e
to
119cf5d
Compare
119cf5d
to
6b2dba1
Compare
There was a problem hiding this 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?
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)}" |
There was a problem hiding this comment.
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:
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)}" |
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Summary
Fixes: #35
When error decoding fails, forward incoming error.
Details and comments