tower-rs / tower-http

HTTP specific Tower utilities.
680 stars 159 forks source link

RequestBodyLimit custom response #417

Open yokomizor opened 11 months ago

yokomizor commented 11 months ago

Feature Request

Make it possible to define the response RequestBodyLimit gives when limits are exceed.

Motivation

User facing endpoints that accept large bodies should return a response that users can understand, possibly with some tips to solve request issues. The most basic tip being "what is the limit?".

Proposal

I think it could be done either via:

Alternatives

The only alternative I can think of for now is to have another layer that checks if the response status is 413, and change the response body there.

rakshith-ravi commented 11 months ago

Wouldn't a tower::ServiceExt::map work here?

yokomizor commented 11 months ago

Uhm, the only way I see to change the response using ServiceExt::map is by checking if response status is 413, which works but feels fragile.

A non breaking change that could help with that is adding something like tower_http::limit::body::ResponseBody::limit_exceeded(&self) -> bool.

Then a ServiceExt::map service could use that instead of relying on response status codes.

jplatte commented 10 months ago

Given the low maintainence this project currently gets, I would recommend checking the status code for now, if you haven't already implemented that. I agree a better solution should be added though.