Class: CGIExceptionPrinter

Inherits:
Object
  • Object
show all
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

ModRubyExceptionPrinter

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#outObject

Returns the value of attribute out.



15
16
17
# File 'lib/cgi_exception.rb', line 15

def out
  @out
end

#skip_headerObject

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(/&/,'&amp;').gsub(/</,'&lt;').gsub(/>/,'&gt;').gsub(/"/,'&quot;')
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 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.message)} (#{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 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