mirror of
https://github.com/tokio-rs/axum.git
synced 2024-12-28 23:38:20 +01:00
docs: add clarification about building middleware and error types (#2448)
This commit is contained in:
parent
ea6dd51e98
commit
560213a7b7
1 changed files with 15 additions and 0 deletions
|
@ -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
|
## `tower::Service` and custom futures
|
||||||
|
|
||||||
If you're comfortable implementing your own futures (or want to learn it) and
|
If you're comfortable implementing your own futures (or want to learn it) and
|
||||||
|
|
Loading…
Reference in a new issue