Module: Backup::NestedExceptions

Included in:
Error, FatalError
Defined in:
lib/backup/errors.rb

Overview

Provides cascading errors with formatted messages. See the specs for details.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/backup/errors.rb', line 9

def self.included(klass)
  klass.extend Module.new {
    def wrap(wrapped_exception, msg = nil)
      new(msg, wrapped_exception)
    end
  }
end

Instance Method Details

#exception(obj = nil) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/backup/errors.rb', line 35

def exception(obj = nil)
  return self if obj.nil? || equal?(obj)

  ex = self.class.new(obj, @wrapped_exception)
  ex.set_backtrace(backtrace) unless ex.backtrace
  ex
end

#initialize(obj = nil, wrapped_exception = nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/backup/errors.rb', line 17

def initialize(obj = nil, wrapped_exception = nil)
  @wrapped_exception = wrapped_exception
  msg = (obj.respond_to?(:to_str) ? obj.to_str : obj.to_s).
        gsub(/^ */, '  ').strip
  msg = clean_name(self.class.name) + (msg.empty? ? '' : ": #{ msg }")

  if wrapped_exception
    msg << "\n--- Wrapped Exception ---\n"
    class_name = clean_name(wrapped_exception.class.name)
    msg << class_name + ': ' unless
        wrapped_exception.message.start_with? class_name
    msg << wrapped_exception.message
  end

  super(msg)
  set_backtrace(wrapped_exception.backtrace) if wrapped_exception
end