Sha256: 1582249c8761581890ab0f39565be5551f1f080e5c05d5dd80bd7ecc33b2c203

Contents?: true

Size: 1.37 KB

Versions: 63

Compression:

Stored size: 1.37 KB

Contents

# encoding: utf-8

module Backup

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

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

    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

    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

    private

    def clean_name(name)
      name.sub(/^Backup::/, '')
    end

  end

  class Error < StandardError
    include NestedExceptions
  end

  class FatalError < Exception
    include NestedExceptions
  end
end

Version data entries

63 entries across 63 versions & 7 rubygems

Version Path
backup-3.7.2 lib/backup/errors.rb
backup-3.7.1 lib/backup/errors.rb
backup-3.7.0 lib/backup/errors.rb