Class: OpenapiFirst::ErrorResponses::Default

Inherits:
Object
  • Object
show all
Includes:
OpenapiFirst::ErrorResponse
Defined in:
lib/openapi_first/error_responses/default.rb

Overview

An error reponse that returns application/problem+json with a list of “errors” See also www.rfc-editor.org/rfc/rfc9457.html

Instance Attribute Summary

Attributes included from OpenapiFirst::ErrorResponse

#failure

Instance Method Summary collapse

Methods included from OpenapiFirst::ErrorResponse

#initialize, #render, #status

Instance Method Details

#bodyObject



24
25
26
27
28
29
30
31
# File 'lib/openapi_first/error_responses/default.rb', line 24

def body
  result = {
    title:,
    status:
  }
  result[:errors] = errors if failure.errors
  JSON.generate(result)
end

#content_typeObject



39
40
41
# File 'lib/openapi_first/error_responses/default.rb', line 39

def content_type
  'application/problem+json'
end

#errorsObject



43
44
45
46
47
48
49
50
51
52
# File 'lib/openapi_first/error_responses/default.rb', line 43

def errors
  key = pointer_key
  failure.errors.map do |error|
    {
      message: error.message,
      key => pointer(error.data_pointer),
      code: error.type
    }
  end
end

#pointer(data_pointer) ⇒ Object



67
68
69
70
71
# File 'lib/openapi_first/error_responses/default.rb', line 67

def pointer(data_pointer)
  return data_pointer if type == :invalid_body

  data_pointer.delete_prefix('/')
end

#pointer_keyObject



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/openapi_first/error_responses/default.rb', line 54

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

#titleObject



35
36
37
# File 'lib/openapi_first/error_responses/default.rb', line 35

def title
  TITLES.fetch(type)
end

#typeObject



33
# File 'lib/openapi_first/error_responses/default.rb', line 33

def type = failure.type