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

fix: gracefully handle non json responses #112

Merged
merged 2 commits into from
Jul 23, 2024
Merged

Conversation

drish
Copy link
Collaborator

@drish drish commented Jun 21, 2024

fixes: #111

@drish drish requested a review from bukinoshita June 21, 2024 22:07
# delete calls do not return a body
if resp.text == "" and resp.status_code == 200:
return None

# this is a safety net, if we get here it means the Resend API is having issues
# and most likely the gateway is returning htmls
if "application/json" not in resp.headers["content-type"]:
Copy link
Contributor

Choose a reason for hiding this comment

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

I would suggest to use req.headers.get("content-type", "") to avoid any KeyError

Copy link
Contributor

Choose a reason for hiding this comment

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

we can also take a different approach

try:
     dict_response = resp.json()
except requests.exceptions.JSONDecodeError:
     raise_for_code_and_type(
                code=500,
                message="Failed to parse Resend API response. Please try again.",
                error_type="InternalServerError",
            )

# handle error in case there is a statusCode attr present
# and status != 200
# and status != 200 and response is a json.
if resp.status_code != 200 and resp.json().get("statusCode"):
Copy link
Contributor

@pedroimpulcetto pedroimpulcetto Jun 23, 2024

Choose a reason for hiding this comment

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

we can re-use the resp.json() definition

dict_response = resp.json()
if resp.status_code != 200 and dict_response.get("statusCode"):
raise_for_code_and_type(
                code=dict_response.get("statusCode"),
                message=dict_response.get("message"),
                error_type=dict_response.get("name"),
            )
return cast(T, dict_response)

@pedroimpulcetto
Copy link
Contributor

those comments are only improvements that we can make later.

overall it looks like it is working

@drish
Copy link
Collaborator Author

drish commented Jul 22, 2024

thanks for the suggestions @pedroimpulcetto, for expediency im merging this as is and will push a pr with the improvements separately

"subject": "subject",
"html": "html",
}
with self.assertRaises(ResendError):

Choose a reason for hiding this comment

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

let's assert for the error message too Failed to parse Resend API response. Please try again

Choose a reason for hiding this comment

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

☝️ this is important to avoid a scenario where we are asserting for 500, but the error "inside" the API is not the one we are trying to simulate in the test

@drish drish requested a review from felipevolpone July 23, 2024 21:44
@drish drish merged commit 00ba292 into main Jul 23, 2024
22 checks passed
@drish drish deleted the fix/handle-not-json-responses branch July 23, 2024 23:52
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.

JSONDecodeError on 503 responses
3 participants