Class: OpenapiFirst::Plugins::Default::ErrorResponse

Inherits:
Object
  • Object
show all
Includes:
ErrorResponse
Defined in:
lib/openapi_first/plugins/default/error_response.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 ErrorResponse

#failure

Instance Method Summary collapse

Methods included from ErrorResponse

#initialize, #render, #status

Instance Method Details

#bodyObject



23
24
25
26
27
28
29
30
# File 'lib/openapi_first/plugins/default/error_response.rb', line 23

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

#content_typeObject



38
39
40
# File 'lib/openapi_first/plugins/default/error_response.rb', line 38

def content_type
  'application/problem+json'
end

#errorsObject



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

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

#pointer(data_pointer) ⇒ Object



66
67
68
69
70
# File 'lib/openapi_first/plugins/default/error_response.rb', line 66

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

  data_pointer.delete_prefix('/')
end

#pointer_keyObject



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

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



34
35
36
# File 'lib/openapi_first/plugins/default/error_response.rb', line 34

def title
  TITLES.fetch(type)
end

#typeObject



32
# File 'lib/openapi_first/plugins/default/error_response.rb', line 32

def type = failure.type