Class: Yeller::ExceptionFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/yeller/exception_formatter.rb

Defined Under Namespace

Classes: IdentityBacktraceFilter

Constant Summary collapse

BACKTRACE_FORMAT =
%r{^((?:[a-zA-Z]:)?[^:]+):(\d+)(?::in `([^']+)')?$}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(e, causes, backtrace_filter, options) ⇒ ExceptionFormatter

Returns a new instance of ExceptionFormatter.



40
41
42
43
44
45
46
47
48
# File 'lib/yeller/exception_formatter.rb', line 40

def initialize(e, causes, backtrace_filter, options)
  exception = e
  @type = exception.class.name
  @message = exception.message
  @backtrace = exception.backtrace
  @causes = causes
  @backtrace_filter = backtrace_filter
  @options = options
end

Instance Attribute Details

#backtrace_filterObject (readonly)

Returns the value of attribute backtrace_filter.



38
39
40
# File 'lib/yeller/exception_formatter.rb', line 38

def backtrace_filter
  @backtrace_filter
end

#optionsObject (readonly)

Returns the value of attribute options.



38
39
40
# File 'lib/yeller/exception_formatter.rb', line 38

def options
  @options
end

#typeObject (readonly)

Returns the value of attribute type.



38
39
40
# File 'lib/yeller/exception_formatter.rb', line 38

def type
  @type
end

Class Method Details

.format(exception, backtrace_filter = IdentityBacktraceFilter.new, options = {}) ⇒ Object



12
13
14
15
16
17
# File 'lib/yeller/exception_formatter.rb', line 12

def self.format(exception, backtrace_filter=IdentityBacktraceFilter.new, options={})
  all_causes = process_causes(exception)
  root = all_causes.fetch(0, exception)
  causes = all_causes.drop(1)
  new(root, causes || [], backtrace_filter, options).to_hash
end

.process_causes(exception) ⇒ Object



19
20
21
# File 'lib/yeller/exception_formatter.rb', line 19

def self.process_causes(exception)
  unwrap_causes(exception)
end

.unwrap_causes(exception) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/yeller/exception_formatter.rb', line 23

def self.unwrap_causes(exception)
  causes = [exception]
  previously_seen = Set.new([exception.object_id])
  while exception.respond_to?(:cause) && exception.cause
    cause = exception.cause
    if previously_seen.include?(cause.object_id)
      break
    end
    causes << cause
    previously_seen << cause.object_id
    exception = cause
  end
  causes.reverse!
end

Instance Method Details

#causesObject



65
66
67
68
69
70
71
72
73
# File 'lib/yeller/exception_formatter.rb', line 65

def causes
  @causes.map do |cause|
    {
      :type => cause.class.name,
      :message => cause.message,
      :stacktrace => formatted_backtrace(cause.backtrace),
    }
  end
end

#formatted_backtrace(backtrace) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/yeller/exception_formatter.rb', line 55

def formatted_backtrace(backtrace)
  return [] unless backtrace

  original_trace = backtrace.map do |line|
    _, file, number, method = line.match(BACKTRACE_FORMAT).to_a
    [file, number, method]
  end
  backtrace_filter.filter(original_trace)
end

#messageObject



50
51
52
53
# File 'lib/yeller/exception_formatter.rb', line 50

def message
  # If a message is not given, rubby will set message to the class name
  @message == type ? nil : @message
end

#to_hashObject



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/yeller/exception_formatter.rb', line 75

def to_hash
  result = {
    :message => message,
    :stacktrace => formatted_backtrace(@backtrace),
    :type => type,
    :"custom-data" => options.fetch(:custom_data, {}),
    :causes => causes,
  }
  result[:url] = options[:url] if options.key?(:url)
  result[:location] = options[:location] if options.key?(:location)
  result
end