Skip to content

Commit

Permalink
Feature: Allows logging of errors that raised in adapter or other mid…
Browse files Browse the repository at this point in the history
…dleware.
  • Loading branch information
epaew authored and olleolleolle committed Nov 14, 2022
1 parent ebdcee2 commit 5d28006
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 1 deletion.
8 changes: 7 additions & 1 deletion docs/middleware/response/logger.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,10 @@ end

### Customize the formatter

You can also provide a custom formatter to control how requests and responses are logged.
You can also provide a custom formatter to control how requests, responses and errors are logged.
Any custom formatter MUST implement the `request` and `response` method, with one argument which
will be passed being the Faraday environment.
Any custom formatter CAN implement the `error` method, with one argument which will be passed being the Faraday error.
If you make your formatter inheriting from `Faraday::Logging::Formatter`,
then the methods `debug`, `info`, `warn`, `error` and `fatal` are automatically delegated to the logger.

Expand All @@ -101,6 +102,11 @@ class MyFormatter < Faraday::Logging::Formatter
# Build a custom message using `env`
info('Response') { 'Response Received' }
end

def error(error)
# Build a custom message using `error`
info('Error') { 'Error Raised' }
end
end

conn = Faraday.new(url: 'http://httpbingo.org/api_key=s3cr3t') do |faraday|
Expand Down
8 changes: 8 additions & 0 deletions lib/faraday/logging/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ def response(env)
log_body('response', env[:body]) if env[:body] && log_body?(:response)
end

def error(error)
error_log = proc { error.full_message }
public_send(log_level, 'error', &error_log)

log_headers('error', error.response_headers) if log_headers?(:error)
log_body('error', error.response_body) if error.response_body && log_body?(:error)
end

def filter(filter_word, filter_replacement)
@filter.push([filter_word, filter_replacement])
end
Expand Down
3 changes: 3 additions & 0 deletions lib/faraday/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ def call(env)
app.call(env).on_complete do |environment|
on_complete(environment) if respond_to?(:on_complete)
end
rescue StandardError => e
on_error(e) if respond_to?(:on_error)
raise
end

def close
Expand Down
4 changes: 4 additions & 0 deletions lib/faraday/response/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ def call(env)
def on_complete(env)
@formatter.response(env)
end

def on_error(error)
@formatter.error(error) if @formatter.respond_to?(:error)
end
end
end
end
Expand Down
18 changes: 18 additions & 0 deletions spec/faraday/middleware_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@ def on_request(env)
end
end

describe '#on_error' do
subject do
Class.new(described_class) do
def on_error(error)
# do nothing
end
end.new(app)
end

it 'is called by #call' do
expect(app).to receive(:call).and_raise(Faraday::ConnectionFailed)
is_expected.to receive(:call).and_call_original
is_expected.to receive(:on_error)

expect { subject.call(double) }.to raise_error(Faraday::ConnectionFailed)
end
end

describe '#close' do
context "with app that doesn't support \#close" do
it 'should issue warning' do
Expand Down
9 changes: 9 additions & 0 deletions spec/faraday/response/logger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@
expect(formatter).to receive(:response).with(an_instance_of(Faraday::Env))
conn.get '/hello'
end

context 'when no route' do
it 'delegates logging to the formatter' do
expect(formatter).to receive(:request).with(an_instance_of(Faraday::Env))
expect(formatter).to receive(:error).with(an_instance_of(Faraday::Adapter::Test::Stubs::NotFound))

expect { conn.get '/noroute' }.to raise_error(Faraday::Adapter::Test::Stubs::NotFound)
end
end
end

context 'with custom formatter' do
Expand Down

0 comments on commit 5d28006

Please sign in to comment.