Class: SolidErrors::BacktraceLine

Inherits:
Object
  • Object
show all
Defined in:
app/models/solid_errors/backtrace_line.rb

Overview

Constant Summary collapse

INPUT_FORMAT =

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

%r{^((?:[a-zA-Z]:)?[^:]+):(\d+)(?::in [`']([^']+)')?$}
STRING_EMPTY =
"".freeze
GEM_ROOT =
"[GEM_ROOT]".freeze
PROJECT_ROOT =
"[PROJECT_ROOT]".freeze
PROJECT_ROOT_CACHE =
{}
GEM_ROOT_CACHE =
{}
RELATIVE_ROOT =
Regexp.new('^\.\/').freeze
RAILS_ROOT =
::Rails.root.to_s.dup.freeze
ROOT_REGEXP =
Regexp.new("^#{Regexp.escape(RAILS_ROOT)}").freeze
BACKTRACE_FILTERS =
[
  lambda { |line|
    return line unless defined?(Gem)
    GEM_ROOT_CACHE[line] ||= Gem.path.reduce(line) do |line, path|
      line.sub(path, GEM_ROOT)
    end
  },
  lambda { |line|
    c = (PROJECT_ROOT_CACHE[RAILS_ROOT] ||= {})
    return c[line] if c.has_key?(line)
    c[line] ||= line.sub(ROOT_REGEXP, PROJECT_ROOT)
  },
  lambda { |line| line.sub(RELATIVE_ROOT, STRING_EMPTY) }
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(unparsed_line, file, number, method, filtered_file = file, filtered_number = number, filtered_method = method, source_radius = 2) ⇒ BacktraceLine

Returns a new instance of BacktraceLine.



54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/models/solid_errors/backtrace_line.rb', line 54

def initialize(unparsed_line, file, number, method, filtered_file = file,
  filtered_number = number, filtered_method = method,
  source_radius = 2)
  self.unparsed_line = unparsed_line
  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

Instance Attribute Details

#fileObject

Returns the value of attribute file.



29
30
31
# File 'app/models/solid_errors/backtrace_line.rb', line 29

def file
  @file
end

#filtered_fileObject

Returns the value of attribute filtered_file.



32
33
34
# File 'app/models/solid_errors/backtrace_line.rb', line 32

def filtered_file
  @filtered_file
end

#filtered_methodObject

Returns the value of attribute filtered_method.



32
33
34
# File 'app/models/solid_errors/backtrace_line.rb', line 32

def filtered_method
  @filtered_method
end

#filtered_numberObject

Returns the value of attribute filtered_number.



32
33
34
# File 'app/models/solid_errors/backtrace_line.rb', line 32

def filtered_number
  @filtered_number
end

#methodObject

Returns the value of attribute method.



31
32
33
# File 'app/models/solid_errors/backtrace_line.rb', line 31

def method
  @method
end

#numberObject

Returns the value of attribute number.



30
31
32
# File 'app/models/solid_errors/backtrace_line.rb', line 30

def number
  @number
end

#unparsed_lineObject

Returns the value of attribute unparsed_line.



32
33
34
# File 'app/models/solid_errors/backtrace_line.rb', line 32

def unparsed_line
  @unparsed_line
end

Class Method Details

.parse(unparsed_line, opts = {}) ⇒ Object

Parses a single line of a given backtrace

Parameters:

  • unparsed_line (String)

    The raw line from caller or some backtrace.

Returns:

  • The parsed backtrace line.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/models/solid_errors/backtrace_line.rb', line 39

def self.parse(unparsed_line, opts = {})
  filtered_line = BACKTRACE_FILTERS.reduce(unparsed_line) do |line, proc|
    proc.call(line)
  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(unparsed_line, file, number, method, *filtered_args, opts.fetch(:source_radius, 2))
  end
end

Instance Method Details

#==(other) ⇒ Object



72
73
74
# File 'app/models/solid_errors/backtrace_line.rb', line 72

def ==(other)
  to_s == other.to_s
end

#application?Boolean

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

Returns:

  • (Boolean)


81
82
83
# File 'app/models/solid_errors/backtrace_line.rb', line 81

def application?
  (filtered_file =~ /^\[PROJECT_ROOT\]/i) && !(filtered_file =~ /^\[PROJECT_ROOT\]\/vendor/i)
end

#inspectObject



76
77
78
# File 'app/models/solid_errors/backtrace_line.rb', line 76

def inspect
  "<Line:#{self}>"
end

#sourceObject



85
86
87
# File 'app/models/solid_errors/backtrace_line.rb', line 85

def source
  @source ||= get_source(file, number, source_radius)
end

#to_sObject

Reconstructs the line in a readable fashion.



68
69
70
# File 'app/models/solid_errors/backtrace_line.rb', line 68

def to_s
  "#{filtered_file}:#{filtered_number}:in `#{filtered_method}'"
end