grpc / grpc/grpc-rust

Handling error layer / converting fallible services/layers to infallible

Open
#1,935 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
12.5k
Forks
1.3k
Avg merge
4d 7h
Merged PRs (30d)
24

Description

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.

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

This did not work for me
```rust

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:
```rust
--> src/lib.rs:144:10
|
144 | .serve(addr)
| ^^^^^ the trait `tower::Layer, {closure@src/lib.rs:94:25: 94:53}>>>>` is not implemented for `LoadShed`, which is required by `Stack, Stack) -> impl Future {handle_concurrency_error}, _>, tower::layer::util::Identity>>: tower::Layer, {closure@src/lib.rs:94:25: 94:53}>>>>`
```

_Originally posted by @lcmgh in https://github.com/hyperium/tonic/discussions/1934_

How could this be achieved?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.