Module: RorVsWild::Location

Included in:
Agent
Defined in:
lib/rorvswild/location.rb

Instance Method Summary collapse

Instance Method Details

#extract_most_relevant_file_and_line(locations) ⇒ Object



3
4
5
6
# File 'lib/rorvswild/location.rb', line 3

def extract_most_relevant_file_and_line(locations)
  location = find_most_relevant_location(locations)
  [relative_path(location.path), location.lineno]
end

#extract_most_relevant_file_and_line_from_array_of_strings(stack) ⇒ Object



25
26
27
28
29
# File 'lib/rorvswild/location.rb', line 25

def extract_most_relevant_file_and_line_from_array_of_strings(stack)
  location = stack.find { |str| str =~ app_root_regex && !(str =~ gem_home_regex) } if app_root_regex
  location ||= stack.find { |str| !(str =~ gem_home_regex) } if gem_home_regex
  relative_path(location || stack.first).split(":".freeze)
end

#extract_most_relevant_file_and_line_from_exception(exception) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rorvswild/location.rb', line 13

def extract_most_relevant_file_and_line_from_exception(exception)
  # Exception#backtrace_locations is faster but exists since 2.1.0.
  # Sometime Exception#backtrace_locations returns nil for an unknow reason. So we fallback to the old way.
  if exception.respond_to?(:backtrace_locations) && locations = exception.backtrace_locations
    extract_most_relevant_file_and_line(locations)
  elsif (backtrace = exception.backtrace) && backtrace.size > 0
    extract_most_relevant_file_and_line_from_array_of_strings(backtrace)
  else
    ["No backtrace".freeze, 1]
  end
end

#find_most_relevant_location(locations) ⇒ Object



8
9
10
11
# File 'lib/rorvswild/location.rb', line 8

def find_most_relevant_location(locations)
  result = locations.find { |l| l.path.index(app_root) == 0 && !(l.path =~ gem_home_regex) } if app_root
  result || locations.find { |l| !(l.path =~ gem_home_regex) } || locations.first
end

#gem_homeObject



35
36
37
# File 'lib/rorvswild/location.rb', line 35

def gem_home
  @gem_home ||= guess_gem_home
end

#gem_home_regexObject



31
32
33
# File 'lib/rorvswild/location.rb', line 31

def gem_home_regex
  @gem_home_regex ||= gem_home ? /\A#{gem_home}/.freeze : /\/gems\//.freeze
end

#guess_gem_homeObject



39
40
41
42
43
44
45
# File 'lib/rorvswild/location.rb', line 39

def guess_gem_home
  if ENV["GEM_HOME"] && !ENV["GEM_HOME"].empty?
    ENV["GEM_HOME"]
  elsif ENV["GEM_PATH"] && !(first_gem_path = ENV["GEM_PATH"].split(":").first)
    first_gem_path if first_gem_path && !first_gem_path.empty?
  end
end

#relative_path(path) ⇒ Object



47
48
49
# File 'lib/rorvswild/location.rb', line 47

def relative_path(path)
  app_root_regex ? path.sub(app_root_regex, "".freeze) : path
end