Skip to content

Commit

Permalink
websocket: improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
neolynx committed Apr 29, 2024
1 parent beacde2 commit 6b211e1
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion cirrina/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import tempfile
from concurrent import futures
from aiohttp import web, WSMsgType
from aiohttp.http_websocket import WSCloseCode
from aiohttp_session import setup, get_session
from aiohttp_session.cookie_storage import EncryptedCookieStorage
from aiohttp_session_file import FileStorage
Expand Down Expand Up @@ -312,7 +313,7 @@ async def _login(self, request):
request.cirrina.web_session['username'] = username
response = web.Response(status=200)
return response
self.logger.warning('User authentication failed for \'%s\'', str(username))
self.logger.error('User authentication failed for \'%s\'', str(username))
await asyncio.sleep(4)
response = web.Response(status=400)
request.cirrina.web_session.invalidate()
Expand Down Expand Up @@ -641,6 +642,10 @@ async def _ws_handler(self, request, group):
* messages
"""
ws_client = web.WebSocketResponse()
if not ws_client.can_prepare(request):
await ws_client.close(code=WSCloseCode.PROTOCOL_ERROR)
self.logger.error("Websocket cannot be prepared")
return ws_client
await ws_client.prepare(request)

session = None
Expand Down

0 comments on commit 6b211e1

Please sign in to comment.