Exception: Grape::Exceptions::ValidationErrors

Inherits:
Base
  • Object
show all
Includes:
Enumerable
Defined in:
lib/grape/exceptions/validation_errors.rb

Constant Summary

Constants inherited from Base

Base::BASE_ATTRIBUTES_KEY, Base::BASE_MESSAGES_KEY, Base::FALLBACK_LOCALE

Instance Attribute Summary collapse

Attributes inherited from Base

#headers, #message, #status

Instance Method Summary collapse

Methods inherited from Base

#[]

Constructor Details

#initialize(args = {}) ⇒ ValidationErrors

Returns a new instance of ValidationErrors



10
11
12
13
14
15
16
17
# File 'lib/grape/exceptions/validation_errors.rb', line 10

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

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors



8
9
10
# File 'lib/grape/exceptions/validation_errors.rb', line 8

def errors
  @errors
end

Instance Method Details

#as_jsonObject



27
28
29
30
31
32
33
34
# File 'lib/grape/exceptions/validation_errors.rb', line 27

def as_json
  errors.map do |k, v|
    {
      params: k,
      messages: v.map(&:to_s)
    }
  end
end

#eachObject



19
20
21
22
23
24
25
# File 'lib/grape/exceptions/validation_errors.rb', line 19

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

#to_jsonObject



36
37
38
# File 'lib/grape/exceptions/validation_errors.rb', line 36

def to_json
  as_json.to_json
end