Unable to catch exception in httpx.stream python

48 views Asked by At

i am unable to capture error while streaming

Here is my code in fastapi

@app.get("/stream")
async def stream_data():
    headers = {
        "Content-Type": "text/plain",
        "Transfer-Encoding": "chunked",
        "Connection": "keep-alive",
    }

    def generate():
        try:
            with httpx.stream(
                "GET", config["NODE_API_URL"] + "/testendpoint"
            ) as r:
                for chunk in r.iter_raw():
                    r.raise_for_status()
                    yield chunk

        except httpx.TimeoutException as e:
            print(f"A TimeoutException occurred during dummy streaming {e}")

        except httpx.RequestError as e:
            print(f"A RequestError occurred during dummy streaming {e}")

        except httpx.StreamError as e:
            print(f"A StreamError occurred during dummy streaming {e}")

        except Exception as e:
            print(f"An error occurred during dummy streaming {e}")

    return StreamingResponse(generate(), headers=headers)

When I do curl, after 30 seconds, i got this error

curl: (92) HTTP/2 stream 0 was not closed cleanly: INTERNAL_ERROR (err 2)
But there is error in console, I mean none of print statement is being executed.

0

There are 0 answers