-
Notifications
You must be signed in to change notification settings - Fork 979
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
Raise exception on nil status #1042
Raise exception on nil status #1042
Conversation
This got me curious about the case where the response code is numeric but in an invalid range. (I'm out traveling, so this happens from a phone.) |
That's a good point. A base case here for any invalid status could be useful |
lib/faraday/error.rb
Outdated
@@ -13,7 +13,10 @@ def initialize(exc, response = nil) | |||
super(exc.message) | |||
@wrapped_exception = exc | |||
elsif exc.respond_to?(:each_key) | |||
super("the server responded with status #{exc[:status]}") | |||
nil_status_message = 'the server responded with a nil status' |
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.
Does it make sense to say the server responded with a nil status if the server did not send a status Header at all?
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.
That's a fair point, maybe a more explicit message, something like status couldn't be derived from server 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.
I like it, I'd say move it down into the error itself as well rather than in this base error class
lib/faraday/error.rb
Outdated
@@ -69,6 +72,10 @@ class ConflictError < ClientError | |||
class UnprocessableEntityError < ClientError | |||
end | |||
|
|||
# Raised by Faraday::Response::RaiseError in case of a nil status in response. | |||
class NilStatusError < ClientError |
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.
class NilStatusError < ClientError | |
class NilStatusError < ServerError |
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.
class NilStatusError < ServerError
def initialize(exc = nil, response = nil)
message = 'http status could not be derived from the server response'
super(message, response)
end
end
lib/faraday/response/raise_error.rb
Outdated
@@ -28,6 +28,8 @@ def on_complete(env) | |||
raise Faraday::ConflictError, response_values(env) | |||
when 422 | |||
raise Faraday::UnprocessableEntityError, response_values(env) | |||
when nil | |||
raise Faraday::NilStatusError, response_values(env) |
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.
If you make the suggestions I made above, you'll need to change this line as well
raise Faraday::NilStatusError, nil, response_values(env)
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.
Sounds good 👍 I'll get to this later today
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.
So passing nil
to this actually caused a bunch of issues for me when setting the backtrace. So I've opted to not pass it at all. Let me know if there's a better way around this.
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.
🤦♂ silly me, we're not using Faraday::NilStatusError.new so the backtrace is implicit
This is failing lint on an unrelated file Inspecting 100 files
.........W..........................................................................................
Offenses:
lib/faraday/adapter/em_http_ssl_patch.rb:62:34: W: Lint/SendWithMixinArgument: Use include EmHttpSslPatch instead of send(:include, EmHttpSslPatch).
EventMachine::HttpStubConnection.send(:include, EmHttpSslPatch)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Locally ~/D/p/faraday ❯❯❯ rubocop raise-exception-on-nil-status
Inspecting 100 files
....................................................................................................
100 files inspected, no offenses detected
~/D/p/faraday ❯❯❯ rubocop --require rubocop/formatter/junit_formatter \ raise-exception-on-nil-status
--require rubocop-performance \
--format progress \
--format RuboCop::Formatter::JUnitFormatter \
--out ~/test-results/linting/rubocop.xml
Inspecting 100 files
.........W..........................................................................................
Offenses:
lib/faraday/adapter/em_http_ssl_patch.rb:62:34: W: Lint/SendWithMixinArgument: Use include EmHttpSslPatch instead of send(:include, EmHttpSslPatch).
EventMachine::HttpStubConnection.send(:include, EmHttpSslPatch)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
100 files inspected, 1 offense detected I don't have permissions to retrigger the build, but this passes locally without JUnit, but once I've installed it it fails |
I'd appreciate a separate PR for the linting fix. <3 |
I had implemented it in #1043, I can open a new PR to target it alone |
Cool! I'm happy to roll it into this either @BobbyMcWho but its own PR would be cleaner I guess |
@olleolleolle merged it, @jonnyom you should be able to |
0569535
to
538e2ae
Compare
538e2ae
to
6adf096
Compare
lib/faraday/error.rb
Outdated
@@ -83,6 +83,14 @@ def initialize(exc = 'timeout', response = nil) | |||
end | |||
end | |||
|
|||
# Raised by Faraday::Response::RaiseError in case of a nil status in response. | |||
class NilStatusError < ServerError | |||
def initialize(response = nil) |
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.
I submitted a PR to your fork, this will need two params to handle the stack trace that's sent
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.
I saw that, just lifted what you had and popped it in here, easier than merging things into the fork
@olleolleolle @BobbyMcWho just wanted to give this a bump |
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.
Thanks for adding this! I identified a really minor change, but I think this is ready to go. However, the original PR has an unchecked Documentation item. Did you plan on updating docs/middleware/response/raise_error.md in this PR too? I'm happy to take this on in a new PR, as I'd like to reformat the list of status codes inside the
block.
when nil | ||
raise Faraday::NilStatusError, response: response_values(env) |
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.
- I'd rather have the
nil
check at the bottom, since it's the least likely to occur than any numerical status. Pretty minor issue, so I'm not sure if it's worth the effort :) - I like passing
response
as a named variable, rather than positional. I've added a note to the v2 wishlist (Faraday Wishlist #953) to make this consistent with the other error types.
@technoweenie if you want to refactor it then maybe we should just merge this and you can make that refactor including these changes? (I also wasn't sure where the documentation was, which is why I didn't add it) Thanks! |
Thanks @jonnyom! I'll take care of the docs. |
Description
If a host responds badly to HTTP requests it's possible for the HTTP status to be returned
nil
.It makes sense to raise an exception here that can be rescued from and handled appropriately instead of silently allowing them through.
Fixes #1028
Todos
List any remaining work that needs to be done, i.e: