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