Module: ActiveMerchant::NetworkConnectionRetries

Included in:
Connection
Defined in:
lib/active_merchant/network_connection_retries.rb

Constant Summary collapse

DEFAULT_RETRIES =
3
DEFAULT_CONNECTION_ERRORS =
{
  EOFError               => 'The remote server dropped the connection',
  Errno::ECONNRESET      => 'The remote server reset the connection',
  Timeout::Error         => 'The connection to the remote server timed out',
  Errno::ETIMEDOUT       => 'The connection to the remote server timed out',
  SocketError            => 'The connection to the remote server could not be established',
  Errno::EHOSTUNREACH    => 'The connection to the remote server could not be established',
  OpenSSL::SSL::SSLError => 'The SSL connection to the remote server could not be established'
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object


16
17
18
# File 'lib/active_merchant/network_connection_retries.rb', line 16

def self.included(base)
  base.send(:attr_accessor, :retry_safe)
end

.log(logger, level, message, tag = nil) ⇒ Object


37
38
39
40
41
# File 'lib/active_merchant/network_connection_retries.rb', line 37

def self.log(logger, level, message, tag = nil)
  tag ||= self.class.to_s
  message = "[#{tag}] #{message}"
  logger&.send(level, message)
end

Instance Method Details

#retry_exceptions(options = {}) ⇒ Object


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/active_merchant/network_connection_retries.rb', line 20

def retry_exceptions(options = {})
  connection_errors = DEFAULT_CONNECTION_ERRORS.merge(options[:connection_exceptions] || {})

  retry_network_exceptions(options) do
    yield
  rescue Errno::ECONNREFUSED => e
    raise ActiveMerchant::RetriableConnectionError.new('The remote server refused the connection', e)
  rescue OpenSSL::X509::CertificateError => e
    NetworkConnectionRetries.log(options[:logger], :error, e.message, options[:tag])
    raise ActiveMerchant::ClientCertificateError, 'The remote server did not accept the provided SSL certificate'
  rescue Zlib::BufError
    raise ActiveMerchant::InvalidResponseError, 'The remote server replied with an invalid response'
  rescue *connection_errors.keys => e
    raise ActiveMerchant::ConnectionError.new(derived_error_message(connection_errors, e.class), e)
  end
end