-
Let's assume I use flask's send_file with an IO stream from S3. In theses streams it can happen that an error occurs. from flask import Flask, send_file
from io import BytesIO
class S3IO(BytesIO):
# just to showcase
def read(self, __size: int | None = ...) -> str:
raise RuntimeError
app = Flask(__name__)
@app.errorhandler(RuntimeError)
def handle_error():
return "error", 400
@app.get("/")
def do():
return send_file(S3IO(), "foo.txt") Output
I'm not sure if it's intended that the error handler is not called. Should I instead overwrite the FileWrapper and handle exceptions there? |
Beta Was this translation helpful? Give feedback.
Answered by
davidism
Jan 26, 2024
Replies: 1 comment 1 reply
-
Streaming responses can't handle errors late. In WSGI (and I'm pretty sure ASGI too), once a response has started, the headers and status code have already been sent to the client, and the body has already been started. It's impossible to take that back and send something else instead. |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
kasium
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Streaming responses can't handle errors late. In WSGI (and I'm pretty sure ASGI too), once a response has started, the headers and status code have already been sent to the client, and the body has already been started. It's impossible to take that back and send something else instead.