Skip to content

Commit

Permalink
docs: add clarification about building middleware and error types (#2448
Browse files Browse the repository at this point in the history
)
  • Loading branch information
jondot authored Dec 29, 2023
1 parent ea6dd51 commit 560213a
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions axum/src/docs/middleware.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 560213a

Please sign in to comment.