From 990799a850247adeb21810903410c1730f50b4e7 Mon Sep 17 00:00:00 2001 From: Eike Send Date: Wed, 28 Jun 2023 11:17:24 +0200 Subject: [PATCH] Fix: Logging headers & errors fails when ConnectionFailed is raised (#1512) --- lib/faraday/logging/formatter.rb | 2 ++ spec/faraday/response/logger_spec.rb | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/lib/faraday/logging/formatter.rb b/lib/faraday/logging/formatter.rb index 1a60cef53..f4313a322 100644 --- a/lib/faraday/logging/formatter.rb +++ b/lib/faraday/logging/formatter.rb @@ -56,6 +56,8 @@ def filter(filter_word, filter_replacement) private def dump_headers(headers) + return if headers.nil? + headers.map { |k, v| "#{k}: #{v.inspect}" }.join("\n") end diff --git a/spec/faraday/response/logger_spec.rb b/spec/faraday/response/logger_spec.rb index 9f16adf56..4fd1e11c4 100644 --- a/spec/faraday/response/logger_spec.rb +++ b/spec/faraday/response/logger_spec.rb @@ -25,6 +25,7 @@ stubs.get('/filtered_headers') { [200, { 'Content-Type' => 'text/html' }, 'headers response'] } stubs.get('/filtered_params') { [200, { 'Content-Type' => 'text/html' }, 'params response'] } stubs.get('/filtered_url') { [200, { 'Content-Type' => 'text/html' }, 'url response'] } + stubs.get('/connection_failed') { raise Faraday::ConnectionFailed, 'Failed to open TCP connection' } end end end @@ -216,6 +217,15 @@ def response(_env) end end + context 'when logging headers and errors' do + let(:logger_options) { { headers: true, errors: true } } + + it 'logs error message' do + expect { conn.get '/connection_failed' }.to raise_error(Faraday::ConnectionFailed) + expect(string_io.string).to match(%(Failed to open TCP connection)) + end + end + context 'when using log_level' do let(:logger_options) { { bodies: true, log_level: :debug } }