docs: add clarification about building middleware and error types (#2448)

This commit is contained in:
Dotan J. Nahum 2023-12-29 15:19:16 +02:00 committed by GitHub
parent ea6dd51e98
commit 560213a7b7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -264,6 +264,21 @@ where
}
```
Note that your error type being defined as `S::Error` means that your middleware typically _returns no errors_. As a principle always try to return a response and try not to bail out with a custom error type. For example, if a 3rd party library you are using inside your new middleware returns its own specialized error type, try to convert it to some reasonable response and return `Ok` with that response.
If you choose to implement a custom error type such as `type Error = BoxError` (a boxed opaque error), or any other error type that is not `Infallible`, you must use a `HandleErrorLayer`, here is an example using a `ServiceBuilder`:
```ignore
ServiceBuilder::new()
.layer(HandleErrorLayer::new(|_: BoxError| async {
// because Axum uses infallible errors, you must handle your custom error type from your middleware here
StatusCode::BAD_REQUEST
}))
.layer(
// <your actual layer which DOES return an error>
);
```
## `tower::Service` and custom futures
If you're comfortable implementing your own futures (or want to learn it) and