Add a new exception signal for ALL exceptions raised anywhere in application (#2724)
This commit is contained in:
@@ -1,18 +1,19 @@
|
||||
import asyncio
|
||||
import os
|
||||
import signal
|
||||
|
||||
from queue import Queue
|
||||
from types import SimpleNamespace
|
||||
from typing import Optional
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import pytest
|
||||
|
||||
from sanic_testing.testing import HOST, PORT
|
||||
|
||||
from sanic import Sanic
|
||||
from sanic.compat import ctrlc_workaround_for_windows
|
||||
from sanic.exceptions import BadRequest
|
||||
from sanic.exceptions import BadRequest, ServerError
|
||||
from sanic.response import HTTPResponse
|
||||
from sanic.signals import Event
|
||||
|
||||
|
||||
async def stop(app, loop):
|
||||
@@ -148,3 +149,26 @@ def test_signals_with_invalid_invocation(app):
|
||||
BadRequest, match="Invalid event registration: Missing event name"
|
||||
):
|
||||
app.listener(stop)
|
||||
|
||||
|
||||
def test_signal_server_lifecycle_exception(app: Sanic):
|
||||
trigger: Optional[Exception] = None
|
||||
|
||||
@app.route("/hello")
|
||||
async def hello_route(request):
|
||||
return HTTPResponse()
|
||||
|
||||
@app.signal(Event.SERVER_LIFECYCLE_EXCEPTION)
|
||||
async def test_signal(exception: Exception):
|
||||
nonlocal trigger
|
||||
trigger = exception
|
||||
|
||||
@app.before_server_start
|
||||
async def test_before_server_start(app):
|
||||
raise ServerError("test_before_server_start")
|
||||
|
||||
with pytest.raises(ServerError, match="test_before_server_start"):
|
||||
app.run(single_process=True)
|
||||
|
||||
assert isinstance(trigger, ServerError)
|
||||
assert str(trigger) == "test_before_server_start"
|
||||
|
||||
Reference in New Issue
Block a user