#!/usr/bin/ruby # Author:: cho45(砂糖) # License:: Creative Commons by-nc require "fcgi" require "erb" require "cgi" $ERROR = DATA.read FCGI.each do |fcgi| $stdout = fcgi.out $stdin = fcgi.in ENV.replace(fcgi.env) begin Thread.start do Dir.chdir(File.dirname(fcgi.env["SCRIPT_FILENAME"])) load(fcgi.env["SCRIPT_FILENAME"], true) end.join rescue SystemExit # nothing to do rescue Exception => e @cgi = CGI.new puts @cgi.header({ "status" => "SERVER_ERROR", "type" => @cgi.user_agent.match(/IE/) ? "text/html" : "application/xhtml+xml" }) e.backtrace.slice!(-6..-1) e.backtrace.unshift(e.message) if e.class == SyntaxError backtrace = e.backtrace.collect do |l| CGI.escapeHTML(l) end lines = {} ERB.new($ERROR, $SAFE, ">").run(binding) end fcgi.finish end __END__ 500 Internal Server Error

500 Internal Server Error

Sorry. Error happened on processing your request.

Error

class: <%=CGI.escapeHTML(e.class.to_s)%> <%=e.class < StandardError ? " < StandardError" : "" %>

<%=CGI.escapeHTML(e.message).sub(/for/, "for
")%>

Backtrace
<% backtrace.each do |l| %> <% _, filename, linenum = */^(.+?):(\d+)/.match(l) %> <% lines[filename] ||= File.readlines(filename)%> <% linenum = linenum.to_i %>

<%=l%>

<% lines[filename].each_with_index do |l, n| n += 1 next if n < linenum - 5 break if n > linenum + 5 %> <% l = CGI.escapeHTML(l.gsub(/\t/, " ").gsub(/\n/, ""))%> <% if n == linenum %> <%="% 4d" % n%>: <%=l%> <% else %> <%="% 4d" % n%>: <%=l%> <% end %> <% end %>
<% end %>