diff options
author | Sebastian Spaeth <Sebastian@SSpaeth.de> | 2012-12-23 11:57:45 +0100 |
---|---|---|
committer | Sebastian Spaeth <Sebastian@SSpaeth.de> | 2012-12-23 11:57:45 +0100 |
commit | 785b287fcb42ac9130b222006097e3f68cec3543 (patch) | |
tree | 686f11a1bcb951af8150292f92a404d16dac7907 /mediagoblin/tools/response.py | |
parent | 511d5b89664daef455da0579e8c8658e5b81cce5 (diff) | |
download | mediagoblin-785b287fcb42ac9130b222006097e3f68cec3543.tar.lz mediagoblin-785b287fcb42ac9130b222006097e3f68cec3543.tar.xz mediagoblin-785b287fcb42ac9130b222006097e3f68cec3543.zip |
Provide tools.response.render_http_exception and use that
After the webob->werkzeug transition, controller functions can raise
werkzeug.HttpExceptions. We need to catch these in app.py when calling
the controller and handle them, rendering the corresponding error Response()
object. For consistency, we also want to allow meddleware functions to
raise HttpExceptions (e.g. the csrf meddleware needs to complain about lack
of cookies), so wrap the request and response parts of the meddleware too.
Finally, the urlmap.match() can also raise HttpExceptions, so we give it the
same treatment (render_http_exception). I am not sure, if we do not need to
handle the Redirect exception there in any different way though...
The new function render_http_exception makes use of the render_error infrastructure
to return a nicely templated error page. It also checks if the stock error
messages was used in cases where we have localizations (403, 404) and use those.
It is now possible to do things like "raise Forbidden(_('You suckr'))" or
raise NotFound(_('where is my left show again')) if you want to return
customized error messages to the user.
Signed-off-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
Diffstat (limited to 'mediagoblin/tools/response.py')
-rw-r--r-- | mediagoblin/tools/response.py | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/mediagoblin/tools/response.py b/mediagoblin/tools/response.py index b02dd6b5..80df1f5a 100644 --- a/mediagoblin/tools/response.py +++ b/mediagoblin/tools/response.py @@ -63,6 +63,25 @@ def render_404(request): return render_error(request, 404, err_msg=err_msg) +def render_http_exception(request, exc, description): + """Return Response() given a werkzeug.HTTPException + + :param exc: werkzeug.HTTPException or subclass thereof + :description: message describing the error.""" + # If we were passed the HTTPException stock description on + # exceptions where we have localized ones, use those: + stock_desc = (description == exc.__class__.description) + + if stock_desc and exc.code == 403: + return render_403(request) + elif stock_desc and exc.code == 404: + return render_404(request) + + return render_error(request, title=exc.args[0], + err_msg=description, + status=exc.code) + + def redirect(request, *args, **kwargs): """Redirects to an URL, using urlgen params or location string |