Есть простой сервер:
from aiohttp import web
routes = web.RouteTableDef()
# Error middleware
@web.middleware
async def error_middleware(request, handler):
print("IN")
try:
response = await handler(request)
status = response.status
if status == 200:
return response
message = response.message
except web.HTTPException as ex:
status = ex.status
message = ex.reason
if status != 404:
print(f"{message} with status {status}")
return web.json_response({'error': message})
@routes.get('/')
async def index(request: web.Request):
return web.Response(text="Hello, world")
async def init_app() -> web.Application:
app = web.Application(middlewares=[error_middleware])
app.add_routes(routes)
return app
if __name__ == "__main__":
try:
print(f'Server start!')
web.run_app(init_app(), host="0.0.0.0", port=8080)
except Exception as e:
print(f'Error: {e}')
Нужно для сервера создать обработчик всех ошибок, и данный код ловит некоторые из них, но не все, к примеру он не ловит invalid HTTP method
:
Как я могу поймать эту ошибку в моём коде и обработать для красивово вывода в консоль или логов?