Exception: Marr::ApiError
- Inherits:
-
StandardError
- Object
- StandardError
- Marr::ApiError
- Defined in:
- lib/marr/api_error.rb
Instance Attribute Summary collapse
-
#add_context ⇒ Object
Returns the value of attribute add_context.
-
#add_error_context ⇒ Object
Returns the value of attribute add_error_context.
-
#object ⇒ Object
Returns the value of attribute object.
-
#request ⇒ Object
Returns the value of attribute request.
-
#resource ⇒ Object
Returns the value of attribute resource.
-
#response ⇒ Object
Returns the value of attribute response.
Instance Method Summary collapse
- #custom_render ⇒ Object
- #default_render ⇒ Object
- #detail ⇒ Object
-
#initialize(attributes = {}) ⇒ ApiError
constructor
A new instance of ApiError.
- #message ⇒ Object
- #object_errors ⇒ Object
- #render ⇒ Object
- #status ⇒ Object
- #subcode ⇒ Object
- #subcode_detail ⇒ Object
- #subcodes(hash = nil) ⇒ Object
- #trace_id ⇒ Object
- #type ⇒ Object
Constructor Details
#initialize(attributes = {}) ⇒ ApiError
Returns a new instance of ApiError.
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/marr/api_error.rb', line 9 def initialize(attributes={}) @controller = attributes[:controller] @object = attributes[:object] @resource = attributes[:resource] || 'Resource'.freeze @subcode = attributes[:subcode] @override_detail = attributes[:override_detail] @override_status = attributes[:override_status] @add_context = attributes[:add_context] || {} @add_error_context = attributes[:add_error_context] || {} @request = @controller.try(:request) @response = @controller.try(:response) set_resource end |
Instance Attribute Details
#add_context ⇒ Object
Returns the value of attribute add_context.
7 8 9 |
# File 'lib/marr/api_error.rb', line 7 def add_context @add_context end |
#add_error_context ⇒ Object
Returns the value of attribute add_error_context.
7 8 9 |
# File 'lib/marr/api_error.rb', line 7 def add_error_context @add_error_context end |
#object ⇒ Object
Returns the value of attribute object.
7 8 9 |
# File 'lib/marr/api_error.rb', line 7 def object @object end |
#request ⇒ Object
Returns the value of attribute request.
7 8 9 |
# File 'lib/marr/api_error.rb', line 7 def request @request end |
#resource ⇒ Object
Returns the value of attribute resource.
7 8 9 |
# File 'lib/marr/api_error.rb', line 7 def resource @resource end |
#response ⇒ Object
Returns the value of attribute response.
7 8 9 |
# File 'lib/marr/api_error.rb', line 7 def response @response end |
Instance Method Details
#custom_render ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/marr/api_error.rb', line 88 def custom_render if Marr.configuration.custom_render raise NotImplementedError, 'Message must be implemented. Add Error message method.' else nil end end |
#default_render ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/marr/api_error.rb', line 96 def default_render { errors: { id: trace_id, status: status, code: type, title: , detail: subcode_detail, meta: { object_errors: object_errors, trace_id: trace_id, } }.merge(add_error_context) }.merge(add_context) end |
#detail ⇒ Object
66 67 68 69 70 |
# File 'lib/marr/api_error.rb', line 66 def detail return '' unless @override_detail.present? || subcodes[@subcode].present? @override_detail.present? ? override_detail : subcodes[@subcode] end |
#message ⇒ Object
46 47 48 |
# File 'lib/marr/api_error.rb', line 46 def raise NotImplementedError, 'Message must be implemented. Add Error message method.' end |
#object_errors ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/marr/api_error.rb', line 23 def object_errors return [] unless @object.present? return [] unless @object&.errors&..present? attributes = @object.errors.details.map(&:first) @object_errors = [] attributes.each do |attrb| @object.errors.(attrb).each do |msg| error = { pointer: attrb.to_s } = error.merge(detail: msg) @object_errors << end end @object_errors end |
#render ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/marr/api_error.rb', line 80 def render if Marr.configuration.custom_render custom_render else default_render.to_json end end |
#status ⇒ Object
41 42 43 44 |
# File 'lib/marr/api_error.rb', line 41 def status default_status = '422' @override_status&.integer? ? @override_status.to_s : default_status end |
#subcode ⇒ Object
56 57 58 59 60 |
# File 'lib/marr/api_error.rb', line 56 def subcode return '' unless subcodes[@subcode].present? @subcode.to_s.camelcase end |
#subcode_detail ⇒ Object
62 63 64 |
# File 'lib/marr/api_error.rb', line 62 def subcode_detail subcodes[@subcode].present? ? subcodes[@subcode] : '' end |
#subcodes(hash = nil) ⇒ Object
50 51 52 53 54 |
# File 'lib/marr/api_error.rb', line 50 def subcodes(hash=nil) return {} if hash.blank hash.with_indifferent_access end |
#trace_id ⇒ Object
72 73 74 |
# File 'lib/marr/api_error.rb', line 72 def trace_id trace_id ||= SecureRandom.hex(trace_id_length).upcase end |
#type ⇒ Object
76 77 78 |
# File 'lib/marr/api_error.rb', line 76 def type self.class.name.split('::').last end |