Module: NestedExceptions
- Defined in:
- lib/nested_exceptions.rb,
lib/nested_exceptions/define.rb,
lib/nested_exceptions/version.rb
Overview
Just include this module in your custom exception class
Constant Summary collapse
- GLOBAL =
true
- VERSION =
"1.0.1"
Instance Attribute Summary collapse
-
#cause ⇒ Object
readonly
Returns the value of attribute cause.
Class Method Summary collapse
- .const_missing(name) ⇒ Object
-
.define_standard_exception_classes_in(target_module, add_module = false) ⇒ Object
Define a set of standard exception classes as recommended in exceptionalruby.com/.
Instance Method Summary collapse
- #backtrace ⇒ Object
- #initialize(message = nil, cause = nil) ⇒ Object
- #root_cause ⇒ Object
- #set_backtrace(bt) ⇒ Object
Instance Attribute Details
#cause ⇒ Object (readonly)
Returns the value of attribute cause.
6 7 8 |
# File 'lib/nested_exceptions.rb', line 6 def cause @cause end |
Class Method Details
.const_missing(name) ⇒ Object
4 5 6 7 8 9 10 |
# File 'lib/nested_exceptions/version.rb', line 4 def self.const_missing(name) if name == 'GLOBAL' or name == :GLOBAL NestedExceptions.const_set(name, false) else super end end |
.define_standard_exception_classes_in(target_module, add_module = false) ⇒ Object
Define a set of standard exception classes as recommended in exceptionalruby.com/
I actually recommend that you just define these manually, but this may be a handy shortcut in smaller projects.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/nested_exceptions/define.rb', line 8 def define_standard_exception_classes_in(target_module, add_module = false) definitions = %{ class Error < StandardError; #{ add_module ? 'include NestedExceptions;' : '' } end class UserError < Error; end class LogicError < Error; end class ClientError < LogicError; end class InternalError < LogicError; end class TransientError < Error; end } target_module.module_eval definitions [:Error, :UserError, :LogicError, :ClientError, :InternalError, :TransientError].map do |name| target_module.const_get name end end |
Instance Method Details
#backtrace ⇒ Object
25 26 27 28 29 30 |
# File 'lib/nested_exceptions.rb', line 25 def backtrace prevent_recursion do return @processed_backtrace if defined? @processed_backtrace and @processed_backtrace @processed_backtrace = process_backtrace(super) end end |
#initialize(message = nil, cause = nil) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/nested_exceptions.rb', line 8 def initialize( = nil, cause = nil) # @cause could be defined if this module is included multiple # times in a class heirarchy. @illegal_nesting = defined? @cause @recursing = false if @illegal_nesting warn "WARNING: NestedExceptions is included in the class heirarchy of #{ self.class } more than once." warn "- Ensure if you require 'nested_exceptions/global' or manually add" warn " NestedExceptions to a built-in Ruby exception class, you must do it at" warn " the beginning of the program." else @cause = cause || $! super() end end |
#root_cause ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/nested_exceptions.rb', line 39 def root_cause rc = e = self while e rc = e break unless e.respond_to? :cause e = e.cause end rc end |
#set_backtrace(bt) ⇒ Object
32 33 34 35 36 |
# File 'lib/nested_exceptions.rb', line 32 def set_backtrace(bt) prevent_recursion do super process_backtrace(bt) end end |