Meilleure gestion des exceptions pour le serveur HTTP
J'ai remarqué cette exception dans les logs:
Jan 15 04:41:04 storage-01 matrix-alertbot[478]: 2023-01-15 04:41:04,617 | aiohttp.server [ERROR] Error handling request
Jan 15 04:41:04 storage-01 matrix-alertbot[478]: Traceback (most recent call last):
Jan 15 04:41:04 storage-01 matrix-alertbot[478]: File "/opt/matrix-alertbot/lib/python3.9/site-packages/aiohttp/web_protocol.py", line 435, in _handle_request
Jan 15 04:41:04 storage-01 matrix-alertbot[478]: resp = await request_handler(request)
Jan 15 04:41:04 storage-01 matrix-alertbot[478]: File "/opt/matrix-alertbot/lib/python3.9/site-packages/aiohttp/web_app.py", line 504, in _handle
Jan 15 04:41:04 storage-01 matrix-alertbot[478]: resp = await handler(request)
Jan 15 04:41:04 storage-01 matrix-alertbot[478]: File "/opt/matrix-alertbot/lib/python3.9/site-packages/aiohttp/web_middlewares.py", line 117, in impl
Jan 15 04:41:04 storage-01 matrix-alertbot[478]: return await handler(request)
Jan 15 04:41:04 storage-01 matrix-alertbot[478]: File "/opt/matrix-alertbot/lib/python3.9/site-packages/aiohttp_prometheus_exporter/middleware.py", line 111, in prometheus_middleware
Jan 15 04:41:04 storage-01 matrix-alertbot[478]: raise e from None
Jan 15 04:41:04 storage-01 matrix-alertbot[478]: File "/opt/matrix-alertbot/lib/python3.9/site-packages/aiohttp_prometheus_exporter/middleware.py", line 83, in prometheus_middleware
Jan 15 04:41:04 storage-01 matrix-alertbot[478]: response = await handler(request)
Jan 15 04:41:04 storage-01 matrix-alertbot[478]: File "/opt/matrix-alertbot/lib/python3.9/site-packages/matrix_alertbot/webhook.py", line 107, in create_alerts
Jan 15 04:41:04 storage-01 matrix-alertbot[478]: await create_alert(alert, room_id, request)
Jan 15 04:41:04 storage-01 matrix-alertbot[478]: File "/opt/matrix-alertbot/lib/python3.9/site-packages/matrix_alertbot/webhook.py", line 157, in create_alert
Jan 15 04:41:04 storage-01 matrix-alertbot[478]: event = await send_text_to_room(
Jan 15 04:41:04 storage-01 matrix-alertbot[478]: File "/opt/matrix-alertbot/lib/python3.9/site-packages/matrix_alertbot/chat_functions.py", line 72, in send_text_to_room
Jan 15 04:41:04 storage-01 matrix-alertbot[478]: response_event = await matrix_client.room_send(
Jan 15 04:41:04 storage-01 matrix-alertbot[478]: File "/opt/matrix-alertbot/lib/python3.9/site-packages/nio/client/async_client.py", line 1575, in room_send
Jan 15 04:41:04 storage-01 matrix-alertbot[478]: await self.share_group_session(
Jan 15 04:41:04 storage-01 matrix-alertbot[478]: File "/opt/matrix-alertbot/lib/python3.9/site-packages/nio/client/async_client.py", line 1918, in share_group_session
Jan 15 04:41:04 storage-01 matrix-alertbot[478]: await self.keys_claim(missing_sessions)
Jan 15 04:41:04 storage-01 matrix-alertbot[478]: File "/opt/matrix-alertbot/lib/python3.9/site-packages/nio/client/async_client.py", line 1866, in keys_claim
Jan 15 04:41:04 storage-01 matrix-alertbot[478]: return await self._send(KeysClaimResponse, method, path, data)
Jan 15 04:41:04 storage-01 matrix-alertbot[478]: File "/opt/matrix-alertbot/lib/python3.9/site-packages/nio/client/async_client.py", line 761, in _send
Jan 15 04:41:04 storage-01 matrix-alertbot[478]: resp = await self.create_matrix_response(
Jan 15 04:41:04 storage-01 matrix-alertbot[478]: File "/opt/matrix-alertbot/lib/python3.9/site-packages/nio/client/async_client.py", line 535, in create_matrix_response
Jan 15 04:41:04 storage-01 matrix-alertbot[478]: resp = response_class.from_dict(parsed_dict, *data)
Jan 15 04:41:04 storage-01 matrix-alertbot[478]: File "/opt/matrix-alertbot/lib/python3.9/site-packages/nio/responses.py", line 178, in wrapper
Jan 15 04:41:04 storage-01 matrix-alertbot[478]: return error_class.from_dict(parsed_dict, *args, **kwargs)
Jan 15 04:41:04 storage-01 matrix-alertbot[478]: TypeError: from_dict() missing 1 required positional argument: 'room_id'
A priori, cela s'est produit parce que le bot était en train de se reconnecter au moment où le serveur essayait d'envoyer l'alerte sur le salon… ce qui a fait complètement crasher le bot (plus d'alertes envoyées à partir de là).
Je vais donc faire en sorte que ce genre d'exception inattendue soit également gérée.