Class: ActiveRecord::ConnectionAdapters::MakaraAbstractAdapter::ErrorHandler

Inherits:
Makara::ErrorHandler show all
Defined in:
lib/active_record/connection_adapters/makara_abstract_adapter.rb

Constant Summary collapse

HARSH_ERRORS =
[
  'ActiveRecord::RecordNotUnique',
  'ActiveRecord::InvalidForeignKey',
  'Makara::Errors::BlacklistConnection'
].map(&:freeze).freeze
CONNECTION_MATCHERS =
[
  /(closed|lost|no|terminating|terminated)\s?([^\s]+)?\sconnection/,
  /gone away/,
  /connection[^:]+refused/,
  /could not connect/,
  /can\'t connect/,
  /cannot connect/,
  /connection[^:]+closed/,
  /can\'t get socket descriptor/,
  /connection to [a-z0-9.]+:[0-9]+ refused/,
  /timeout expired/,
  /could not translate host name/,
  /timeout waiting for a response/,
  /the database system is (starting|shutting)/
].map(&:freeze).freeze

Instance Method Summary collapse

Instance Method Details

#connection_matchersObject



50
51
52
# File 'lib/active_record/connection_adapters/makara_abstract_adapter.rb', line 50

def connection_matchers
  CONNECTION_MATCHERS
end

#connection_message?(message) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
57
58
59
60
61
62
63
# File 'lib/active_record/connection_adapters/makara_abstract_adapter.rb', line 54

def connection_message?(message)
  message = message.to_s.downcase

  case message
  when *connection_matchers
    true
  else
    false
  end
end

#custom_error_message?(connection, message) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/active_record/connection_adapters/makara_abstract_adapter.rb', line 65

def custom_error_message?(connection, message)
  custom_error_matchers = connection._makara_custom_error_matchers
  return false if custom_error_matchers.empty?

  message = message.to_s

  custom_error_matchers.each do |matcher|
    if matcher.is_a?(String)

      # accept strings that look like "/.../" as a regex
      if matcher =~ /^\/(.+)\/([a-z])?$/

        options = $2 ? (($2.include?('x') ? Regexp::EXTENDED : 0) |
                  ($2.include?('i') ? Regexp::IGNORECASE : 0) |
                  ($2.include?('m') ? Regexp::MULTILINE : 0)) : 0

        matcher = Regexp.new($1, options)
      end
    end

    return true if matcher === message
  end

  false
end

#handle(connection) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/active_record/connection_adapters/makara_abstract_adapter.rb', line 30

def handle(connection)
  yield
rescue Exception => e
  # do it via class name to avoid version-specific constant dependencies
  case e.class.name
  when *harsh_errors
    harshly(e)
  else
    if connection_message?(e) || custom_error_message?(connection, e)
      gracefully(connection, e)
    else
      harshly(e)
    end
  end
end

#harsh_errorsObject



46
47
48
# File 'lib/active_record/connection_adapters/makara_abstract_adapter.rb', line 46

def harsh_errors
  HARSH_ERRORS
end