Class: Rnotifier::ExceptionCode

Inherits:
Object
  • Object
show all
Defined in:
lib/rnotifier/exception_code.rb

Constant Summary collapse

SYNTAX_ERROR_REGX =
/\A(.*:\d*):/

Class Method Summary collapse

Class Method Details

.find(filename, line_no, wrap_size = 1) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rnotifier/exception_code.rb', line 24

def find(filename, line_no, wrap_size = 1)
  s_range = [line_no - wrap_size, 1].max - 1
  e_range = line_no + wrap_size - 1
  code = [s_range]

  begin
    File.open(filename) do |f|
      f.each_with_index do |line, i|
        code << line if i >= s_range && i <= e_range
        break if i > e_range
      end
    end
  rescue Exception => e
  end
  code
end

.get(exception) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rnotifier/exception_code.rb', line 8

def get(exception)
  return unless exception.backtrace

  if exception.class == SyntaxError && m = exception.message.match(SYNTAX_ERROR_REGX)
    bline = m[1]
  else
    bline = exception.backtrace.find do |l| 
      l.index(Config.app_env[:app_root]) == 0 && !Gem.path.any?{|path| l.index(path) == 0}
    end
  end

  filename, line, method = (bline || exception.backtrace[0]).split(':')

  self.find(filename, line.to_i, 3)
end