TypeError:当应该生成pagenotfound时,'JsonObject'对象是unsubscriptable

时间:2011-05-10 13:56:50

标签: python pylons

当我发现内部Pylons:python错误时,我应该得到一个整洁的“找不到页面”页面时,我得到了这个丑陋的“内部服务器错误”。

我在其他地方模拟过Python错误&生成正确的pagenotfound模板。在我的配置(development.ini)中:debug = false,full_stack = true ...以便在发生异常时呈现此页面。

这就是middleware.py当前的样子:

"""Pylons middleware initialization"""
from beaker.middleware import CacheMiddleware, SessionMiddleware
from paste.cascade import Cascade
from paste.registry import RegistryManager
from paste.urlparser import StaticURLParser
from paste.deploy.converters import asbool
from pylons import config
from pylons.middleware import ErrorHandler, StatusCodeRedirect
from observer import Observer
from pylons.wsgiapp import PylonsApp
from routes.middleware import RoutesMiddleware
from r4l.lib.components.middleware.striptrailingslash import     StripTrailingSlash
from r4l.config.environment import load_environment

def make_app(global_conf, full_stack=True, static_files=True, **app_conf):
"""
Create a Pylons WSGI application and return it

``global_conf``
    The inherited configuration for this application. Normally from
    the [DEFAULT] section of the Paste ini file.

``full_stack``
    Whether this application provides a full WSGI stack (by default,
    meaning it handles its own exceptions and errors). Disable
    full_stack when this application is "managed" by another WSGI
    middleware.

``static_files``
    Whether this application serves its own static files; disable
    when another web server is responsible for serving them.

``app_conf``
    The application's local configuration. Normally specified in
    the [app:<name>] section of the Paste ini file (where <name>
    defaults to main).

"""
# Configure the Pylons environment
load_environment(global_conf, app_conf)

# The Pylons WSGI app
app = PylonsApp()

# Routing/Session/Cache Middleware
app = RoutesMiddleware(app, config['routes.map'])
app = SessionMiddleware(app, config)
app = CacheMiddleware(app, config)

# CUSTOM MIDDLEWARE HERE (filtered by error handling middlewares)

if asbool(full_stack):
    # Handle Python exceptions
    app = ErrorHandler(app, global_conf, **config['pylons.errorware'])
    app = Observer(app)

    # Display error documents for 401, 403, 404 status codes (and
    # 500 when debug is disabled)
    if asbool(config['debug']):
        app = StatusCodeRedirect(app, [400, 401, 403, 404], '/content/pagenotfound')
    else:
        app = StatusCodeRedirect(app, [400, 401, 403, 404, 500], '/content/pagenotfound')

# Establish the Registry for this application
app = RegistryManager(app)

if asbool(static_files):
    # Serve static files
    static_app = StaticURLParser(config['pylons.paths']['static_files'])
    app = Cascade([static_app, app])

app = StripTrailingSlash(app)
return app

还有Observer.py类

from pylons.util import call_wsgi_application
from webob import Request, Response
from weberror import formatter, collector

class Observer(object):
""" Observer object to monitor and extract the traceback for 'pagenotfound' emails """

def __init__(self, app):
    self.app = app

def __call__(self, environ, start_response):        
    status, headers, app_iter, exc_info = call_wsgi_application(
        self.app, environ, catch_exc_info=True)
    if exc_info is not None:
        exc_data = collector.collect_exception(*exc_info)
        err_report = formatter.format_text(exc_data, show_hidden_frames=True)[0]
        environ['err_report'] = err_report
    start_response(status, headers, exc_info)
    return app_iter

这是Error.py

import cgi

from paste.urlparser import PkgResourcesParser
from pylons import request
from pylons.controllers.util import forward
from pylons.middleware import error_document_template
from webhelpers.html.builder import literal

from r4l.lib.base import BaseController
from r4l.lib import helpers as h

class ErrorController(BaseController):

"""Generates error documents as and when they are required.

The ErrorDocuments middleware forwards to ErrorController when error
related status codes are returned from the application.

This behaviour can be altered by changing the parameters to the
ErrorDocuments middleware in your config/middleware.py file.

"""

def document(self):
    """Render the error document"""
    resp = request.environ.get('pylons.original_response')
    if resp.status_int == 404:
        h.redirect_to('/content/pagenotfound')
    content = literal(resp.body) or cgi.escape(request.GET.get('message', ''))
    page = error_document_template % \
        dict(prefix=request.environ.get('SCRIPT_NAME', ''),
             code=cgi.escape(request.GET.get('code', str(resp.status_int))),
             message=content)
    return page

def img(self, id):
    """Serve Pylons' stock images"""
    return self._serve_file('/'.join(['media/img', id]))

def style(self, id):
    """Serve Pylons' stock stylesheets"""
    return self._serve_file('/'.join(['media/style', id]))

def _serve_file(self, path):
    """Call Paste's FileApp (a WSGI application) to serve the file
    at the specified path
    """
    request.environ['PATH_INFO'] = '/%s' % path
    return forward(PkgResourcesParser('pylons', 'pylons'))

最后控制器为pagenotfound func

@h.template(u'content/404')
def pagenotfound(self):
    """
    Default pagenotfound page
    """

    c.err_report = str(request.environ.get('pylons.original_response','')) + '\n\n'
    if 'err_report' in request.environ:
        c.err_report += request.environ.get('err_report','')
        log.info("###### c.err_report: %s" % c.err_report)
    c.page_title = u'Page Not Found'
    c.previous_url = request.environ.get('HTTP_REFERER', h.url_for('/'))
    pagenotfound = True
    session['pagenotfound'] = pagenotfound
    session.save()
    if c.login:
        user = Session.query(User).get(session[u'username'])
        if user:
            c.login.email = user.contactemail.value

这是未被捕获的例外:

  

文件'/home/chilton/work/cj2/CJ-7519/r4l/templates/resume/preview.html',第441行     

$ {h.cj_month_year(employment.startdate)} - 当前$ {employment.enddate.strftime('%m /%Y')}
  ValueError:year = 200在1900之前; datetime strftime()方法需要年份> = 1900

我尝试过不同的解决方法,但没有成功。

0 个答案:

没有答案