Skip to content
Browse files

Improve connection request logging on errors (Closes #6)

  • Loading branch information...
1 parent e91d293 commit a6d77e545eb7ce3f7cb6aaa0cde4cc9df482543b Denis Odorcic committed Feb 10, 2012
Showing with 12 additions and 1 deletion.
  1. +7 −1 lib/active_utils/common/connection.rb
  2. +5 −0 test/unit/connection_test.rb
View
8 lib/active_utils/common/connection.rb
@@ -72,6 +72,7 @@ def request(method, body, headers = {})
rescue Errno::ECONNREFUSED => e
raise RetriableConnectionError, "The remote server refused the connection"
rescue OpenSSL::X509::CertificateError => e
+ error(e.message, tag)
raise ClientCertificateError, "The remote server did not accept the provided SSL certificate"
rescue Timeout::Error, Errno::ETIMEDOUT => e
raise ConnectionError, "The connection to the remote server timed out"
@@ -131,9 +132,10 @@ def retry_exceptions
retries -= 1
retry unless retries.zero?
raise ConnectionError, e.message
- rescue ConnectionError
+ rescue ConnectionError => e
retries -= 1
retry if retry_safe && !retries.zero?
+ error(e.message, tag)
raise
end
end
@@ -159,6 +161,10 @@ def info(message, tag = nil)
log(:info, message, tag)
end
+ def error(message, tag = nil)
+ log(:error, message, tag)
+ end
+
def log(level, message, tag)
message = "[#{tag}] #{message}" if tag
logger.send(level, message) if logger
View
5 test/unit/connection_test.rb
@@ -7,6 +7,7 @@ def setup
@endpoint = 'https://example.com/tx.php'
@connection = ActiveMerchant::Connection.new(@endpoint)
+ @connection.logger = stub(:info => nil, :debug => nil, :error => nil)
end
def test_connection_endpoint_parses_string_to_uri
@@ -26,6 +27,7 @@ def test_connection_endpoint_raises_uri_error
end
def test_successful_get_request
+ @connection.logger.expects(:info).twice
Net::HTTP.any_instance.expects(:get).with('/tx.php', {}).returns(@ok)
response = @connection.request(:get, nil, {})
assert_equal 'success', response.body
@@ -87,6 +89,7 @@ def test_override_verify_peer
end
def test_unrecoverable_exception
+ @connection.logger.expects(:error).once
Net::HTTP.any_instance.expects(:post).raises(EOFError)
assert_raises(ActiveMerchant::ConnectionError) do
@@ -95,6 +98,7 @@ def test_unrecoverable_exception
end
def test_failure_then_success_with_recoverable_exception
+ @connection.logger.expects(:error).never
Net::HTTP.any_instance.expects(:post).times(2).raises(Errno::ECONNREFUSED).then.returns(@ok)
assert_nothing_raised do
@@ -133,6 +137,7 @@ def test_mixture_of_failures_with_retry_safe_enabled
end
def test_failure_with_ssl_certificate
+ @connection.logger.expects(:error).once
Net::HTTP.any_instance.expects(:post).raises(OpenSSL::X509::CertificateError)
assert_raises(ActiveMerchant::ClientCertificateError) do

0 comments on commit a6d77e5

Please sign in to comment.
Something went wrong with that request. Please try again.