public
Description: TextMate bundle for Ruby Shoulda gem/Rails plugin
Homepage: http://thoughtbot.com/projects/shoulda
Clone URL: git://github.com/drnic/ruby-shoulda-tmbundle.git
ruby-shoulda-tmbundle / Support / RubyMate / catch_exception.rb
100644 40 lines (30 sloc) 1.282 kb
1
2
3
4
5
6
7
8
9
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
STDOUT.sync = true
STDERR.sync = true
 
require 'pathname'
 
at_exit do
  if (e = $!) && !e.instance_of?(SystemExit)
    require "#{ENV['TM_SUPPORT_PATH']}/lib/escape"
    require "cgi"
    io = IO.for_fd(ENV['TM_ERROR_FD'].to_i)
 
    io.write "<div id='exception_report' class='framed'>\n"
    io.write "<p id='exception'><strong>#{e.class.name}:</strong> #{CGI.escapeHTML e.message.sub(/`(\w+)'/, '‘\1’').sub(/ -- /, ' — ')}</p>\n"
 
    io.write "<blockquote><table border='0' cellspacing='0' cellpadding='0'>\n"
 
    e.backtrace.each do |b|
      if b =~ /(.*?):(\d+)(?::in\s*`(.*?)')?/ then
        file, line, method = $1, $2, $3
 
        url, display_name = '', 'untitled document';
        if file != '-' && File.exists?(file) then
          file = Pathname.new(file).realpath.to_s
          url = '&url=file://' + e_url(file)
          display_name = File.basename(file)
        end
          
        io << "<tr><td><a class='near' href='txmt://open?line=#{line + url}'>"
        io << (method ? "method #{CGI::escapeHTML method}" : '<em>at top level</em>')
        io << "</a></td>\n<td>in <strong>#{CGI::escapeHTML display_name}</strong> at line #{line}</td></tr>\n"
      end
    end
    
    io.write "</table></blockquote></div>"
    io.flush
 
    exit!
  end
end