Class: Praxis::Responses::ValidationError

Inherits:
BadRequest show all
Defined in:
lib/praxis/responses/validation_error.rb

Instance Attribute Summary

Attributes inherited from Praxis::Response

#body, #headers, #name, #parts, #request, #status

Instance Method Summary collapse

Methods inherited from Praxis::Response

#add_part, #encode!, #finish, #handle, inherited, #response_name, #validate

Constructor Details

#initialize(errors: nil, exception: nil, **opts) ⇒ ValidationError

Returns a new instance of ValidationError.



6
7
8
9
10
11
# File 'lib/praxis/responses/validation_error.rb', line 6

def initialize(errors: nil, exception: nil, **opts)
  super(**opts)
  @headers['Content-Type'] = 'application/json' #TODO: might want an error mediatype
  @errors = errors
  @exception = exception
end

Instance Method Details

#format!Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/praxis/responses/validation_error.rb', line 13

def format!
  if @errors
    @body = {name: 'ValidationError', errors: @errors}
  elsif @exception
    @body = {name: 'ValidationError', message: @exception.message}
    if @exception.cause
      @body[:cause] = {name: @exception.cause.class.name, message: @exception.cause.message}
    end
    @body
  end
end