From 2848d7c80ee2c8644b347666a34a0fb0114a7894 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20KUBLER?= Date: Fri, 23 Jun 2017 16:44:57 +0200 Subject: [PATCH] Added a Forbidden exception Also added a small test. --- sanic/exceptions.py | 5 +++++ tests/test_exceptions.py | 13 ++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/sanic/exceptions.py b/sanic/exceptions.py index e1136dd1..7a104122 100644 --- a/sanic/exceptions.py +++ b/sanic/exceptions.py @@ -194,6 +194,11 @@ class ContentRangeError(SanicException): } +@add_status_code(403) +class Forbidden(SanicException): + pass + + class InvalidRangeType(ContentRangeError): pass diff --git a/tests/test_exceptions.py b/tests/test_exceptions.py index a2b8dc71..955a9ab4 100644 --- a/tests/test_exceptions.py +++ b/tests/test_exceptions.py @@ -3,7 +3,8 @@ from bs4 import BeautifulSoup from sanic import Sanic from sanic.response import text -from sanic.exceptions import InvalidUsage, ServerError, NotFound, abort +from sanic.exceptions import InvalidUsage, ServerError, NotFound, Forbidden +from sanic.exceptions import abort class SanicExceptionTestException(Exception): @@ -26,6 +27,10 @@ def exception_app(): def handler_404(request): raise NotFound("OK") + @app.route('/403') + def handler_403(request): + raise Forbidden("Forbidden") + @app.route('/invalid') def handler_invalid(request): raise InvalidUsage("OK") @@ -91,6 +96,12 @@ def test_not_found_exception(exception_app): assert response.status == 404 +def test_forbidden_exception(exception_app): + """Test the built-in Forbidden exception""" + request, response = exception_app.test_client.get('/403') + assert response.status == 403 + + def test_handled_unhandled_exception(exception_app): """Test that an exception not built into sanic is handled""" request, response = exception_app.test_client.get('/divide_by_zero')