Skip to content
Newer
Older
100644 116 lines (106 sloc) 4.15 KB
8a91ccf @mislav add missing requires to Rescuable and RouteSet [#4415 state:committed]
mislav authored Apr 16, 2010
1 require 'active_support/concern'
e5ab4b0 @jeremy Convert to class_attribute
jeremy authored Jan 31, 2010
2 require 'active_support/core_ext/class/attribute'
148da35 @jeremy Explicit dependency on Proc#bind extension
jeremy authored Mar 28, 2009
3 require 'active_support/core_ext/proc'
bddf0a6 @fxn rescuable.rb needs active_support/core_ext/string/inflections because…
fxn authored Jan 1, 2010
4 require 'active_support/core_ext/string/inflections'
0e5b9d7 @fxn rescuable.rb needs active_support/core_ext/array/extract_options
fxn authored Jan 1, 2010
5 require 'active_support/core_ext/array/extract_options'
f8dbd41 @jeremy Explicit dependency on class_inheritable_accessor
jeremy authored Mar 24, 2009
6
964dfc1 @norbert First draft of ActiveSupport::Rescuable
norbert authored Oct 4, 2008
7 module ActiveSupport
259a7a8 @lifo Add tests for ActiveSupport::Rescuable. Use ActiveSupport::Rescuable …
lifo authored Oct 4, 2008
8 # Rescuable module adds support for easier exception handling.
964dfc1 @norbert First draft of ActiveSupport::Rescuable
norbert authored Oct 4, 2008
9 module Rescuable
7b169ed @josh Extend Callbacks and Rescuable with AS concern
josh authored Oct 13, 2009
10 extend Concern
259a7a8 @lifo Add tests for ActiveSupport::Rescuable. Use ActiveSupport::Rescuable …
lifo authored Oct 4, 2008
11
7b169ed @josh Extend Callbacks and Rescuable with AS concern
josh authored Oct 14, 2009
12 included do
e5ab4b0 @jeremy Convert to class_attribute
jeremy authored Feb 1, 2010
13 class_attribute :rescue_handlers
7b169ed @josh Extend Callbacks and Rescuable with AS concern
josh authored Oct 14, 2009
14 self.rescue_handlers = []
964dfc1 @norbert First draft of ActiveSupport::Rescuable
norbert authored Oct 4, 2008
15 end
16
17 module ClassMethods
259a7a8 @lifo Add tests for ActiveSupport::Rescuable. Use ActiveSupport::Rescuable …
lifo authored Oct 4, 2008
18 # Rescue exceptions raised in controller actions.
19 #
20 # <tt>rescue_from</tt> receives a series of exception classes or class
21 # names, and a trailing <tt>:with</tt> option with the name of a method
22 # or a Proc object to be called to handle them. Alternatively a block can
23 # be given.
24 #
25 # Handlers that take one argument will be called with the exception, so
26 # that the exception can be inspected when dealing with it.
27 #
28 # Handlers are inherited. They are searched from right to left, from
29 # bottom to top, and up the hierarchy. The handler of the first class for
30 # which <tt>exception.is_a?(klass)</tt> holds true is the one invoked, if
31 # any.
32 #
33 # class ApplicationController < ActionController::Base
34 # rescue_from User::NotAuthorized, :with => :deny_access # self defined exception
35 # rescue_from ActiveRecord::RecordInvalid, :with => :show_errors
36 #
37 # rescue_from 'MyAppError::Base' do |exception|
38 # render :xml => exception, :status => 500
39 # end
40 #
41 # protected
42 # def deny_access
43 # ...
44 # end
45 #
46 # def show_errors(exception)
47 # exception.record.new_record? ? ...
48 # end
49 # end
9f9a02a @txus Whitespace and example identation
txus authored Aug 14, 2010
50 #
964dfc1 @norbert First draft of ActiveSupport::Rescuable
norbert authored Oct 4, 2008
51 def rescue_from(*klasses, &block)
52 options = klasses.extract_options!
259a7a8 @lifo Add tests for ActiveSupport::Rescuable. Use ActiveSupport::Rescuable …
lifo authored Oct 4, 2008
53
964dfc1 @norbert First draft of ActiveSupport::Rescuable
norbert authored Oct 4, 2008
54 unless options.has_key?(:with)
55 if block_given?
56 options[:with] = block
57 else
58 raise ArgumentError, "Need a handler. Supply an options hash that has a :with key as the last argument."
59 end
60 end
61
62 klasses.each do |klass|
63 key = if klass.is_a?(Class) && klass <= Exception
64 klass.name
65 elsif klass.is_a?(String)
66 klass
67 else
68 raise ArgumentError, "#{klass} is neither an Exception nor a String"
69 end
70
71 # put the new handler at the end because the list is read in reverse
e5ab4b0 @jeremy Convert to class_attribute
jeremy authored Feb 1, 2010
72 self.rescue_handlers += [[key, options[:with]]]
964dfc1 @norbert First draft of ActiveSupport::Rescuable
norbert authored Oct 4, 2008
73 end
74 end
75 end
76
259a7a8 @lifo Add tests for ActiveSupport::Rescuable. Use ActiveSupport::Rescuable …
lifo authored Oct 4, 2008
77 # Tries to rescue the exception by looking up and calling a registered handler.
964dfc1 @norbert First draft of ActiveSupport::Rescuable
norbert authored Oct 4, 2008
78 def rescue_with_handler(exception)
79 if handler = handler_for_rescue(exception)
80 handler.arity != 0 ? handler.call(exception) : handler.call
259a7a8 @lifo Add tests for ActiveSupport::Rescuable. Use ActiveSupport::Rescuable …
lifo authored Oct 4, 2008
81 true # don't rely on the return value of the handler
964dfc1 @norbert First draft of ActiveSupport::Rescuable
norbert authored Oct 4, 2008
82 end
83 end
84
85 def handler_for_rescue(exception)
259a7a8 @lifo Add tests for ActiveSupport::Rescuable. Use ActiveSupport::Rescuable …
lifo authored Oct 4, 2008
86 # We go from right to left because pairs are pushed onto rescue_handlers
87 # as rescue_from declarations are found.
e5ab4b0 @jeremy Convert to class_attribute
jeremy authored Feb 1, 2010
88 _, rescuer = self.class.rescue_handlers.reverse.detect do |klass_name, handler|
259a7a8 @lifo Add tests for ActiveSupport::Rescuable. Use ActiveSupport::Rescuable …
lifo authored Oct 4, 2008
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.
964dfc1 @norbert First draft of ActiveSupport::Rescuable
norbert authored Oct 4, 2008
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
983dc80 @jeremy Don't shadow local with black arg
jeremy authored Nov 7, 2008
107 case rescuer
964dfc1 @norbert First draft of ActiveSupport::Rescuable
norbert authored Oct 4, 2008
108 when Symbol
983dc80 @jeremy Don't shadow local with black arg
jeremy authored Nov 7, 2008
109 method(rescuer)
964dfc1 @norbert First draft of ActiveSupport::Rescuable
norbert authored Oct 4, 2008
110 when Proc
983dc80 @jeremy Don't shadow local with black arg
jeremy authored Nov 7, 2008
111 rescuer.bind(self)
964dfc1 @norbert First draft of ActiveSupport::Rescuable
norbert authored Oct 4, 2008
112 end
113 end
114 end
115 end
Something went wrong with that request. Please try again.