Sha256: ca7db86a3f67c6c94fec822410417449e9cd8bccf94c4e5454dd1facfa743754

Contents?: true

Size: 1015 Bytes

Versions: 1

Compression:

Stored size: 1015 Bytes

Contents

require 'grape/exceptions/base'

module Grape
  module Exceptions
    class ValidationErrors < Grape::Exceptions::Base
      include Enumerable

      attr_reader :errors

      def initialize(args = {})
        @errors = {}
        args[:errors].each do |validation_error|
          @errors[validation_error.param] ||= []
          @errors[validation_error.param] << validation_error
        end
        super message: full_messages.join(', '), status: 400
      end

      def each
        errors.each_pair do |attribute, errors|
          errors.each do |error|
            yield attribute, error
          end
        end
      end

      private

      def full_messages
        map { |attribute, error| full_message(attribute, error) }
      end

      def full_message(attribute, error)
        I18n.t("grape.errors.format".to_sym, {
          default:  "%{attribute} %{message}",
          attribute: translate_attribute(attribute),
          message:   error.message
        })
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
grape-0.6.1 lib/grape/exceptions/validation_errors.rb