Class: Cyberweb::CGI::Exception

Inherits:
Object
  • Object
show all
Defined in:
lib/cyberweb/cgi/exceptions.rb

Overview

Cyberweb::CGI::Exception

Direct Known Subclasses

InvalidEncoding, ModRubyExceptionPrinter

Constant Summary collapse

CONTENT_TYPE =
#

CONTENT_TYPE

Some useful constants.

#
'Content-Type: text/html'+LINE_FEED
TOP_DIV_ELEMENT =
#

TOP_DIV_ELEMENT

#
'<div style="padding:0.5em; margin: 12x; color:#CC0000">'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(skip_header = false, stdout = $stdout) ⇒ Exception

#

initialize

The second argument will keep a pointer to stdout.

#


57
58
59
60
61
62
63
# File 'lib/cyberweb/cgi/exceptions.rb', line 57

def initialize(
    skip_header = false,
    stdout      = $stdout
  )
  @skip_header = skip_header
  @out = stdout # A pointer towards stdout.
end

Instance Attribute Details

#outObject

Returns the value of attribute out.



38
39
40
# File 'lib/cyberweb/cgi/exceptions.rb', line 38

def out
  @out
end

Instance Method Details

#escape_html(i) ⇒ Object

#

escape_html

We escape html characters here.

#


150
151
152
153
154
155
# File 'lib/cyberweb/cgi/exceptions.rb', line 150

def escape_html(i)
  i.to_s.gsub(/&/,'&amp;').
         gsub(/</,'&lt;').
         gsub(/>/,'&gt;').
         gsub(/"/,'&quot;')
end

#handle(exception) ⇒ Object

#

handle

#


140
141
142
143
# File 'lib/cyberweb/cgi/exceptions.rb', line 140

def handle(exception)
  print_header unless skip_header?
  print_exception(exception)
end
#

print_exception

This method will print the exception in a valid HTML format.

The argument is expected to be an Exception object - in other words, it must respond to the method .backtrace().

#


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/cyberweb/cgi/exceptions.rb', line 89

def print_exception(exception)
  array = exception.backtrace # Grab the backtrack-array here.
  @out.print TOP_DIV_ELEMENT # Open the <pre> tag here.
  # ======================================================================= #
  # The specific error message will be bold, with the <b> tag:
  # ======================================================================= #
  _ = "<b>
  #{::Cyberweb.html_escape(array.first)}: #{::Cyberweb.html_escape(exception.message)} (#{::Cyberweb.html_escape(exception.class.name)})
  </b>\n"+BR
  @out.print _ # Print this to STDOUT.
  # ======================================================================= #
  # Iterate over our block next.
  # ======================================================================= #
  block = proc {|s|
    _ = '<span style="padding-left:4em">from <b>'+::Cyberweb.html_escape(s)+'</b></span>'+N+BR
    @out.print _
  }
  max = 20 # How many lines of the Array we will display.
  if array.length <= max
    array[1..-1].each(&block)
  else
    n = 5 # Display the last 5 lines.
    array[1..(max-n)].each(&block)
    @out.print "           ...\n"
    array[-n..-1].each(&block)
  end
  @out.print '</div>' # Close the <div> tag again.
end
#

print_header

We print the HTTP header.

#


123
124
125
126
127
128
# File 'lib/cyberweb/cgi/exceptions.rb', line 123

def print_header
  @out.print 'Status: 500 Internal Error'+LINE_FEED
  @out.print CONTENT_TYPE
  @out.print 'X-CGI-Exception: 0.4.0'+LINE_FEED
  @out.print LINE_FEED
end

#skip_headerBoolean

#

skip_header?

#

skip_header

Returns:

  • (Boolean)


70
71
72
# File 'lib/cyberweb/cgi/exceptions.rb', line 70

def skip_header?
  @skip_header
end

#skip_header=(i) ⇒ Object

#

skip_header=

Whether we will skip the header or whether we will not.

#


77
78
79
# File 'lib/cyberweb/cgi/exceptions.rb', line 77

def skip_header=(i)
  @skip_header = i
end

#skip_header?Boolean

#

skip_header?

#

Returns:

  • (Boolean)


68
69
70
# File 'lib/cyberweb/cgi/exceptions.rb', line 68

def skip_header?
  @skip_header
end