Skip to content

Commit 80637a0

Browse files
brophdawg11joseph0926
authored andcommitted
Add note on middleware thrown responses
1 parent 9c9de7f commit 80637a0

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

docs/how-to/middleware.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ const authMiddleware = async ({ request, context }) => {
441441

442442
### `next()` and Error Handling
443443

444-
React Router contains built-in error handling via the route [`ErrorBoundary`](../start/framework/route-module#errorboundary) export. Just like when a loader/action throws, if a middleware throws an error it will be caught and handled at the appropriate `ErrorBoundary` and the `Response` will be returned through the ancestor `next()` call. This means that the `next()` function should never throw and should always return a `Response`, so you don't need to worry about wrapping it in a try/catch.
444+
React Router contains built-in error handling via the route [`ErrorBoundary`](../start/framework/route-module#errorboundary) export. Just like when a loader/action throws (_mostly_), if a middleware throws it will be caught and handled at the appropriate `ErrorBoundary` and a `Response` will be returned through the ancestor `next()` call. This means that the `next()` function should never throw and should always return a `Response`, so you don't need to worry about wrapping it in a try/catch.
445445

446446
This behavior is important to allow middleware patterns such as automatically setting required headers on outgoing responses (i.e., committing a session) from a root middleware. If any error from a middleware caused `next()` to `throw`, we'd miss the execution of ancestor middlewares on the way out and those required headers wouldn't be set.
447447

@@ -467,6 +467,8 @@ export const unstable_middleware = [
467467
]
468468
```
469469
470+
<docs-info>We say _"mostly"_ above because there is a small/nuanced difference if you throw a non-redirect `Response` in a `loader`/`action` versus throwing a `Response` in a `middleware` (redirects behave the same).<br/><br/>Throwing a non-redirect response from a middleware will use that response directly and return it up through the parent `next()` call. This differs from the behavior in `loaders`/`actions` where that response will be converted to an `ErrorResponse` to be rendered by the `ErrorBoundary`.<br/><br/>The difference here is because `loaders`/`actions` are expected to return data which is then provided to components for rendering. But middleware is expected to return a response - so if you return or throw one, we will use it directly. If you want to throw an error with a status code to an error boundary from middleware, you should use the [`data`](../api/utils/data) utility.</docs-info>
471+
470472
## Changes to `getLoadContext`/`AppLoadContext`
471473
472474
<docs-info>This only applies if you are using a custom server and a custom `getLoadContext` function</docs-info>

0 commit comments

Comments
 (0)