root: properly catch 404 errors for websocket connections
Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
parent
d1bd8f333b
commit
bb6eed0db1
|
@ -42,11 +42,28 @@ class LifespanApp:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
|
class RouteNotFoundMiddleware:
|
||||||
|
"""Middleware to ignore 404s for websocket requests
|
||||||
|
taken from https://github.com/django/daphne/issues/165#issuecomment-808284950"""
|
||||||
|
|
||||||
|
def __init__(self, app):
|
||||||
|
self.app = app
|
||||||
|
|
||||||
|
async def __call__(self, scope, receive, send):
|
||||||
|
try:
|
||||||
|
return await self.app(scope, receive, send)
|
||||||
|
except ValueError as exc:
|
||||||
|
if "No route found for path" in str(exc) and scope["type"] == "websocket":
|
||||||
|
await send({"type": "websocket.close"})
|
||||||
|
else:
|
||||||
|
raise exc
|
||||||
|
|
||||||
|
|
||||||
application = SentryAsgiMiddleware(
|
application = SentryAsgiMiddleware(
|
||||||
ProtocolTypeRouter(
|
ProtocolTypeRouter(
|
||||||
{
|
{
|
||||||
"http": get_asgi_application(),
|
"http": get_asgi_application(),
|
||||||
"websocket": URLRouter(websocket.websocket_urlpatterns),
|
"websocket": RouteNotFoundMiddleware(URLRouter(websocket.websocket_urlpatterns)),
|
||||||
"lifespan": LifespanApp,
|
"lifespan": LifespanApp,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
Reference in New Issue