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, backtrace_filter, options) ⇒ ExceptionFormatter

Returns a new instance of ExceptionFormatter.



17
18
19
20
21
22
23
24
# File 'lib/yeller/exception_formatter.rb', line 17

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

Instance Attribute Details

#backtrace_filterObject (readonly)

Returns the value of attribute backtrace_filter.



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

def backtrace_filter
  @backtrace_filter
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

Class Method Details

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



11
12
13
# File 'lib/yeller/exception_formatter.rb', line 11

def self.format(exception, backtrace_filter=IdentityBacktraceFilter.new, options={})
  new(exception, backtrace_filter, options).to_hash
end

Instance Method Details

#formatted_backtraceObject



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

def formatted_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



34
35
36
37
# File 'lib/yeller/exception_formatter.rb', line 34

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

#to_hashObject



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/yeller/exception_formatter.rb', line 49

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

#unwrap_nested_exception(e) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/yeller/exception_formatter.rb', line 26

def unwrap_nested_exception(e)
  if e.respond_to?(:cause) && e.cause
    e.cause
  else
    e
  end
end