Replies: 2 comments
-
Did you find solution? |
Beta Was this translation helpful? Give feedback.
0 replies
-
The only solution I found for this for was using custom middleware and checking the status code. Hopefully a better solution is found in the future. If the payload is too big, the Note that with approach if it ever reaches the handlers and any handler sends a 413 error message, the response will be overwritten by this middleware. use crate::{
middleware,
prelude::*,
};
use axum::middleware as axum_middleware;
pub fn app(app_state: ApiState) -> ApiResult<Router> {
let cors = setup_cors(app_state.settings)?;
let app = Router::new()
.nest("/api/{version}", all_routes(app_state.clone()))
.layer(axum_middleware::from_fn_with_state(
app_state,
middleware::extract_token_if_present,
))
.layer(RequestBodyLimitLayer::new(1000 * 1000 * 2))
.layer(axum_middleware::from_fn(middleware::handle_large_request));
Ok(app)
}
pub async fn handle_large_request(req: Request, next: Next) -> ApiResult<Response<Body>> {
let response = next.run(req).await;
if response.status() == StatusCode::PAYLOAD_TOO_LARGE {
// customize based on what you want
return Err(ApiError::PayloadTooLarge);
}
Ok(response)
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I'm building a simple API server and I want as many of its responses to be JSON-formatted, including errors. I wanted to put a request timeout on this server and a max request body limit. I quickly found out that
tower_http
's TimeoutLayer could not be customized because it automatically converts errors to a response with a text/plain body. Howevertower
's TimeoutLayer can be customized with aHandleErrorLayer
so I did just that. Unfortunately, I have not been able to find anything likeRequestBodyLimitLayer
intower
. Seemingly onlytower_http
has this layer which does not allow customization.How can I solve this without creating my own service layer?
Beta Was this translation helpful? Give feedback.
All reactions