Sha256: 76255c69d7bf39a4d0fbf104cea396291b91c0ad496b0b3f7e9b595868cdc300

Contents?: true

Size: 1.15 KB

Versions: 2

Compression:

Stored size: 1.15 KB

Contents

module Dry
  module Types
    extend Dry::Configurable

    setting :namespace, self

    class SchemaError < TypeError
      def initialize(key, value)
        super("#{value.inspect} (#{value.class}) has invalid type for :#{key}")
      end
    end

    class SchemaKeyError < KeyError
      def initialize(key)
        super(":#{key} is missing in Hash input")
      end
    end

    StructError = Class.new(TypeError)

    class RepeatedAttributeError < ArgumentError
      def initialize(key)
        super("Attribute :#{key} has already been defined")
      end
    end

    ConstraintError = Class.new(TypeError) do
      attr_reader :result

      def initialize(result)
        @result = result
        if result.is_a?(String)
          super
        else
          super("#{result.input.inspect} violates constraints (#{failure_message})")
        end
      end

      def input
        result.input
      end

      def failure_message
        if result.respond_to?(:rule)
          rule = result.rule
          "#{rule.predicate.id}(#{rule.predicate.args.map(&:inspect).join(', ')}) failed"
        else
          result.inspect
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dry-types-0.7.2 lib/dry/types/errors.rb
dry-types-0.7.1 lib/dry/types/errors.rb