Exception: Grape::Exceptions::ValidationErrors

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

Constant Summary

Constants inherited from Base

Base::MESSAGE_STEPS

Instance Attribute Summary collapse

Attributes inherited from Base

#headers, #status

Instance Method Summary collapse

Methods inherited from Base

#[]

Constructor Details

#initialize(exceptions: [], headers: {}) ⇒ ValidationErrors

Returns a new instance of ValidationErrors.



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

def initialize(exceptions: [], headers: {})
  @errors = exceptions.flat_map(&:errors).group_by(&:params)
  super(message: full_messages.join(', '), status: 400, headers:)
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



6
7
8
# File 'lib/grape/exceptions/validation_errors.rb', line 6

def errors
  @errors
end

Instance Method Details

#as_json(**_opts) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/grape/exceptions/validation_errors.rb', line 13

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

#full_messagesObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/grape/exceptions/validation_errors.rb', line 26

def full_messages
  messages = errors.flat_map do |attributes, errs|
    errs.map do |error|
      translate(
        :format,
        scope: 'grape.errors',
        default: '%<attributes>s %<message>s',
        attributes: translate_attributes(attributes),
        message: error.message
      )
    end
  end
  messages.uniq!
  messages
end

#to_json(*_opts) ⇒ Object



22
23
24
# File 'lib/grape/exceptions/validation_errors.rb', line 22

def to_json(*_opts)
  as_json.to_json
end