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



60
61
62
# File 'lib/active_record/connection_adapters/makara_abstract_adapter.rb', line 60

def connection_matchers
  CONNECTION_MATCHERS
end

#connection_message?(message) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
68
69
70
71
72
73
74
# File 'lib/active_record/connection_adapters/makara_abstract_adapter.rb', line 65

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)


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/active_record/connection_adapters/makara_abstract_adapter.rb', line 77

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



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/active_record/connection_adapters/makara_abstract_adapter.rb', line 35

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



55
56
57
# File 'lib/active_record/connection_adapters/makara_abstract_adapter.rb', line 55

def harsh_errors
  HARSH_ERRORS
end