Sha256: c237d5ae81a79decd834ecb2b19c8cede99251d99faea4e71e5d33918468c9ac

Contents?: true

Size: 1.42 KB

Versions: 18

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

module LedgerSync
  class Error < StandardError
    module HelpersMixin
      module ClassMethods
        def raise_if_unexpected_class(expected:, given:)
          expected = Array(expected)
          expected = expected.map { |e| (e.is_a?(Class) ? e : e.class) }
          given = given.class unless given.is_a?(Class)

          return if expected.any? { |c| given <= c }

          raise UnexpectedClassError.new(
            expected: expected,
            given: given
          )
        end
      end

      def self.included(base)
        base.extend ClassMethods
      end
    end

    attr_reader :message

    def initialize(message:)
      @message = message
      super(message)
    end

    class UnexpectedClassError < self
      def initialize(expected:, given:)
        expected = Array(expected)

        super(
          message: "Unexpected class.  Given #{given.name}.  Expected: #{expected.map(&:name).join(', ')}"
        )
      end
    end

    module TypeError
      class ValueClassError < UnexpectedClassError
      end

      class StringFromSetError < Error
        def initialize(expected:, given:)
          to_show = expected.first(10).join(', ')
          to_show += ", ... (#{expected.count - 10} more possible values)" if expected.count > 10
          super(
            message: "Unexpected value. Given #{given}. Expected: #{to_show}"
          )
        end
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
ledger_sync-2.6.0 lib/ledger_sync/error.rb
ledger_sync-2.5.0 lib/ledger_sync/error.rb
ledger_sync-2.4.4 lib/ledger_sync/error.rb
ledger_sync-2.3.1 lib/ledger_sync/error.rb
ledger_sync-2.2.3 lib/ledger_sync/error.rb
ledger_sync-2.2.1 lib/ledger_sync/error.rb
ledger_sync-2.2.0 lib/ledger_sync/error.rb
ledger_sync-2.0.2 lib/ledger_sync/error.rb
ledger_sync-2.0.1 lib/ledger_sync/error.rb
ledger_sync-2.0.0 lib/ledger_sync/error.rb
ledger_sync-2.0.0.pre.1 lib/ledger_sync/error.rb
ledger_sync-1.8.1 lib/ledger_sync/error.rb
ledger_sync-1.8.0 lib/ledger_sync/error.rb
ledger_sync-1.7.0 lib/ledger_sync/error.rb
ledger_sync-1.6.0 lib/ledger_sync/error.rb
ledger_sync-1.5.2 lib/ledger_sync/error.rb
ledger_sync-1.5.1 lib/ledger_sync/error.rb
ledger_sync-1.5.0 lib/ledger_sync/error.rb