D
D
Dmitry Arkhipov2020-07-24 15:10:27
Python
Dmitry Arkhipov, 2020-07-24 15:10:27

FastAPI + uvicorn: how to catch exception in websocket handshake process?

Good afternoon.
I can't find an option to catch an exception with a websocket handshake.
Project with API implementation on FastAPI, works with programmatically launched uvicorn ( uvicorn.run() )
Trouble gets out when client connects with incorrect cookie - not ascii characters in cookie body.
The server issues an exception to the console and stops serving requests for a while.

I would like to catch this exception, log the client's ip and continue working, but I can't find a way to catch the error.
It is uvicorn that throws the exception, it hasn't even reached fastapi yet...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey_Dolg, 2020-07-26
@Andrey_Dolg

Since I don’t know uvicorn to the right extent, the only thing I can advise is to look for the import'a websockets/server.py fragment in the initialization of the uvicorn server. and forward changes to the process_request method of the class, which is described in websockets_impl.py. Well, or modify the code or not through inheritance, then this is of course a crutch (=.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question