Permalink
Browse files

Improve connection request logging on errors (Closes #6)

  • Loading branch information...
1 parent e91d293 commit b127fd8ad2fb7987756c08539196c86ef81e9e45 Denis Odorcic committed Feb 10, 2012
Showing with 14 additions and 1 deletion.
  1. +8 −1 lib/active_utils/common/connection.rb
  2. +6 −0 test/unit/connection_test.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"
@@ -130,10 +131,12 @@ def retry_exceptions
rescue RetriableConnectionError => e
retries -= 1
retry unless retries.zero?
+ error(e.message, tag)
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 +162,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
@@ -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
@@ -103,6 +107,7 @@ def test_failure_then_success_with_recoverable_exception
end
def test_failure_limit_reached
+ @connection.logger.expects(:error).once
Net::HTTP.any_instance.expects(:post).times(ActiveMerchant::Connection::MAX_RETRIES).raises(Errno::ECONNREFUSED)
assert_raises(ActiveMerchant::ConnectionError) do
@@ -133,6 +138,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 b127fd8

Please sign in to comment.