Skip to content

Properly failing a large request while using an extractor #3015

Answered by echelonh
echelonh asked this question in Q&A
Discussion options

You must be logged in to vote

tbh I'm not sure why I worked at the time with an Extractor to begin with.
using from_fn_with_state did the trick.

For anyone who might reach this post in the future - I ended up with something like this:

pub async fn auth_middleware(
    State(state): State<AuthState>,
    mut request: Request,
    next: Next,
) -> Response {
    if let Err(e) = auth_middleware_inner(state, &mut request).await {
        let _ = request.into_body().collect().await;
        return e.into_response();
    }
    next.run(request).await
}

// just so that the `?` operator can be used with ease to short-circuit, AuthError impls IntoResponse
async fn auth_middleware_inner(
    state: AuthState,
    request: &mut R…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@echelonh
Comment options

@mladedav
Comment options

mladedav Nov 4, 2024
Collaborator

@echelonh
Comment options

Answer selected by echelonh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants