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)
|
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
|
```python
|
||||||
from sanic.exceptions import abort
|
from sanic.exceptions import abort
|
||||||
|
|
|
@ -142,25 +142,25 @@ class SanicException(Exception):
|
||||||
|
|
||||||
@add_status_code(404)
|
@add_status_code(404)
|
||||||
class NotFound(SanicException):
|
class NotFound(SanicException):
|
||||||
status_code = 404
|
pass
|
||||||
|
|
||||||
|
|
||||||
@add_status_code(400)
|
@add_status_code(400)
|
||||||
class InvalidUsage(SanicException):
|
class InvalidUsage(SanicException):
|
||||||
status_code = 400
|
pass
|
||||||
|
|
||||||
|
|
||||||
@add_status_code(500)
|
@add_status_code(500)
|
||||||
class ServerError(SanicException):
|
class ServerError(SanicException):
|
||||||
status_code = 500
|
pass
|
||||||
|
|
||||||
|
|
||||||
class URLBuildError(SanicException):
|
class URLBuildError(ServerError):
|
||||||
status_code = 500
|
pass
|
||||||
|
|
||||||
|
|
||||||
class FileNotFound(NotFound):
|
class FileNotFound(NotFound):
|
||||||
status_code = 404
|
pass
|
||||||
|
|
||||||
def __init__(self, message, path, relative_url):
|
def __init__(self, message, path, relative_url):
|
||||||
super().__init__(message)
|
super().__init__(message)
|
||||||
|
@ -170,21 +170,21 @@ class FileNotFound(NotFound):
|
||||||
|
|
||||||
@add_status_code(408)
|
@add_status_code(408)
|
||||||
class RequestTimeout(SanicException):
|
class RequestTimeout(SanicException):
|
||||||
status_code = 408
|
pass
|
||||||
|
|
||||||
|
|
||||||
@add_status_code(413)
|
@add_status_code(413)
|
||||||
class PayloadTooLarge(SanicException):
|
class PayloadTooLarge(SanicException):
|
||||||
status_code = 413
|
pass
|
||||||
|
|
||||||
|
|
||||||
class HeaderNotFound(SanicException):
|
class HeaderNotFound(InvalidUsage):
|
||||||
status_code = 400
|
pass
|
||||||
|
|
||||||
|
|
||||||
@add_status_code(416)
|
@add_status_code(416)
|
||||||
class ContentRangeError(SanicException):
|
class ContentRangeError(SanicException):
|
||||||
status_code = 416
|
pass
|
||||||
|
|
||||||
def __init__(self, message, content_range):
|
def __init__(self, message, content_range):
|
||||||
super().__init__(message)
|
super().__init__(message)
|
||||||
|
@ -209,5 +209,7 @@ def abort(status_code, message=None):
|
||||||
if message is None:
|
if message is None:
|
||||||
message = COMMON_STATUS_CODES.get(status_code,
|
message = COMMON_STATUS_CODES.get(status_code,
|
||||||
ALL_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)
|
sanic_exception = _sanic_exceptions.get(status_code, SanicException)
|
||||||
raise sanic_exception(message=message, status_code=status_code)
|
raise sanic_exception(message=message, status_code=status_code)
|
||||||
|
|
|
@ -3,7 +3,7 @@ from bs4 import BeautifulSoup
|
||||||
|
|
||||||
from sanic import Sanic
|
from sanic import Sanic
|
||||||
from sanic.response import text
|
from sanic.response import text
|
||||||
from sanic.exceptions import InvalidUsage, ServerError, NotFound
|
from sanic.exceptions import InvalidUsage, ServerError, NotFound, abort
|
||||||
|
|
||||||
|
|
||||||
class SanicExceptionTestException(Exception):
|
class SanicExceptionTestException(Exception):
|
||||||
|
@ -33,7 +33,7 @@ def exception_app():
|
||||||
@app.route('/abort')
|
@app.route('/abort')
|
||||||
def handler_invalid(request):
|
def handler_invalid(request):
|
||||||
abort(500)
|
abort(500)
|
||||||
raise InvalidUsage("OK")
|
return text("OK")
|
||||||
|
|
||||||
@app.route('/divide_by_zero')
|
@app.route('/divide_by_zero')
|
||||||
def handle_unhandled_exception(request):
|
def handle_unhandled_exception(request):
|
||||||
|
@ -65,6 +65,7 @@ def test_catch_exception_list():
|
||||||
request, response = app.test_client.get('/')
|
request, response = app.test_client.get('/')
|
||||||
assert response.text == 'ok'
|
assert response.text == 'ok'
|
||||||
|
|
||||||
|
|
||||||
def test_no_exception(exception_app):
|
def test_no_exception(exception_app):
|
||||||
"""Test that a route works without an exception"""
|
"""Test that a route works without an exception"""
|
||||||
request, response = exception_app.test_client.get('/')
|
request, response = exception_app.test_client.get('/')
|
||||||
|
@ -78,10 +79,10 @@ def test_server_error_exception(exception_app):
|
||||||
assert response.status == 500
|
assert response.status == 500
|
||||||
|
|
||||||
|
|
||||||
def test_abort(exception_app):
|
def test_invalid_usage_exception(exception_app):
|
||||||
"""Test the built-in InvalidUsage exception works"""
|
"""Test the built-in InvalidUsage exception works"""
|
||||||
request, response = exception_app.test_client.get('/abort')
|
request, response = exception_app.test_client.get('/invalid')
|
||||||
assert response.status == 500
|
assert response.status == 400
|
||||||
|
|
||||||
|
|
||||||
def test_not_found_exception(exception_app):
|
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 "
|
"The server encountered an internal error and "
|
||||||
"cannot complete your request.")
|
"cannot complete your request.")
|
||||||
|
|
||||||
|
|
||||||
def test_exception_in_exception_handler(exception_app):
|
def test_exception_in_exception_handler(exception_app):
|
||||||
"""Test that an exception thrown in an error handler is handled"""
|
"""Test that an exception thrown in an error handler is handled"""
|
||||||
request, response = exception_app.test_client.get(
|
request, response = exception_app.test_client.get(
|
||||||
|
@ -126,3 +128,9 @@ def test_exception_in_exception_handler_debug_off(exception_app):
|
||||||
debug=True)
|
debug=True)
|
||||||
assert response.status == 500
|
assert response.status == 500
|
||||||
assert response.body.startswith(b'Exception raised in exception ')
|
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