Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling error layer / converting fallible services/layers to infallible #1935

Open
lcmgh opened this issue Sep 9, 2024 · 0 comments
Open

Comments

@lcmgh
Copy link

lcmgh commented Sep 9, 2024

Axum provides https://docs.rs/axum/0.7.5/axum/error_handling/struct.HandleErrorLayer.html

Example use case:
Respond with too many requests http status code in case ConcurrencyLimitLayer is not ready.

let concurrency_layer = LoadShedLayer::new().layer(ConcurrencyLimitLayer::new(100));

This did not work for me

let handle_error_layer = HandleErrorLayer::new(handle_concurrency_error); // from axum

async fn handle_concurrency_error(
    // `Method` and `Uri` are extractors so they can be used here
    method: Method,
    uri: Uri,
    // the last argument must be the error itself
    err: tower::BoxError,
) -> (StatusCode, String) {
    (
        StatusCode::TOO_MANY_REQUESTS,
        format!("`{method} {uri}` failed with {err}"),
    )
}

let concurrency_layer = LoadShedLayer::new().layer(ConcurrencyLimitLayer::new(100));


Server::builder()
    .layer(handle_error_layer)
    .layer(concurrency_layer)

Error:

   --> src/lib.rs:144:10
    |
144 |         .serve(addr)
    |          ^^^^^ the trait `tower::Layer<SetSensitiveRequestHeaders<SetSensitiveResponseHeaders<tower_http::trace::Trace<Routes, SharedClassifier<GrpcErrorsAsFailures>, {closure@src/lib.rs:94:25: 94:53}>>>>` is not implemented for `LoadShed<ConcurrencyLimitLayer>`, which is required by `Stack<LoadShed<ConcurrencyLimitLayer>, Stack<HandleErrorLayer<fn(Method, Uri, Box<(dyn std::error::Error + Send + Sync + 'static)>) -> impl Future<Output = (http::StatusCode, std::string::String)> {handle_concurrency_error}, _>, tower::layer::util::Identity>>: tower::Layer<SetSensitiveRequestHeaders<SetSensitiveResponseHeaders<tower_http::trace::Trace<Routes, SharedClassifier<GrpcErrorsAsFailures>, {closure@src/lib.rs:94:25: 94:53}>>>>`

Originally posted by @lcmgh in #1934

How could this be achieved?

@lcmgh lcmgh changed the title Handling error layer Handling error layer / converting fallible services/layers Sep 9, 2024
@lcmgh lcmgh changed the title Handling error layer / converting fallible services/layers Handling error layer / converting fallible services/layers to infallible Sep 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant