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

Improved error handling for http requests #26

Merged
merged 4 commits into from
Feb 23, 2021

Conversation

britzl
Copy link
Collaborator

@britzl britzl commented Feb 19, 2021

Fixes #23

log(result.response)
result.response = json.decode(result.response)
log(result.response)
local ok, decoded = pcall(json.decode, result.response)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Always try to decode the response (but catch errors)

log(result.response)
local ok, decoded = pcall(json.decode, result.response)
if not ok then
result.response = { error = true, message = "Unable to decode response" }
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The response was not JSON (or it was malformed). Is it safe to assume that it is an error in this case?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, it's safe to assume. If there's a bug which will fail the request it could cause a load balancer to return a HTML error response.

if not ok then
result.response = { error = true, message = "Unable to decode response" }
elseif result.status < 200 or result.status > 299 then
result.response = { error = decoded.error or true, message = decoded.message, code = decoded.code }
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Anything outside the 200-299 range is treated as an error

@britzl britzl requested a review from novabyte February 19, 2021 14:00
@novabyte
Copy link
Member

Looks good to me @britzl Thanks 🙏

@britzl britzl merged commit 816b704 into master Feb 23, 2021
@britzl britzl deleted the dev-improve-failed-http-requests branch February 23, 2021 17:53
@britzl
Copy link
Collaborator Author

britzl commented Feb 23, 2021

Thanks. Want me to create a new release @novabyte ?

@novabyte
Copy link
Member

That would be great @britzl 🙏

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.

Authentication errors raise exceptions
2 participants