axum
axum copied to clipboard
How should `Body` handle errors?
In 0.7 axum is getting its own Body type. Currently hyper just ignores errors produced by bodies which is a bit of a footgun. We should consider whether axum's body should use Infallible as the error type.
If not for Infallible, what's our other option? Also, what're the reasons a body can fail? Are we talking about network failures when sending data or something else?
hyper doesn't ignore errors, an error from a body means the connection gets closed abruptly. Transmission and generations of bodies can fail part-way through, so Infallible doesn't seem correct.
In that case I think the only time when we should actually handle errors for Body is for upgraded requests (like websockets). If a JSON body is being sent, and the connection gets closed abruptly, there's not much we (or the user) can do anyway. We can just ignore that part and not expose that to the user.
For upgraded requests where connections are kept open though, it certainly makes sense to handle errors for Body and provide the user with a way to handle it
The primary use case I have in mind is making it easy to log the error or send it to whatever error reporting system a user wants to use.
Maybe we can just do a simple log::info!() with a specific target and let the user route that target to wherever they want?
Indeed, i just shot my foot :-)
In the serve with hyper example, on an error, no 500 responses are returned, it just closes the connection abruptly. Im happy to contribute a PR to "fix" the example if there anyone has pointers.
My question becomes, how does one take those errors and make hyper return a 500 from it ?
@happysalada that's impossible, at the time the body error is generated, the status code has already been encoded and possibly sent.
This makes sense.
I seem to remember that an error in the stream caused a panic and the server to stop accepting request.
It seems in this case, closing the connection abruptly makes sense, it should just fail gracefully and leave am option to log the error somewhere
I'm not really seeing anything actionable here, as noted by Sean, Infallible isn't really adequate.