-
-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Middleware: errors caught at root error boundary, not route error boundary #13529
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
There 2 things going on here - 1 bug and 1 expected (but unobvious) behavior The The "why doesn't my error show on the
|
Thanks a lot @brophdawg11 for the quick response. I've got the following funny little solution for binding middleware errors to the nearest error boundary. Maybe you can tell me it's very broken or maybe it gives you an idea. Thanks also for #13538! /**
* Ensures that errors thrown during middleware render at the nearest Error Boundary.
* https://github.com/remix-run/react-router/issues/13529#issuecomment-2848038105
* @example
* const provideAccountContext = async (dataArgs, next) => {
* try {
* const record = await fetchAccount(dataArgs); // 404
* context.set(accountContext, record);
* } catch (error) {
* await bindMiddlewareError(error, context, next);
* }
* }
* @example
* const provideAccountContext = async (dataArgs, next) => {
* try {
* const record = await fetchAccount(dataArgs); // 404
* context.set(accountContext, record);
* } catch (error) {
* if (error instanceof Fault) {
* switch (error.status) {
* case 401:
* case 404:
* // like GitHub, avoid revealing information by differentiating between
* // 401 and 404
* await bindMiddlewareError(new Fault({ status: 404 }), context, next);
* break;
* default:
* await bindMiddlewareError(error, context, next);
* }
* }
* }
*/
export const bindMiddlewareError = async (
error: unknown,
context: unstable_RouterContextProvider,
next: unstable_MiddlewareNextFunction
): Promise<never> => {
context.set(middlewareErrorContext, error); // middlewares later in chain may skip if prior error
/**
* Resolve matched loaders. Awaiting `next` brings us to code path that renders
* error boundaries. Expect `next` to throw from thwarted expectations due to failed
* middleware.
*/
try {
await next();
throw error; // sometimes `next` doesn't throw
} catch (expected) {
throw error; // ignore the expected error from `next`, instead throw the root error
}
}; |
I've experienced something similar sounding, but I don't use middleware. This happens sometimes after a fetcher has run and a new post has been added to the page, when I click to view the post (from the parent route) it throws this error:
But only on the first click and not always, impossible to reproduce consistently. I have preloading on intent set. Not sure if relevant but I'm using react-compiler. v7.5.3. |
This should be resolved by #13538 and will be available in the next release |
🤖 Hello there, We just published version Thanks! |
🤖 Hello there, We just published version Thanks! |
I'm using React Router as a...
framework
Reproduction
Middleware: errors caught at root error boundary, not route error boundary
Minimal repro
Under certain conditions route-level middleware errors are caught at
root.tsx
rather than at route-defined error boundaries.Additionally, different errors are reported for document loads vs. client navigation.
Please note: Comment the
loader
in theauth
layout on and off to see the expected behavior when no loader is defined for that layout and the unexpected behavior when a loader is defined.Issue template
What version of React Router are you using?
7.5.3
Steps to Reproduce
Minimal repro
Starting at index, click
Account
link.provideAccount
middleware throws and is caught byroot.tsx
error boundary.Reload the page to see different errors reported between client navigation and document load.
Client navigation:
Cannot use 'in' operator to search for 'error' in undefined
.vs. Document load:
Expect Error Boundary: account.tsx
Comment out the
loader
in theauth
layout and observe that errors are caught at the expected route error boundaries.System Info
Used Package Manager
npm
Expected Behavior
Errors thrown in route-specific middleware should be caught by the route's error boundary. Errors should be consistent between document loads and client navigation.
Actual Behavior
Errors thrown in route-specific middleware are caught by the root error boundary. Different errors are reported for document loads vs. client navigation.
The text was updated successfully, but these errors were encountered: