Module: JsonApiToolbox::RenderizableExceptions

Extended by:
ActiveSupport::Concern
Defined in:
lib/renderizable_exceptions.rb

Instance Method Summary collapse

Instance Method Details

#build_jsonapi(exception) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/renderizable_exceptions.rb', line 19

def build_jsonapi(exception)
  { jsonapi_errors:
    { title: exception.class.to_s,
      detail: exception.message,
      status: 500,
      code: code(exception),
      source: {} },
    jsonapi_class: {
      class: { Hash: JsonApiToolbox::Serializables::SerializableException }
    }, status: :internal_server_error }
end

#code(exception) ⇒ Object



47
48
49
50
51
# File 'lib/renderizable_exceptions.rb', line 47

def code(exception)
  model_errors_code(exception) ||
    exception.try(:code) ||
    'default_error'
end

#log_exception(exception) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/renderizable_exceptions.rb', line 31

def log_exception(exception)
  if Gem.loaded_specs.key?('log_toolbox')
    log_critical(exception)
  else
    puts_build_log_error(exception)
  end
end

#model_errors_code(error) ⇒ Object



53
54
55
56
57
58
# File 'lib/renderizable_exceptions.rb', line 53

def model_errors_code(error)
  detail_error = error.try(:detail)
  detail_error.try(:map, &:flatten).try(:map) do |item|
    [item[0], item[1]['error']].join('_error_')
  end
end

#puts_build_log_error(error) ⇒ Object



43
44
45
# File 'lib/renderizable_exceptions.rb', line 43

def puts_build_log_error(error)
  puts messages: error.try(:message), trace: error.try(:backtrace)
end

#render_any_exception(exception) ⇒ Object



13
14
15
16
17
# File 'lib/renderizable_exceptions.rb', line 13

def render_any_exception(exception)
  log_exception(exception)
  send_to_apm(exception)
  render build_jsonapi(exception)
end

#send_to_apm(exception) ⇒ Object



39
40
41
# File 'lib/renderizable_exceptions.rb', line 39

def send_to_apm(exception)
  ElasticAPM.report(exception) if Gem.loaded_specs.key?('elastic-apm')
end