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'
  @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



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_renderObject



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

#detailObject



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

#messageObject

Raises:

  • (NotImplementedError)


49
50
51
# File 'lib/marr/api_error.rb', line 49

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

#object_errorsObject

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&.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



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

#subcodeObject



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_idObject



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

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

#typeObject



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

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