Module: Gin::Errorable::ClassMethods
- Defined in:
- lib/gin/errorable.rb
Class Method Summary collapse
-
.extended(obj) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#__setup_errorable ⇒ Object
:nodoc:.
-
#all_errors(&block) ⇒ Object
Run after an error has been raised and optionally handled by an error callback.
-
#error(*err_types, &block) ⇒ Object
Define an error handler for this Controller.
-
#error_handler_for(err) ⇒ Object
Find the appropriate error handler for the given error.
-
#error_handlers ⇒ Object
Hash of error handlers defined by Gin::Controller.error.
-
#inherited(subclass) ⇒ Object
:nodoc:.
Class Method Details
.extended(obj) ⇒ Object
:nodoc:
10 11 12 |
# File 'lib/gin/errorable.rb', line 10 def self.extended obj # :nodoc: obj.__setup_errorable end |
Instance Method Details
#__setup_errorable ⇒ Object
:nodoc:
19 20 21 |
# File 'lib/gin/errorable.rb', line 19 def __setup_errorable # :nodoc: @err_handlers = {} end |
#all_errors(&block) ⇒ Object
Run after an error has been raised and optionally handled by an error callback. The block will get run on all errors and is given the exception instance as an argument. Note: This block will not get run after http status error handlers.
57 58 59 60 |
# File 'lib/gin/errorable.rb', line 57 def all_errors &block return unless block_given? self.error_handlers[:all] = block end |
#error(*err_types, &block) ⇒ Object
Define an error handler for this Controller. Configurable with exceptions or status codes. Omitting the err_types argument acts as a catch-all for non-explicitly handled errors.
error 502, 503, 504 do
# handle unexpected upstream error
end
error do |err|
# catch-all
end
error Timeout::Error do |err|
# something timed out
end
41 42 43 44 45 46 47 48 |
# File 'lib/gin/errorable.rb', line 41 def error *err_types, &block return unless block_given? err_types << nil if err_types.empty? err_types.each do |name| self.error_handlers[name] = block end end |
#error_handler_for(err) ⇒ Object
Find the appropriate error handler for the given error. First looks for handler in the current class, then looks in parent classes if none is found.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/gin/errorable.rb', line 76 def error_handler_for err #:nodoc: handler = case err when Integer error_handlers[err] || error_handlers[nil] when Exception klasses = err.class.ancestors[0...-3] key = klasses.find{|klass| error_handlers[klass] } error_handlers[key] else error_handlers[err] end handler || self.superclass.respond_to?(:error_handler_for) && self.superclass.error_handler_for(err) end |
#error_handlers ⇒ Object
Hash of error handlers defined by Gin::Controller.error.
66 67 68 |
# File 'lib/gin/errorable.rb', line 66 def error_handlers @err_handlers end |
#inherited(subclass) ⇒ Object
:nodoc:
14 15 16 17 |
# File 'lib/gin/errorable.rb', line 14 def inherited subclass # :nodoc: subclass.__setup_errorable super end |