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
Error name => code Status => status Subcodes => title message => detail.
- #render ⇒ Object
- #status(status: 422) ⇒ Object
- #subcode ⇒ 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' @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
87 88 89 90 91 92 93 |
# File 'lib/marr/api_error.rb', line 87 def custom_render if Marr::Api::Error.configuration.custom_render raise NotImplementedError, 'Message must be implemented. Add Error message method.' else nil end end |
#default_render ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/marr/api_error.rb', line 95 def default_render { errors: { code: type, title: subcode, detail: , meta: { object_errors: object_errors, trace_id: trace_id, } }.merge(add_error_context) }.merge(add_context) end |
#detail ⇒ Object
65 66 67 68 69 |
# File 'lib/marr/api_error.rb', line 65 def detail return '' unless @override_detail.present? || subcodes[@subcode].present? @override_detail.present? ? override_detail : subcodes[@subcode] end |
#message ⇒ Object
49 50 51 |
# File 'lib/marr/api_error.rb', line 49 def raise NotImplementedError, 'Message must be implemented. Add Error message method.' end |
#object_errors ⇒ Object
Error name => code Status => status Subcodes => title message => detail
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/marr/api_error.rb', line 27 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
79 80 81 82 83 84 85 |
# File 'lib/marr/api_error.rb', line 79 def render if Marr::Api::Error.configuration.custom_render custom_render else default_render.to_json end end |
#status(status: 422) ⇒ Object
45 46 47 |
# File 'lib/marr/api_error.rb', line 45 def status(status: 422) @override_status&.integer? ? @override_status : status end |
#subcode ⇒ Object
59 60 61 62 63 |
# File 'lib/marr/api_error.rb', line 59 def subcode return '' unless subcodes[@subcode].present? @subcode.to_s.camelcase end |
#subcodes(hash = nil) ⇒ Object
53 54 55 56 57 |
# File 'lib/marr/api_error.rb', line 53 def subcodes(hash=nil) return {} if hash.blank hash.with_indifferent_access end |
#trace_id ⇒ Object
71 72 73 |
# File 'lib/marr/api_error.rb', line 71 def trace_id trace_id ||= SecureRandom.hex(trace_id_length).upcase end |
#type ⇒ Object
75 76 77 |
# File 'lib/marr/api_error.rb', line 75 def type self.class.name.split('::').last end |