Class: Kiss::ExceptionReport

Inherits:
Object
  • Object
show all
Defined in:
lib/kiss/exception_report.rb

Overview

Generates HTML reports on exceptions raised from the app, showing the backtrace with clickable stack frames with TextMate links to source files, plus login hash, last SQL, GET/POST params, cookies, and Rack environment variables.

Constant Summary collapse

@@context =
7

Class Method Summary collapse

Class Method Details

.generate(exception, env = nil, cache = nil, sql = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/kiss/exception_report.rb', line 10

def generate(exception, env = nil, cache = nil, sql = nil)
  if env
    req = Rack::Request.new(env)
    path = (req.script_name + req.path_info).squeeze("/")
    url = (req.scheme + '://' + req.host + path) rescue 'n/a'
  end

  backtrace = exception.backtrace
  
  frames = backtrace.map { |line|
    frame = {}
    if line =~ /(.*?):(\d+)(:in `(.*)')?/
      frame.filename = $1
      frame.lineno = $2.to_i
      frame.function = $4
      lines = nil
      
      begin
        lineno = frame.lineno-1
        lines ||= ::File.readlines(frame.filename)
        frame.pre_context_lineno = [lineno-@@context, 0].max
        frame.pre_context = lines[frame.pre_context_lineno...lineno]
        frame.context_line = lines[lineno].chomp
        frame.post_context_lineno = [lineno+@@context, lines.size].min
        frame.post_context = lines[lineno+1..frame.post_context_lineno]
      rescue
      end
      
      frame
    else
      nil
    end
  }.compact
  
  i = 0
  while true do
    break unless (frames[i].filename =~ /\/lib\/kiss(\/|\.rb)/) || (frames[i].filename =~ /\/lib\/sequel/)
    i += 1
  end
  toggle_frame = frames[i] || frames[0]
  
  if env
    env["rack.errors"].puts "#{exception.class}: #{exception.message}"
    env["rack.errors"].puts exception.backtrace.map { |l| "\t" + l }
    env["rack.errors"].flush
  end
  
  db_query = begin (Sequel::MySQL::Database.last_query) rescue nil end
  
  @@erubis ||= Erubis::Eruby.new(template)
  begin
    @@erubis.result(binding)
  rescue => e
    gdebug e.message
  end
end

.h(str) ⇒ Object



75
76
77
# File 'lib/kiss/exception_report.rb', line 75

def h(str)
  str.html_escape
end

.templateObject

:stopdoc:



81
82
83
# File 'lib/kiss/exception_report.rb', line 81

def template
  @@template ||= File.read( template_dir + '/exception_report.html' )
end

.template_dirObject



85
86
87
# File 'lib/kiss/exception_report.rb', line 85

def template_dir
  @@template_dir = File.dirname(__FILE__) + '/html'
end

.textmate_href(frame) ⇒ Object



67
68
69
# File 'lib/kiss/exception_report.rb', line 67

def textmate_href(frame)
  "txmt://open?url=file://"+(h(Kiss.absolute_path(frame.filename)))+"&line="+(h frame.lineno)
end

.w(str) ⇒ Object



71
72
73
# File 'lib/kiss/exception_report.rb', line 71

def w(str)
  str.gsub(/([;\/])/, '\1<wbr/>')
end