axum icon indicating copy to clipboard operation
axum copied to clipboard

How should `Body` handle errors?

Open davidpdrsn opened this issue 2 years ago • 6 comments

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.

davidpdrsn avatar Apr 21 '23 15:04 davidpdrsn

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?

rakshith-ravi avatar Sep 29 '23 18:09 rakshith-ravi

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.

seanmonstar avatar Sep 29 '23 18:09 seanmonstar

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

rakshith-ravi avatar Sep 29 '23 18:09 rakshith-ravi

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.

davidpdrsn avatar Sep 29 '23 20:09 davidpdrsn

Maybe we can just do a simple log::info!() with a specific target and let the user route that target to wherever they want?

rakshith-ravi avatar Sep 29 '23 20:09 rakshith-ravi

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 avatar Feb 22 '24 21:02 happysalada

@happysalada that's impossible, at the time the body error is generated, the status code has already been encoded and possibly sent.

jplatte avatar Sep 24 '24 21:09 jplatte

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

happysalada avatar Sep 24 '24 23:09 happysalada

I'm not really seeing anything actionable here, as noted by Sean, Infallible isn't really adequate.

jplatte avatar Sep 28 '24 21:09 jplatte