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



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

def connection_matchers
  CONNECTION_MATCHERS
end

#connection_message?(message) ⇒ Boolean

Returns:

  • (Boolean)


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

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)


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
103
# File 'lib/active_record/connection_adapters/makara_abstract_adapter.rb', line 78

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



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

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



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

def harsh_errors
  HARSH_ERRORS
end