-
Notifications
You must be signed in to change notification settings - Fork 998
Include HTTP method and URL in Faraday::Error messages for improved exception log transparency #1628
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
Include HTTP method and URL in Faraday::Error messages for improved exception log transparency #1628
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,12 +79,23 @@ def exc_msg_and_response!(exc, response = nil) | |
|
||
# Pulls out potential parent exception and response hash. | ||
def exc_msg_and_response(exc, response = nil) | ||
return [exc, exc.message, response] if exc.respond_to?(:backtrace) | ||
if exc.is_a?(Exception) | ||
[exc, exc.message, response] | ||
elsif exc.is_a?(Hash) | ||
http_status = exc.fetch(:status) | ||
|
||
return [nil, "the server responded with status #{exc[:status]}", exc] \ | ||
if exc.respond_to?(:each_key) | ||
request = exc.fetch(:request, nil) | ||
|
||
[nil, exc.to_s, response] | ||
exception_message = if request.nil? | ||
"the server responded with status #{http_status} - method and url are not available due to include_request: false on Faraday::Response::RaiseError middleware" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can overcome the Rubocop error by using Ruby's line continuation: "the server responded with status #{http_status} - method and url are " \
'not available due to include_request: false on Faraday::Response::RaiseError middleware' There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, I can see why the limitation with the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just went on a bit of a wild goose chase because of this error message because the moneybird gem raises an exception descended from Faraday::Error but passes in a hash without request information. This lead to the "due to include_request: false" text appearing in my logs, even though I don't set that option anywhere. Should the moneybird just not do that, or is there some way to make sure the error actually comes from Faraday::Response::RaiseError before emitting that message? For reference: https://github.com/maartenvanvliet/moneybird/blob/master/lib/moneybird/middleware/error_handling.rb There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not the architect of Faraday and I can see that this change I submitted last month has caused regressions for some people. 😢 I did not assume that anyone was subclassing the Faraday errors or using their own middleware to raise them. If you want to raise subclassed errors, I think you should include the request property in the hash you generate in your Perhaps you don't need your own implementation of this middleware, considering that Faraday ships with the RaiseError middleware. However, this would break your consumers error handling if they are rescuing Moneybird specific exceptions, as those would now become Faraday errors. Maybe @iMacTia can provide a better perspective. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks @nielsbuus. I'll open a ticket in the moneybird gem repo as well and see what their thoughts are. |
||
else | ||
"the server responded with status #{http_status} for #{request.fetch(:method).upcase} #{request.fetch(:url)}" | ||
end | ||
|
||
[nil, exception_message, exc] | ||
else | ||
[nil, exc.to_s, response] | ||
end | ||
end | ||
end | ||
|
||
|
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.
This is perfectly fine, duck typing was OK back in the day, but I think people expect more of this nowadays with the advent of Sorbet and RBS