-
Notifications
You must be signed in to change notification settings - Fork 13
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
Conversation
Treat outside the 200-299 range as errors Parse response Set response error
log(result.response) | ||
result.response = json.decode(result.response) | ||
log(result.response) | ||
local ok, decoded = pcall(json.decode, result.response) |
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.
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" } |
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.
The response was not JSON (or it was malformed). Is it safe to assume that it is an error in this case?
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.
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 } |
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.
Anything outside the 200-299 range is treated as an error
Looks good to me @britzl Thanks 🙏 |
Thanks. Want me to create a new release @novabyte ? |
That would be great @britzl 🙏 |
Fixes #23