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

Returns a new instance of ExceptionFormatter.



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

def initialize(exception, backtrace_filter, options)
  @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.



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

def backtrace_filter
  @backtrace_filter
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

Class Method Details

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



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

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

Instance Method Details

#formatted_backtraceObject



32
33
34
35
36
37
38
39
40
# File 'lib/yeller/exception_formatter.rb', line 32

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



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

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

#to_hashObject



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/yeller/exception_formatter.rb', line 42

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