Class: OpenapiFirst::Plugins::Jsonapi::ErrorResponse

Inherits:
Object
  • Object
show all
Includes:
ErrorResponse
Defined in:
lib/openapi_first/plugins/jsonapi/error_response.rb

Instance Attribute Summary

Attributes included from ErrorResponse

#failure

Instance Method Summary collapse

Methods included from ErrorResponse

#initialize, #render, #status

Instance Method Details

#bodyObject



9
10
11
# File 'lib/openapi_first/plugins/jsonapi/error_response.rb', line 9

def body
  MultiJson.dump({ errors: serialized_errors })
end

#content_typeObject



13
14
15
# File 'lib/openapi_first/plugins/jsonapi/error_response.rb', line 13

def content_type
  'application/vnd.api+json'
end

#default_errorsObject



30
31
32
33
34
35
# File 'lib/openapi_first/plugins/jsonapi/error_response.rb', line 30

def default_errors
  [{
    status: status.to_s,
    title: Rack::Utils::HTTP_STATUS_CODES[status]
  }]
end

#pointer(data_pointer) ⇒ Object



50
51
52
53
54
# File 'lib/openapi_first/plugins/jsonapi/error_response.rb', line 50

def pointer(data_pointer)
  return data_pointer if failure.error_type == :invalid_body

  data_pointer.delete_prefix('/')
end

#pointer_keyObject



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/openapi_first/plugins/jsonapi/error_response.rb', line 37

def pointer_key
  case failure.error_type
  when :invalid_body
    :pointer
  when :invalid_query, :invalid_path
    :parameter
  when :invalid_header
    :header
  when :invalid_cookie
    :cookie
  end
end

#serialized_errorsObject



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/openapi_first/plugins/jsonapi/error_response.rb', line 17

def serialized_errors
  return default_errors unless failure.errors

  key = pointer_key
  failure.errors.map do |error|
    {
      status: status.to_s,
      source: { key => pointer(error.instance_location) },
      title: error.error
    }
  end
end