tower-rs/tower-http

RequestBodyLimit custom response

yokomizor opened this issue · 3 comments

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:

  • accepting a Fn() -> Response<B> in, or
  • returning a BoxError

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.

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

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.

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.