Class: CGIExceptionPrinter
- Inherits:
-
Object
- Object
- CGIExceptionPrinter
- Defined in:
- lib/cgi_exception.rb
Overview
$Rev: 16 $ $Release: 0.3.0 $ copyright© 2007-2008 kuwata-lab.com all rights reserved. License: public domain
Direct Known Subclasses
Instance Attribute Summary collapse
-
#out ⇒ Object
Returns the value of attribute out.
-
#skip_header ⇒ Object
Returns the value of attribute skip_header.
Instance Method Summary collapse
-
#escape_html(s) ⇒ Object
(also: #h)
escape HTML characters.
- #handle(exception) ⇒ Object
-
#initialize(skip_header = false, out = $stdout) ⇒ CGIExceptionPrinter
constructor
A new instance of CGIExceptionPrinter.
-
#print_exception(ex) ⇒ Object
print exception in HTML format.
-
#print_header ⇒ Object
print http header.
Constructor Details
#initialize(skip_header = false, out = $stdout) ⇒ CGIExceptionPrinter
Returns a new instance of CGIExceptionPrinter.
10 11 12 13 |
# File 'lib/cgi_exception.rb', line 10 def initialize(skip_header=false, out=$stdout) @skip_header = skip_header @out = out end |
Instance Attribute Details
#out ⇒ Object
Returns the value of attribute out.
15 16 17 |
# File 'lib/cgi_exception.rb', line 15 def out @out end |
#skip_header ⇒ Object
Returns the value of attribute skip_header.
15 16 17 |
# File 'lib/cgi_exception.rb', line 15 def skip_header @skip_header end |
Instance Method Details
#escape_html(s) ⇒ Object Also known as: h
escape HTML characters
18 19 20 |
# File 'lib/cgi_exception.rb', line 18 def escape_html(s) s.to_s.gsub(/&/,'&').gsub(/</,'<').gsub(/>/,'>').gsub(/"/,'"') end |
#handle(exception) ⇒ Object
50 51 52 53 |
# File 'lib/cgi_exception.rb', line 50 def handle(exception) print_header() unless @skip_header print_exception(exception) end |
#print_exception(ex) ⇒ Object
print exception in HTML format
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/cgi_exception.rb', line 33 def print_exception(ex) arr = ex.backtrace @out.print "<pre style=\"color:#CC0000\">" @out.print "<b>#{h(arr[0])}: #{h(ex.)} (#{h(ex.class.name)})</b>\n" block = proc {|s| @out.print " from #{h(s)}\n" } max = 20 if arr.length <= max arr[1..-1].each(&block) else n = 5 arr[1..(max-n)].each(&block) @out.print " ...\n" arr[-n..-1].each(&block) end @out.print "</pre>" end |
#print_header ⇒ Object
print http header
25 26 27 28 29 30 |
# File 'lib/cgi_exception.rb', line 25 def print_header() @out.print "Status: 500 Internal Error\r\n" @out.print "Content-Type: text/html\r\n" @out.print "X-CGI-Exception: 0.3.0\r\n" @out.print "\r\n" end |