In Pyramid-based website production environments, should generate a 404 NotFound Error instead of Server Error `URLDecodeError`.

Sunday, October 30, 2011

a unknown url always request my
Pyramid-based website server, it will be raise a URLDecodeError.


Traceback (most recent call last):
File "/root/env/lib/python2.6/site-packages/repoze.tm2-1.0b2-py2.6.egg/repoze/tm/", line 24, in __call__
    result = self.application(environ, save_status_and_headers)
File "/var/www/lxneng/src/lxneng/", line 27, in __call__
    return self.application(environ, start_response)
File "/root/env/lib/python2.6/site-packages/pyramid/", line 176, in __call__
    response = self.handle_request(request)
File "/root/env/lib/python2.6/site-packages/pyramid/", line 17, in excview_tween
    response = handler(request)
File "/root/env/lib/python2.6/site-packages/pyramid/", line 116, in handle_request
    tdict = traverser(request)
File "/root/env/lib/python2.6/site-packages/pyramid/", line 610, in __call__
    vpath_tuple = traversal_path(vpath)
File "/root/env/lib/python2.6/site-packages/repoze/lru/", line 96, in lru_cached
    val = f(*arg)
File "/root/env/lib/python2.6/site-packages/pyramid/", line 486, in traversal_path
    raise URLDecodeError(e.encoding, e.object, e.start, e.end, e.reason)
URLDecodeError: 'utf8' codec can't decode bytes in position 4-5: invalid data

so this should generate a 404 instead of a 500 internal server error.

@view_config(context='pyramid.exceptions.URLDecodeError', renderer='404.html')
@view_config(context='pyramid.exceptions.NotFound', renderer='404.html')
def error_view(context, request):
    return {}

This entry was tagged Pyramid and Python

comments powered by Disqus

© 2009-2013 All rights reserved. Powered by Pyramid

go to Top