Sha256: 108936dfda086259b1ff1d0e90ba0183b1c0a98394505cd9370e37d70b030318

Contents?: true

Size: 1.38 KB

Versions: 2

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

module Backup
  # Provides cascading errors with formatted messages.
  # See the specs for details.
  module NestedExceptions
    def self.included(klass)
      klass.extend(Module.new do
        def wrap(wrapped_exception, msg = nil)
          new(msg, wrapped_exception)
        end
      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(%r{^ *}, "  ").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(%r{^Backup::}, "")
    end
  end

  class Error < StandardError
    include NestedExceptions
  end

  class FatalError < Exception
    include NestedExceptions
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
backupii-0.1.0.pre.alpha.2 lib/backup/errors.rb
backupii-0.1.0.pre.alpha.1 lib/backup/errors.rb