Sha256: d8a9b7bad5c174e857475fa2af161bc0acd716bc001ebbb3ce530b105e16481d

Contents?: true

Size: 1.24 KB

Versions: 3

Compression:

Stored size: 1.24 KB

Contents

# frozen_string_literal: true

module Lite
  module Command

    class Fault < StandardError

      attr_reader :reason, :metadata, :caused_by, :thrown_by

      def initialize(**params)
        @reason    = params.fetch(:reason)
        @metadata  = params.fetch(:metadata)
        @caused_by = params.fetch(:caused_by)
        @thrown_by = params.fetch(:thrown_by)

        super(reason)
      end

      def self.build(type, catcher, thrower, dynamic: false)
        klass = dynamic ? catcher.class : Lite::Command
        fault = klass.const_get(type.to_s)
        fault = fault.new(
          reason: catcher.reason,
          metadata: catcher.metadata,
          caused_by: catcher.caused_by,
          thrown_by: catcher.thrown_by
        )
        fault.set_backtrace(thrower.backtrace) if thrower.respond_to?(:backtrace)
        fault
      end

      def self.===(item)
        Utils.descendant_of?(self, item) || Utils.descendant_of?(item, self)
      end

      def type
        @type ||= self.class.name.split("::").last.downcase
      end

      def ===(item)
        Utils.descendant_of?(self, item)
      end

    end

    class Noop < Fault; end
    class Invalid < Fault; end
    class Failure < Fault; end
    class Error < Fault; end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
lite-command-3.1.2 lib/lite/command/fault.rb
lite-command-3.1.1 lib/lite/command/fault.rb
lite-command-3.1.0 lib/lite/command/fault.rb