Skip to content
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

Rename Faraday::Logger::Formatter#error to #exception #1468

Merged
merged 2 commits into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions docs/middleware/response/logger.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ end
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.
Any custom formatter CAN implement the `exception` method,
with one argument which will be passed being the exception (StandardError).
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 @@ -112,8 +113,8 @@ class MyFormatter < Faraday::Logging::Formatter
info('Response') { 'Response Received' }
end

def error(error)
# Build a custom message using `error`
def exception(exc)
# Build a custom message using `exc`
info('Error') { 'Error Raised' }
end
end
Expand Down
10 changes: 5 additions & 5 deletions lib/faraday/logging/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ def response(env)
log_body('response', env[:body]) if env[:body] && log_body?(:response)
end

def error(error)
def exception(exc)
return unless log_errors?

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

log_headers('error', error.response_headers) if error.respond_to?(:response_headers) && log_headers?(:error)
return unless error.respond_to?(:response_body) && error.response_body && log_body?(:error)
log_headers('error', exc.response_headers) if exc.respond_to?(:response_headers) && log_headers?(:error)
return unless exc.respond_to?(:response_body) && exc.response_body && log_body?(:error)

log_body('error', error.response_body)
log_body('error', exc.response_body)
end

def filter(filter_word, filter_replacement)
Expand Down
4 changes: 2 additions & 2 deletions lib/faraday/response/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ def on_complete(env)
@formatter.response(env)
end

def on_error(error)
@formatter.error(error) if @formatter.respond_to?(:error)
def on_error(exc)
@formatter.exception(exc) if @formatter.respond_to?(:exception)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/faraday/response/logger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
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(formatter).to receive(:exception).with(an_instance_of(Faraday::Adapter::Test::Stubs::NotFound))

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