class Honeybadger::Backtrace::Line

Handles backtrace parsing line by line.

Constants

INPUT_FORMAT

Backtrace line regexp (optionally allowing leading X: for windows support).

Attributes

file[RW]

The file portion of the line (such as app/models/user.rb).

filtered_file[RW]

Filtered representations

filtered_method[RW]

Filtered representations

filtered_number[RW]

Filtered representations

method[RW]

The method of the line (such as index).

number[RW]

The line number portion of the line.

source_radius[RW]

Public Class Methods

new(file, number, method, filtered_file = file, filtered_number = number, filtered_method = method, source_radius = 2) click to toggle source
# File lib/honeybadger/backtrace.rb, line 52
def initialize(file, number, method, filtered_file = file,
               filtered_number = number, filtered_method = method,
               source_radius = 2)
  self.filtered_file   = filtered_file
  self.filtered_number = filtered_number
  self.filtered_method = filtered_method
  self.file            = file
  self.number          = number
  self.method          = method
  self.source_radius   = source_radius
end
parse(unparsed_line, opts = {}) click to toggle source

Parses a single line of a given backtrace

@param [String] unparsed_line The raw line from caller or some backtrace.

@return The parsed backtrace line.

# File lib/honeybadger/backtrace.rb, line 29
def self.parse(unparsed_line, opts = {})
  filters = opts[:filters] || []
  filtered_line = filters.reduce(unparsed_line) do |line, proc|
    # TODO: Break if nil
    if proc.arity == 2
      proc.call(line, opts[:config])
    else
      proc.call(line)
    end
  end

  if filtered_line
    match = unparsed_line.match(INPUT_FORMAT) || [].freeze
    fmatch = filtered_line.match(INPUT_FORMAT) || [].freeze

    file, number, method = match[1], match[2], match[3]
    filtered_args = [fmatch[1], fmatch[2], fmatch[3]]
    new(file, number, method, *filtered_args, opts.fetch(:source_radius, 2))
  else
    nil
  end
end

Public Instance Methods

==(other) click to toggle source
# File lib/honeybadger/backtrace.rb, line 69
def ==(other)
  to_s == other.to_s
end
application?() click to toggle source

Determines if this line is part of the application trace or not.

# File lib/honeybadger/backtrace.rb, line 78
def application?
  (filtered_file =~ /^\[PROJECT_ROOT\]/i) && !(filtered_file =~ /^\[PROJECT_ROOT\]\/vendor/i)
end
inspect() click to toggle source
# File lib/honeybadger/backtrace.rb, line 73
def inspect
  "<Line:#{to_s}>"
end
source() click to toggle source
# File lib/honeybadger/backtrace.rb, line 82
def source
  @source ||= get_source(file, number, source_radius)
end
to_s() click to toggle source

Reconstructs the line in a readable fashion.

# File lib/honeybadger/backtrace.rb, line 65
def to_s
  "#{filtered_file}:#{filtered_number}:in `#{filtered_method}'"
end

Private Instance Methods

get_source(file, number, radius = 2) click to toggle source

Open source file and read line(s).

Returns

Returns an array of line(s) from source file.

# File lib/honeybadger/backtrace.rb, line 95
def get_source(file, number, radius = 2)
  if file && File.exist?(file)
    before = after = radius
    start = (number.to_i - 1) - before
    start = 0 and before = 1 if start <= 0
    duration = before + 1 + after

    l = 0
    File.open(file) do |f|
      start.times { f.gets ; l += 1 }
      return Hash[duration.times.map { (line = f.gets) ? [(l += 1), line] : nil }.compact]
    end
  else
    {}
  end
end