Closed eikes closed 1 year ago
Thank you for opening this @eikes, I love that you provided a failing test to demonstrate the issue. I'm trying to understand if this is somehow related to this recent change, but I'm not sure there's any connection at this point.
My preference would also be to add a return if headers.nil?
guard to the dump_headers
method 👍
@iMacTia Thank you for the kind and immediate feedback! I made the change and the test is now passing... 😄
Description
I was not able to use faraday logging with the logging options
headers: true, errors: true
when the connection can not be estabilshed and aFaraday::ConnectionFailed
error should be raised, because it raised a different error instead:NoMethodError: undefined method
map' for nil:NilClass`Here is the stacktrace:
The reason this happens is that the
Faraday::Logging::Formatter#exception
method tries to log the headers of the exception, but there aren't any.#log_headers
is still called though, becauseexc.respond_to?(:response_headers) && log_headers?(:error)
is true, as theFaraday::Error
class implements the#response_headers
method. Again, the@response
of theexc
instance isnil
, so when thedump_headers
method is finally called, it is called withnil
. And there it fails because it can't callmap
on nil.There are various ways to solve this, but the simplest would be to check for nil in the
dump_headers
method:You could also catch this in the exception method, but I think catching it in the dump_headers method is more robust: