Faraday: Logger middleware just crashes

Created on 28 Oct 2013  ·  9Comments  ·  Source: lostisland/faraday

This simple program crashes because when logger is called there no headers and body yet.

require 'faraday'

conn = Faraday.new(url: "http://google.com") {|config| config.response :logger}
conn.get "/"
/Users/synapse/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/faraday-0.8.8/lib/faraday/response/logger.rb:31:in `dump_headers': undefined method `map' for nil:NilClass (NoMethodError)
    from /Users/synapse/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/faraday-0.8.8/lib/faraday/response/logger.rb:25:in `block in on_complete'
    from /Users/synapse/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/logger.rb:376:in `add'
    from /Users/synapse/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/logger.rb:404:in `debug'
    from /Users/synapse/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/faraday-0.8.8/lib/faraday/response/logger.rb:25:in `on_complete'
    from /Users/synapse/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/faraday-0.8.8/lib/faraday/response.rb:9:in `block in call'
    from /Users/synapse/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/faraday-0.8.8/lib/faraday/response.rb:63:in `on_complete'
    from /Users/synapse/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/faraday-0.8.8/lib/faraday/response.rb:8:in `call'
    from /Users/synapse/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/faraday-0.8.8/lib/faraday/response/logger.rb:20:in `call'
    from /Users/synapse/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/faraday-0.8.8/lib/faraday/connection.rb:253:in `run_request'
    from /Users/synapse/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/faraday-0.8.8/lib/faraday/connection.rb:106:in `get'
    from faraday_bug.rb:4:in `<main>'

Most helpful comment

This is expected. None of the middleware can work without HTTP requests being made, i.e. without an adapter.

Faraday.new(url: "http://google.com") do |conn|
  conn.response :logger
  conn.adapter Faraday.default_adapter
end

All 9 comments

This is expected. None of the middleware can work without HTTP requests being made, i.e. without an adapter.

Faraday.new(url: "http://google.com") do |conn|
  conn.response :logger
  conn.adapter Faraday.default_adapter
end

@mislav while this is the expected behavior, the resulting error message could be more useful.

I agree with @sethvargo, the message could be better.

In my case, I went from no parameters to Faraday.new and then added conn.response :logger. I would have expected the adapter to be the default_adapter by default, so I was reasonably confused at the new error. It didn't help that I was debugging another error, so I thought that this might have been another symptom of my original error.

I also ran into this problem. I'd say that the expected behaviour is that Faraday uses the default adapter if one isn't specified.

If you'd rather not do this, an 'adaptor not specified' exception would save a lot of head scratching.

@mislav I was about to say exactly what @bluefuton just said. I mean it is called the "default_adapter", which suggests that it's the default one.

Oh, I was just bitten by the same thing. Cryptic error message for what is likely a common mistake. (I removed the adapter specification because I mistakenly assumed it needn't be explicit.)

@mislav Would you accept a pull request to change the error to something like "No adapter specified"?

Or (but you seem to not want this) to assume the "default adapter" if nothing is explicitly specified?

Oh, hey @pengwynn, noticed you're part of this organization as well. Since Mislav didn't reply above, any thoughts on this? Don't mean to step on anyone's toes, though – if Mislav makes these calls I don't want to bypass him :)

The message should be better. Although, as I said, none of the middleware will actually work without an adapter. So, improving the error message specifically for this middleware leaves other cryptic failures in other middleware.

The proper solution should be that Faraday aborts early when a Connection object doesn't include an adapter in the stack. Please subscribe to one of #47, #121, #170

@mislav Aha, thank you for clarifying!

I think I'll try to make a pull request just to improve the error message from logger – it seems like it could be a fairly simple change that would improve on a common gotcha. Hope that's OK.

I might look into the proper fix also, but it seems more challenging, so I might not have the time or skill to achieve that.

Was this page helpful?
0 / 5 - 0 ratings