Sha256: 154e1bbad69828e20c371d353e602478ed0c59497ebb6572760f8b68b44585bf

Contents?: true

Size: 1 KB

Versions: 6

Compression:

Stored size: 1 KB

Contents

module Rasti
  module Types

    class Error < StandardError
    end

    class CastError < Error

      attr_reader :type, :value

      def initialize(type, value)
        @type = type
        @value = value

        super "Invalid cast: #{display_value} -> #{type}"
      end

      private

      def display_value
        value.is_a?(::String) ? "'#{value}'" : value.inspect
      end

    end

    class CompoundError < Error

      attr_reader :errors

      def initialize(errors)
        @errors = errors
        super "#{message_title}\n#{message_lines}"
      end

      private

      def message_title
        'Errors:'
      end

      def message_lines
        errors.map { |k,v| "- #{k}: #{v}" }.join("\n")
      end

    end

    class MultiCastError < CompoundError

      attr_reader :type, :value

      def initialize(type, value, errors)
        @type = type
        @value = value
        super errors
      end

      private

      def message_title
        'Cast errors:'
      end

    end

  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rasti-types-2.0.1 lib/rasti/types/errors.rb
rasti-types-2.0.0 lib/rasti/types/errors.rb
rasti-types-1.1.2 lib/rasti/types/errors.rb
rasti-types-1.1.1 lib/rasti/types/errors.rb
rasti-types-1.1.0 lib/rasti/types/errors.rb
rasti-types-1.0.0 lib/rasti/types/errors.rb