Class: Logatron::ErrorFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/logatron/error_formatter.rb

Instance Method Summary collapse

Instance Method Details

#format_additional_info(e, additional_info = nil) ⇒ Object



18
19
20
21
# File 'lib/logatron/error_formatter.rb', line 18

def format_additional_info(e, additional_info = nil)
  info = additional_info || (e.respond_to?(:meta) && e.meta)
  info ? "; MORE_INFO( #{info.to_s} )" : ''
end

#format_backtrace(error_or_bt) ⇒ Object



13
14
15
16
# File 'lib/logatron/error_formatter.rb', line 13

def format_backtrace(error_or_bt)
  bt = error_or_bt.is_a?(Exception) ? error_or_bt.backtrace : error_or_bt
  bt.map { |f| "      #{f.starts_with?('...') ? f : "at #{f}"}\n" }.join
end

#format_error_report(e, additional_info = nil) ⇒ Object

Returns a human-readable exception report. Includes nested exception information. Suppresses duplicate frames in nested exception backtraces.



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/logatron/error_formatter.rb', line 26

def format_error_report(e, additional_info = nil)
  error_chain = Enumerator.unfold(e) { |e| [e.cause, e.cause] }.to_a
  initial_report = format_single_error(e, clean_backtrace(nil, e), additional_info)
  # starting with the outermost error, reduce the error chain into the formatted report
  error_chain.reduce([e, initial_report]) do |(prev_e, report), e|
    new_report = format_single_error(e, clean_backtrace(prev_e, e)) + "  which caused:\n    " + report
    [e, new_report]
  end[1]
rescue
  # if something went wrong, fall back to something simpler
  format_short_message(e) + "\n" + e.backtrace.join("\n")
end

#format_full_message(e, additional_info = nil) ⇒ Object



9
10
11
# File 'lib/logatron/error_formatter.rb', line 9

def format_full_message(e, additional_info = nil)
  format_short_message(e) + format_additional_info(e, additional_info)
end

#format_short_message(e) ⇒ Object



5
6
7
# File 'lib/logatron/error_formatter.rb', line 5

def format_short_message(e)
  "#{e.class}: #{e.message}"
end