Ignore:
Timestamp:
Sep 28, 2009, 3:04:33 AM (15 years ago)
Author:
quentin
Message:

Full error handling

File:
1 edited

Legend:

Unmodified
Added
Removed
  • package_branches/invirt-web/cherrypy/code/view.py

    r2482 r2485  
    1 import os
     1import os, sys
    22
    33import cherrypy
     
    66import simplejson
    77import datetime, decimal
     8from StringIO import StringIO
    89from invirt.config import structs as config
    910from webcommon import State
     
    2829    def __init__(self):
    2930        self.lookups = {}
    30    
    31     def __call__(self, filename, directories, module_directory=None,
    32                  collection_size=-1, content_type='text/html; charset=utf-8',
    33                  imports=[]):
     31
     32    def get_lookup(self, directories, module_directory=None,
     33                     collection_size=-1, imports=[], **kwargs):
    3434        # Find the appropriate template lookup.
    3535        key = (tuple(directories), module_directory)
     
    4646                                    )
    4747            self.lookups[key] = lookup
    48         cherrypy.request.lookup = lookup
     48        return lookup
     49
     50    def __call__(self, filename, directories, module_directory=None,
     51                 collection_size=-1, content_type='text/html; charset=utf-8',
     52                 imports=[]):
     53        cherrypy.request.lookup = lookup = self.get_lookup(directories, module_directory,
     54                                                           collection_size, imports)
    4955       
    5056        # Replace the current handler.
     
    5460main = MakoLoader()
    5561cherrypy.tools.mako = cherrypy.Tool('on_start_resource', main)
     62
     63def revertStandardError():
     64    """Move stderr to stdout, and return the contents of the old stderr."""
     65    errio = sys.stderr
     66    if not isinstance(errio, StringIO):
     67        return ''
     68    sys.stderr = sys.stdout
     69    errio.seek(0)
     70    return errio.read()
     71
     72def catchStderr():
     73    old_handler = cherrypy.request.handler
     74    def wrapper(*args, **kwargs):
     75        sys.stderr = StringIO()
     76        ret = old_handler(*args, **kwargs)
     77        e = revertStandardError()
     78        if e:
     79            if isinstance(ret, dict):
     80                ret["error_text"] = e
     81        return ret
     82    if old_handler:
     83        cherrypy.request.handler = wrapper
     84
     85cherrypy.tools.catch_stderr = cherrypy.Tool('before_handler', catchStderr)
    5686
    5787class JSONEncoder(simplejson.JSONEncoder):
Note: See TracChangeset for help on using the changeset viewer.