spyce
         
home     documentation     download     Spyce logo


Documentation - Modules

Prev: 3.8.11 - Internal modules Up: 3.8.11 - Internal modules Next: 3.8.11.2 - Stdout

The error module is implicitly loaded and provides error-handling functionality. An error is any unhandled runtime exception that occurs during Spyce processing. This mechanism does not include exceptions that are not related to Spyce processing (i.e. server-related exceptions), that can be caused before or after Spyce processing by invalid syntax, missing files and file access restrictions. To install a server-level error handler use a configuration file. The default page-level error handler can also be modified in the configuration file. This module allows the user to install page-level error handling code, overriding the default page-level handler, by using one of the following functions:

  • setStringHandler( string ):
    Installs a function that will processes the given string, as Spyce code, for error handling.
  • setFileHandler( uri ):
    Installs a function that will processes the given uri for error handling.
  • setHandler( fn ):
    Installs the fn function for error handling. The function is passed one parameter, a reference to the error module. From this, all the error information as well as references to other modules and Spyce objects can be accessed.
The error module provides the following information about an error:

  • isError():
    Returns whether an error is being handled.
  • getMessage():
    Return the error message; the string of the object that was raised, or None if there is no current error.
  • getType():
    Return the error type; the type of the object that was raised, or None if there is no current error.
  • getFile():
    Return the file where the error was raised, or None if there is no current error.
  • getTraceback():
    Return the stack trace as an array of tuples, or None if there is no current error. Each tuple entry is of the form: (file, line numbers, function name, code context).
  • getString():
    Return the string of the entire error (the string representation of the message, type, location and stack trace), or None if there is no current error.
The default error handling function uses the following string handler:

[[.module name=transform]]
[[transform.expr('html_encode')]]
<html>
<title>Spyce exception: [[=error.getMessage()]]</title>
<body>
<table cellspacing=10 border=0>
  <tr><td colspan=2><h1>Spyce exception</h1></td></tr>
  <tr><td valign=top align=right><b>File:</b></td><td>[[=error.getFile()]]</tr>
  <tr><td valign=top align=right><b>Message:</b></td>
    <td><pre>[[=error.getMessage()]]</pre></tr>
  <tr><td valign=top align=right><b>Stack:</b></td><td>
    [[\
      L = list(error.getTraceback())
      L.reverse()
    ]]
    [[ for frame in L: { ]]
      [[=frame[0] ]]:[[=frame[1] ]], in [[=frame[2] ]]:<br>
      <table border=0><tr><td width=10></td><td>
        <pre>[[=frame[3] ]]</pre>
      </td></tr></table>
    [[ } ]]
    </td></tr>
</table>
</body></html>

The example below shows the error module in use. Error handling can often be used to send emails notifying webmasters of problems, as this example shows.

examples/error.spy
[[error.setFileHandler('error.spi') ]]
This is a page with an error...
[[ raise 'an error' ]]
Run this code

examples/error.spi
<h1>Oops</h1>
An error occurred while processing your request. 
We have logged this for our webmasters, and they 
will fix it shortly. We apologize for the inconvenience.
In the meantime, please use the parts of our site that 
actually do work... <a href="somewhere">somewhere</a>.
[[\
  # could redirect the user immediately
  #response.getModule('redirect').external('somewhere.spy')

  # could send an email
  import time
  msg = '''
time: %s
error: %s
env: %s
other info...
''' % (
    time.asctime(time.localtime(time.time())), 
    error.getString(),
    request.env()
  )
  #function_to_send_email('webmaster@foo.com', msg)

  #or perform other generic error handling...
]]

This mechanism is not a subsititute for proper exception handling within the code itself, and should not be abused. It does, however, serve as a useful catch-all for bugs that slip through the cracks.


Prev: 3.8.11 - Internal modules Up: 3.8.11 - Internal modules Next: 3.8.11.2 - Stdout


Spyce logo
Python Server Pages
version 2.1.3
Spyce Powered SourceForge Logo