refactor: consistent exception naming (#2420)
Co-authored-by: Adam Hopkins <adam@amhopkins.com>
This commit is contained in:
@@ -9,7 +9,7 @@ import uvicorn
|
||||
from sanic import Sanic
|
||||
from sanic.application.state import Mode
|
||||
from sanic.asgi import MockTransport
|
||||
from sanic.exceptions import Forbidden, InvalidUsage, ServiceUnavailable
|
||||
from sanic.exceptions import Forbidden, BadRequest, ServiceUnavailable
|
||||
from sanic.request import Request
|
||||
from sanic.response import json, text
|
||||
from sanic.server.websockets.connection import WebSocketConnection
|
||||
@@ -392,7 +392,7 @@ async def test_websocket_accept_with_multiple_subprotocols(
|
||||
|
||||
|
||||
def test_improper_websocket_connection(transport, send, receive):
|
||||
with pytest.raises(InvalidUsage):
|
||||
with pytest.raises(BadRequest):
|
||||
transport.get_websocket_connection()
|
||||
|
||||
transport.create_websocket_connection(send, receive)
|
||||
|
||||
@@ -5,7 +5,7 @@ from sanic.blueprint_group import BlueprintGroup
|
||||
from sanic.blueprints import Blueprint
|
||||
from sanic.exceptions import (
|
||||
Forbidden,
|
||||
InvalidUsage,
|
||||
BadRequest,
|
||||
SanicException,
|
||||
ServerError,
|
||||
)
|
||||
@@ -104,7 +104,7 @@ def test_bp_group(app: Sanic):
|
||||
|
||||
@blueprint_1.route("/invalid")
|
||||
def blueprint_1_error(request: Request):
|
||||
raise InvalidUsage("Invalid")
|
||||
raise BadRequest("Invalid")
|
||||
|
||||
@blueprint_2.route("/")
|
||||
def blueprint_2_default_route(request):
|
||||
@@ -120,7 +120,7 @@ def test_bp_group(app: Sanic):
|
||||
|
||||
blueprint_3 = Blueprint("blueprint_3", url_prefix="/bp3")
|
||||
|
||||
@blueprint_group_1.exception(InvalidUsage)
|
||||
@blueprint_group_1.exception(BadRequest)
|
||||
def handle_group_exception(request, exception):
|
||||
return text("BP1_ERR_OK")
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ from sanic.app import Sanic
|
||||
from sanic.blueprints import Blueprint
|
||||
from sanic.constants import HTTP_METHODS
|
||||
from sanic.exceptions import (
|
||||
InvalidUsage,
|
||||
BadRequest,
|
||||
NotFound,
|
||||
SanicException,
|
||||
ServerError,
|
||||
@@ -448,7 +448,7 @@ def test_bp_exception_handler(app):
|
||||
|
||||
@blueprint.route("/1")
|
||||
def handler_1(request):
|
||||
raise InvalidUsage("OK")
|
||||
raise BadRequest("OK")
|
||||
|
||||
@blueprint.route("/2")
|
||||
def handler_2(request):
|
||||
|
||||
@@ -6,9 +6,16 @@ from bs4 import BeautifulSoup
|
||||
|
||||
from sanic import Sanic
|
||||
from sanic.exceptions import (
|
||||
BadRequest,
|
||||
ContentRangeError,
|
||||
ExpectationFailed,
|
||||
Forbidden,
|
||||
HeaderExpectationFailed,
|
||||
InvalidUsage,
|
||||
MethodNotAllowed,
|
||||
MethodNotSupported,
|
||||
NotFound,
|
||||
RangeNotSatisfiable,
|
||||
SanicException,
|
||||
ServerError,
|
||||
Unauthorized,
|
||||
@@ -77,7 +84,7 @@ def exception_app():
|
||||
|
||||
@app.route("/invalid")
|
||||
def handler_invalid(request):
|
||||
raise InvalidUsage("OK")
|
||||
raise BadRequest("OK")
|
||||
|
||||
@app.route("/abort/401")
|
||||
def handler_401_error(request):
|
||||
@@ -136,7 +143,7 @@ def test_server_error_exception(exception_app):
|
||||
|
||||
|
||||
def test_invalid_usage_exception(exception_app):
|
||||
"""Test the built-in InvalidUsage exception works"""
|
||||
"""Test the built-in BadRequest exception works"""
|
||||
request, response = exception_app.test_client.get("/invalid")
|
||||
assert response.status == 400
|
||||
|
||||
@@ -375,3 +382,10 @@ def test_contextual_exception_functional_message(override):
|
||||
assert response.status == 418
|
||||
assert response.json["message"] == error_message
|
||||
assert response.json["context"] == {"foo": "bar"}
|
||||
|
||||
|
||||
def test_exception_aliases():
|
||||
assert InvalidUsage is BadRequest
|
||||
assert MethodNotSupported is MethodNotAllowed
|
||||
assert ContentRangeError is RangeNotSatisfiable
|
||||
assert HeaderExpectationFailed is ExpectationFailed
|
||||
|
||||
@@ -10,7 +10,7 @@ from bs4 import BeautifulSoup
|
||||
from pytest import LogCaptureFixture, MonkeyPatch
|
||||
|
||||
from sanic import Sanic, handlers
|
||||
from sanic.exceptions import Forbidden, InvalidUsage, NotFound, ServerError
|
||||
from sanic.exceptions import Forbidden, BadRequest, NotFound, ServerError
|
||||
from sanic.handlers import ErrorHandler
|
||||
from sanic.request import Request
|
||||
from sanic.response import stream, text
|
||||
@@ -32,7 +32,7 @@ def exception_handler_app():
|
||||
|
||||
@exception_handler_app.route("/1", error_format="html")
|
||||
def handler_1(request):
|
||||
raise InvalidUsage("OK")
|
||||
raise BadRequest("OK")
|
||||
|
||||
@exception_handler_app.route("/2", error_format="html")
|
||||
def handler_2(request):
|
||||
|
||||
@@ -8,7 +8,7 @@ import pytest
|
||||
|
||||
from sanic_testing.testing import HOST, PORT
|
||||
|
||||
from sanic.exceptions import InvalidUsage, SanicException
|
||||
from sanic.exceptions import BadRequest, SanicException
|
||||
|
||||
|
||||
AVAILABLE_LISTENERS = [
|
||||
@@ -137,7 +137,7 @@ async def test_trigger_before_events_create_server_missing_event(app):
|
||||
class MySanicDb:
|
||||
pass
|
||||
|
||||
with pytest.raises(InvalidUsage):
|
||||
with pytest.raises(BadRequest):
|
||||
|
||||
@app.listener
|
||||
async def init_db(app, loop):
|
||||
|
||||
@@ -11,7 +11,7 @@ import pytest
|
||||
from sanic_testing.testing import HOST, PORT
|
||||
|
||||
from sanic.compat import ctrlc_workaround_for_windows
|
||||
from sanic.exceptions import InvalidUsage
|
||||
from sanic.exceptions import BadRequest
|
||||
from sanic.response import HTTPResponse
|
||||
|
||||
|
||||
@@ -122,6 +122,6 @@ def test_signals_with_invalid_invocation(app):
|
||||
return HTTPResponse()
|
||||
|
||||
with pytest.raises(
|
||||
InvalidUsage, match="Invalid event registration: Missing event name"
|
||||
BadRequest, match="Invalid event registration: Missing event name"
|
||||
):
|
||||
app.listener(stop)
|
||||
|
||||
@@ -2,7 +2,6 @@ import pytest
|
||||
|
||||
from sanic.blueprints import Blueprint
|
||||
from sanic.constants import HTTP_METHODS
|
||||
from sanic.exceptions import InvalidUsage
|
||||
from sanic.request import Request
|
||||
from sanic.response import HTTPResponse, text
|
||||
from sanic.views import HTTPMethodView
|
||||
|
||||
Reference in New Issue
Block a user