Exception: Marr::ApiError

Inherits:
StandardError
  • Object
show all
Defined in:
lib/marr/api_error.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_contextObject

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_contextObject

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

#objectObject

Returns the value of attribute object.



7
8
9
# File 'lib/marr/api_error.rb', line 7

def object
  @object
end

#requestObject

Returns the value of attribute request.



7
8
9
# File 'lib/marr/api_error.rb', line 7

def request
  @request
end

#resourceObject

Returns the value of attribute resource.



7
8
9
# File 'lib/marr/api_error.rb', line 7

def resource
  @resource
end

#responseObject

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_renderObject



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_renderObject



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: message,
      detail: subcode_detail,
      meta: {
        object_errors: object_errors,
        trace_id: trace_id,
      }
    }.merge(add_error_context)
  }.merge(add_context)
end

#detailObject



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

#messageObject

Raises:

  • (NotImplementedError)


46
47
48
# File 'lib/marr/api_error.rb', line 46

def message
  raise NotImplementedError, 'Message must be implemented. Add Error message method.'
end

#object_errorsObject



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&.full_messages.present?

  attributes = @object.errors.details.map(&:first)
  @object_errors = []

  attributes.each do |attrb|
    @object.errors.full_messages_for(attrb).each do |msg|
      error = { pointer: attrb.to_s }
      error_with_message = error.merge(detail: msg)

      @object_errors << error_with_message
    end
  end
  @object_errors
end

#renderObject



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

#statusObject



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

#subcodeObject



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_detailObject



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_idObject



72
73
74
# File 'lib/marr/api_error.rb', line 72

def trace_id
  trace_id ||= SecureRandom.hex(trace_id_length).upcase
end

#typeObject



76
77
78
# File 'lib/marr/api_error.rb', line 76

def type
  self.class.name.split('::').last
end