remove redundant code; decode response
This commit is contained in:
parent
5d9c8d59a0
commit
9efa7c116d
|
@ -17,7 +17,7 @@ def i_am_ready_to_die(request):
|
|||
raise ServerError("Something bad happened", status_code=500)
|
||||
```
|
||||
|
||||
You can also use the `abort` function with the correct status code:
|
||||
You can also use the `abort` function with the appropriate status code:
|
||||
|
||||
```python
|
||||
from sanic.exceptions import abort
|
||||
|
|
|
@ -142,25 +142,25 @@ class SanicException(Exception):
|
|||
|
||||
@add_status_code(404)
|
||||
class NotFound(SanicException):
|
||||
status_code = 404
|
||||
pass
|
||||
|
||||
|
||||
@add_status_code(400)
|
||||
class InvalidUsage(SanicException):
|
||||
status_code = 400
|
||||
pass
|
||||
|
||||
|
||||
@add_status_code(500)
|
||||
class ServerError(SanicException):
|
||||
status_code = 500
|
||||
pass
|
||||
|
||||
|
||||
class URLBuildError(SanicException):
|
||||
status_code = 500
|
||||
class URLBuildError(ServerError):
|
||||
pass
|
||||
|
||||
|
||||
class FileNotFound(NotFound):
|
||||
status_code = 404
|
||||
pass
|
||||
|
||||
def __init__(self, message, path, relative_url):
|
||||
super().__init__(message)
|
||||
|
@ -170,21 +170,21 @@ class FileNotFound(NotFound):
|
|||
|
||||
@add_status_code(408)
|
||||
class RequestTimeout(SanicException):
|
||||
status_code = 408
|
||||
pass
|
||||
|
||||
|
||||
@add_status_code(413)
|
||||
class PayloadTooLarge(SanicException):
|
||||
status_code = 413
|
||||
pass
|
||||
|
||||
|
||||
class HeaderNotFound(SanicException):
|
||||
status_code = 400
|
||||
class HeaderNotFound(InvalidUsage):
|
||||
pass
|
||||
|
||||
|
||||
@add_status_code(416)
|
||||
class ContentRangeError(SanicException):
|
||||
status_code = 416
|
||||
pass
|
||||
|
||||
def __init__(self, message, content_range):
|
||||
super().__init__(message)
|
||||
|
@ -209,5 +209,7 @@ def abort(status_code, message=None):
|
|||
if message is None:
|
||||
message = COMMON_STATUS_CODES.get(status_code,
|
||||
ALL_STATUS_CODES.get(status_code))
|
||||
# These are stored as bytes in the STATUS_CODES dict
|
||||
message = message.decode('utf8')
|
||||
sanic_exception = _sanic_exceptions.get(status_code, SanicException)
|
||||
raise sanic_exception(message=message, status_code=status_code)
|
||||
|
|
|
@ -3,7 +3,7 @@ from bs4 import BeautifulSoup
|
|||
|
||||
from sanic import Sanic
|
||||
from sanic.response import text
|
||||
from sanic.exceptions import InvalidUsage, ServerError, NotFound
|
||||
from sanic.exceptions import InvalidUsage, ServerError, NotFound, abort
|
||||
|
||||
|
||||
class SanicExceptionTestException(Exception):
|
||||
|
@ -33,7 +33,7 @@ def exception_app():
|
|||
@app.route('/abort')
|
||||
def handler_invalid(request):
|
||||
abort(500)
|
||||
raise InvalidUsage("OK")
|
||||
return text("OK")
|
||||
|
||||
@app.route('/divide_by_zero')
|
||||
def handle_unhandled_exception(request):
|
||||
|
@ -65,6 +65,7 @@ def test_catch_exception_list():
|
|||
request, response = app.test_client.get('/')
|
||||
assert response.text == 'ok'
|
||||
|
||||
|
||||
def test_no_exception(exception_app):
|
||||
"""Test that a route works without an exception"""
|
||||
request, response = exception_app.test_client.get('/')
|
||||
|
@ -78,10 +79,10 @@ def test_server_error_exception(exception_app):
|
|||
assert response.status == 500
|
||||
|
||||
|
||||
def test_abort(exception_app):
|
||||
def test_invalid_usage_exception(exception_app):
|
||||
"""Test the built-in InvalidUsage exception works"""
|
||||
request, response = exception_app.test_client.get('/abort')
|
||||
assert response.status == 500
|
||||
request, response = exception_app.test_client.get('/invalid')
|
||||
assert response.status == 400
|
||||
|
||||
|
||||
def test_not_found_exception(exception_app):
|
||||
|
@ -102,6 +103,7 @@ def test_handled_unhandled_exception(exception_app):
|
|||
"The server encountered an internal error and "
|
||||
"cannot complete your request.")
|
||||
|
||||
|
||||
def test_exception_in_exception_handler(exception_app):
|
||||
"""Test that an exception thrown in an error handler is handled"""
|
||||
request, response = exception_app.test_client.get(
|
||||
|
@ -126,3 +128,9 @@ def test_exception_in_exception_handler_debug_off(exception_app):
|
|||
debug=True)
|
||||
assert response.status == 500
|
||||
assert response.body.startswith(b'Exception raised in exception ')
|
||||
|
||||
|
||||
def test_abort(exception_app):
|
||||
"""Test the abort function"""
|
||||
request, response = exception_app.test_client.get('/abort')
|
||||
assert response.status == 500
|
||||
|
|
Loading…
Reference in New Issue
Block a user