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

Add support for Unauthorized and Forbidden response codes, and handle non-JSON errors #55

Merged
merged 1 commit into from
Jan 16, 2024

Conversation

DougEdey
Copy link
Collaborator

This adds extra HTTP Status handling, for 401 -> UnauthorizedError, and 403 -> ForbiddenError

It also adds better error parsing, sometimes the API will respond with a non-JSON body, and the JSON.parse needs a rescue in that case.

@@ -180,6 +207,8 @@ def initialize(token, token_type: :Token, url: API_PREFIX, debug: false)
conn.use RaiseApiErrorOnNon200
conn.use RaiseFileNotFoundOn404
conn.use RaiseRateLimitOn429
conn.use RaiseForbiddenOn403
conn.use RaiseUnauthorizedOn401
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@technicalpickles I was wondering if it's easier to merge these error handlers into one handler, it'll make it easier to patch for consumers of the gem, and may clean up the stack a bit.

If you're OK with that, I'll make a new PR after this one

Copy link
Owner

Choose a reason for hiding this comment

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

Yeah, that is a good point. A follow up PR would be appreciated 👍🏻

@ag-TJNII
Copy link
Contributor

I switched to this branch after seeing some non-JSON body errors in our logs and it much improved the exception output. 👍 on this.

Comment on lines -43 to +72
# TODO May Need to check error.errors too
message += "\n#{JSON.parse(error)}"
begin
# TODO May Need to check error.errors too
message += "\n#{JSON.parse(error)}"
rescue JSON::ParserError
message += "\n#{error}"
end
Copy link
Owner

Choose a reason for hiding this comment

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

Now that I am looking at it, it makes me wonder if it is even worth trying to parse the error? It has been awhile, but does JSON.parse end up pretty-printing the error?

If it doesn't, or if it's already reasonably formatted, maybe we just get rid of the parsing, and always just show the error

Copy link
Owner

Choose a reason for hiding this comment

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

I started this review long ago, but forgot to hit submit 😓 This is more of a note and a possible improvement, this looks good to get this behavior fixed.

@@ -180,6 +207,8 @@ def initialize(token, token_type: :Token, url: API_PREFIX, debug: false)
conn.use RaiseApiErrorOnNon200
conn.use RaiseFileNotFoundOn404
conn.use RaiseRateLimitOn429
conn.use RaiseForbiddenOn403
conn.use RaiseUnauthorizedOn401
Copy link
Owner

Choose a reason for hiding this comment

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

Yeah, that is a good point. A follow up PR would be appreciated 👍🏻

@technicalpickles technicalpickles merged commit 9ffb147 into technicalpickles:master Jan 16, 2024
@technicalpickles technicalpickles mentioned this pull request Jan 16, 2024
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.

3 participants