diff options
| author | Armin Ronacher <armin.ronacher@active-4.com> | 2017-12-27 16:52:04 +0100 |
|---|---|---|
| committer | Armin Ronacher <armin.ronacher@active-4.com> | 2017-12-27 16:52:04 +0100 |
| commit | fdd9b72889845a4848f298d5e3684a4e5784709f (patch) | |
| tree | 7f527d4f9c8ff52ec365a26aa04dd40218165d0b | |
| parent | eb066b71263bce2602afc399f011d9b404dbdd79 (diff) | |
| download | werkzeug-feature/wrapper-exceptions.tar.gz | |
Catch HTTP exceptions in Request.applicationfeature/wrapper-exceptions
| -rw-r--r-- | CHANGES.rst | 3 | ||||
| -rw-r--r-- | tests/test_wrappers.py | 21 | ||||
| -rw-r--r-- | werkzeug/wrappers.py | 10 |
3 files changed, 31 insertions, 3 deletions
diff --git a/CHANGES.rst b/CHANGES.rst index f498b372..0a7f6dae 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -4,7 +4,8 @@ Werkzeug Changelog Version 0.14 ------------ -Unreleased +- HTTP execptions are now automatically caught by + ``Request.application``. Version 0.13 diff --git a/tests/test_wrappers.py b/tests/test_wrappers.py index 2d68b784..33a39df7 100644 --- a/tests/test_wrappers.py +++ b/tests/test_wrappers.py @@ -21,7 +21,8 @@ from werkzeug._compat import iteritems from tests import strict_eq from werkzeug import wrappers -from werkzeug.exceptions import SecurityError, RequestedRangeNotSatisfiable +from werkzeug.exceptions import SecurityError, RequestedRangeNotSatisfiable, \ + BadRequest from werkzeug.wsgi import LimitedStream, wrap_file from werkzeug.datastructures import MultiDict, ImmutableOrderedMultiDict, \ ImmutableList, ImmutableTypeConversionDict, CharsetAccept, \ @@ -220,6 +221,24 @@ def test_stream_only_mixing(): strict_eq(request.stream.read(), b'foo=blub+hehe') +def test_request_application(): + @wrappers.Request.application + def application(request): + return wrappers.Response('Hello World!') + + @wrappers.Request.application + def failing_application(request): + raise BadRequest() + + resp = wrappers.Response.from_app(application, create_environ()) + assert resp.data == b'Hello World!' + assert resp.status_code == 200 + + resp = wrappers.Response.from_app(failing_application, create_environ()) + assert b'Bad Request' in resp.data + assert resp.status_code == 400 + + def test_base_response(): # unicode response = wrappers.BaseResponse(u'öäü') diff --git a/werkzeug/wrappers.py b/werkzeug/wrappers.py index f89241dd..5e28bec9 100644 --- a/werkzeug/wrappers.py +++ b/werkzeug/wrappers.py @@ -288,6 +288,9 @@ class BaseRequest(object): def my_wsgi_app(request): return Response('Hello World!') + As of Werkzeug 0.14 HTTP exceptions are automatically caught and + converted to responses instead of failing. + :param f: the WSGI callable to decorate :return: a new WSGI callable """ @@ -296,10 +299,15 @@ class BaseRequest(object): #: the request. The return value is then called with the latest #: two arguments. This makes it possible to use this decorator for #: both methods and standalone WSGI functions. + from werkzeug.exceptions import HTTPException def application(*args): request = cls(args[-2]) with request: - return f(*args[:-2] + (request,))(*args[-2:]) + try: + resp = f(*args[:-2] + (request,)) + except HTTPException as e: + resp = e.get_response(args[-2]) + return resp(*args[-2:]) return update_wrapper(application, f) def _get_file_stream(self, total_content_length, content_type, filename=None, |
