| Module | ActiveSupport::Rescuable |
| In: |
vendor/rails/activesupport/lib/active_support/rescuable.rb
|
Rescuable module adds support for easier exception handling.
# File vendor/rails/activesupport/lib/active_support/rescuable.rb, line 78
78: def handler_for_rescue(exception)
79: # We go from right to left because pairs are pushed onto rescue_handlers
80: # as rescue_from declarations are found.
81: _, rescuer = Array(rescue_handlers).reverse.detect do |klass_name, handler|
82: # The purpose of allowing strings in rescue_from is to support the
83: # declaration of handler associations for exception classes whose
84: # definition is yet unknown.
85: #
86: # Since this loop needs the constants it would be inconsistent to
87: # assume they should exist at this point. An early raised exception
88: # could trigger some other handler and the array could include
89: # precisely a string whose corresponding constant has not yet been
90: # seen. This is why we are tolerant to unknown constants.
91: #
92: # Note that this tolerance only matters if the exception was given as
93: # a string, otherwise a NameError will be raised by the interpreter
94: # itself when rescue_from CONSTANT is executed.
95: klass = self.class.const_get(klass_name) rescue nil
96: klass ||= klass_name.constantize rescue nil
97: exception.is_a?(klass) if klass
98: end
99:
100: case rescuer
101: when Symbol
102: method(rescuer)
103: when Proc
104: rescuer.bind(self)
105: end
106: end
Tries to rescue the exception by looking up and calling a registered handler.
# File vendor/rails/activesupport/lib/active_support/rescuable.rb, line 71
71: def rescue_with_handler(exception)
72: if handler = handler_for_rescue(exception)
73: handler.arity != 0 ? handler.call(exception) : handler.call
74: true # don't rely on the return value of the handler
75: end
76: end