-
Notifications
You must be signed in to change notification settings - Fork 26.3k
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
Non-NextJS Error Boundary doesn't catch errors originating from server components. #58754
Comments
The Legacy react docs mention that Server Rendering errors aren't caught by error boundaries, but this note was removed in the new React docs--has this changed?
|
This discussion topic seems to target the same issue #53618 |
Using App Router -- currently it's possible to catch server-side errors like this: import { Suspense } from 'react';
import { ErrorBoundary } from 'react-error-boundary';
const ThisWillFail = async () => {
// If this request fails I want the <ErrorBoundary> to catch it, rather than having
// the whole page fails and fallback to `error.tsx`.
const data = fetch('https://somepage.com/api');
return <div>Hello</div>;
}
const Thing = () => (
<ErrorBoundary fallback={<div>An error happened</div>}>
<Suspense fallback={<div>Loading...</div>}>
<ThisWillFail />
</Suspense>
</ErrorBoundary>
);
export default Thing; However, if I want to display the actual error message to the user (ie. use the import { Suspense } from 'react';
import { ErrorBoundary } from 'react-error-boundary';
const ThisWillFail = async () => {
// If this request fails I want the <ErrorBoundary> to catch it, rather than having
// the whole page fails and fallback to `error.tsx`.
const data = fetch('https://somepage.com/api');
return <div>Hello</div>;
}
const ErrorHandler = ({error}) => {
return <div>{error.message}</div>
}
const Thing = () => (
<ErrorBoundary FallbackComponent={ErrorHandler}>
<Suspense fallback={<div>Loading...</div>}>
<ThisWillFail />
</Suspense>
</ErrorBoundary>
);
export default Thing;
The Next.js Error Handling documentation mentions using Any help would be appreciated here. |
looks like no infinite loops for now. |
@EvHaus thanks a lot for your answer. Your proposition is working for me on Next Can you share more details on why Previously I was trying to use my Don't know if I am an edge case, but it's very frustrating that Vercel provides poor documentation about how to handle RSC errors. |
@th91vi Sorry, I don't know. That's why this issue exists. :) Need Next.js devs to help us. |
@EvHaus Correct me if I'm wrong, but don't forget that suspense will shift part of the server rendering to a second time and display a fallback to the client in the meantime. |
Link to the code that reproduces this issue
https://codesandbox.io/p/devbox/nextjs-error-boundary-catches-server-throws-mmvlsd
To Reproduce
ErrorBoundary
component<ErrorBoundary />
with some arbitrary fallback UICurrent vs. Expected behavior
Current Behavior:
any custom
<ErrorBoundary />
component will not catch an error that is thrown from a descendent server component, instead it will be uncaught, unless aerror.tsx
file is defined somewhere for the given route.Expected Behavior:
<ErrorBoundary />
-like components should catch server errors without needing to be implemented as Route Segmenterror.tsx
files.Verify canary release
Provide environment information
Operating System: Platform: linux Arch: x64 Version: #1 SMP PREEMPT_DYNAMIC Sun Aug 6 20:05:33 UTC 2023 Binaries: Node: 20.9.0 npm: 10.1.0 Yarn: 1.22.19 pnpm: N/A Relevant Packages: next: 14.0.4-canary.9 eslint-config-next: 14.0.2 react: 18.2.0 react-dom: 18.2.0 typescript: 5.2.2 Next.js Config: output: N/A
Which area(s) are affected? (Select all that apply)
App Router
Additional context
In the linked sandbox, adding
"use client";
to the<BadComponent />
resolves the issue, proving the gap in functionality between Next's proprietary error boundary and how "native" error boundaries behave. I also don't believe this discrepancy has always been the case.The text was updated successfully, but these errors were encountered: