Skip to content
Newer
Older
100644 120 lines (110 sloc) 4.29 KB
8a91ccf @mislav add missing requires to Rescuable and RouteSet [#4415 state:committed]
mislav authored
1 require 'active_support/concern'
e5ab4b0 @jeremy Convert to class_attribute
jeremy authored
2 require 'active_support/core_ext/class/attribute'
148da35 @jeremy Explicit dependency on Proc#bind extension
jeremy authored
3 require 'active_support/core_ext/proc'
bddf0a6 @fxn rescuable.rb needs active_support/core_ext/string/inflections because…
fxn authored
4 require 'active_support/core_ext/string/inflections'
0e5b9d7 @fxn rescuable.rb needs active_support/core_ext/array/extract_options
fxn authored
5 require 'active_support/core_ext/array/extract_options'
f8dbd41 @jeremy Explicit dependency on class_inheritable_accessor
jeremy authored
6
964dfc1 @norbert First draft of ActiveSupport::Rescuable
norbert authored
7 module ActiveSupport
259a7a8 @lifo Add tests for ActiveSupport::Rescuable. Use ActiveSupport::Rescuable …
lifo authored
8 # Rescuable module adds support for easier exception handling.
964dfc1 @norbert First draft of ActiveSupport::Rescuable
norbert authored
9 module Rescuable
7b169ed @josh Extend Callbacks and Rescuable with AS concern
josh authored
10 extend Concern
259a7a8 @lifo Add tests for ActiveSupport::Rescuable. Use ActiveSupport::Rescuable …
lifo authored
11
7b169ed @josh Extend Callbacks and Rescuable with AS concern
josh authored
12 included do
e5ab4b0 @jeremy Convert to class_attribute
jeremy authored
13 class_attribute :rescue_handlers
7b169ed @josh Extend Callbacks and Rescuable with AS concern
josh authored
14 self.rescue_handlers = []
964dfc1 @norbert First draft of ActiveSupport::Rescuable
norbert authored
15 end
16
17 module ClassMethods
259a7a8 @lifo Add tests for ActiveSupport::Rescuable. Use ActiveSupport::Rescuable …
lifo authored
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
50 #
964dfc1 @norbert First draft of ActiveSupport::Rescuable
norbert authored
51 def rescue_from(*klasses, &block)
52 options = klasses.extract_options!
259a7a8 @lifo Add tests for ActiveSupport::Rescuable. Use ActiveSupport::Rescuable …
lifo authored
53
964dfc1 @norbert First draft of ActiveSupport::Rescuable
norbert authored
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
72 self.rescue_handlers += [[key, options[:with]]]
964dfc1 @norbert First draft of ActiveSupport::Rescuable
norbert authored
73 end
74 end
75 end
76
259a7a8 @lifo Add tests for ActiveSupport::Rescuable. Use ActiveSupport::Rescuable …
lifo authored
77 # Tries to rescue the exception by looking up and calling a registered handler.
964dfc1 @norbert First draft of ActiveSupport::Rescuable
norbert authored
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
81 true # don't rely on the return value of the handler
964dfc1 @norbert First draft of ActiveSupport::Rescuable
norbert authored
82 end
83 end
84
85 def handler_for_rescue(exception)
259a7a8 @lifo Add tests for ActiveSupport::Rescuable. Use ActiveSupport::Rescuable …
lifo authored
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
88 _, rescuer = self.class.rescue_handlers.reverse.detect do |klass_name, handler|
259a7a8 @lifo Add tests for ActiveSupport::Rescuable. Use ActiveSupport::Rescuable …
lifo authored
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
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
107 case rescuer
964dfc1 @norbert First draft of ActiveSupport::Rescuable
norbert authored
108 when Symbol
983dc80 @jeremy Don't shadow local with black arg
jeremy authored
109 method(rescuer)
964dfc1 @norbert First draft of ActiveSupport::Rescuable
norbert authored
110 when Proc
d1887d3 @drogus Fix tests, when creating rescue handler, we need to check for arity now
drogus authored
111 if rescuer.arity == 0
112 Proc.new { instance_exec(&rescuer) }
113 else
ef864c3 @arunagw removed warning: shadowing outer local variable - exception
arunagw authored
114 Proc.new { |_exception| instance_exec(_exception, &rescuer) }
d1887d3 @drogus Fix tests, when creating rescue handler, we need to check for arity now
drogus authored
115 end
964dfc1 @norbert First draft of ActiveSupport::Rescuable
norbert authored
116 end
117 end
118 end
119 end
Something went wrong with that request. Please try again.