Replies: 9 comments 7 replies
-
Could you explain the behavior you're seeing more? I'm not sure I understand. The overlay will always display on an unhandled error, even if you have an error boundary. You can dismiss this overlay to view your error boundary, but the overlay will make you aware of the bug with a quick way to access the source code and fix it. |
Beta Was this translation helpful? Give feedback.
-
@Timer i didn't realize that errorboundaries should only be used for unexpected errors that would otherwise crash a react tree. sounds like this is expected behavior, and that makes sense to me. here's an example project demonstrating the behavior (run in development), though it sounds like we're on the same page. https://github.com/shrugs/error-boundary-test my use-case around catching known errors with errorboundaries is to synchronously render errors that would otherwise make the entire page unusable (not found, permission denied, rate limited, etc), which works well with both ssr and csr. is there a best practice around handling app-level errors that's compatible with ssr that you can point me towards? for context, i'm using apollo-client, and these errors can arise during an ssr pass during the pattern i've seen people use with next frequently is a page HOC that checks for props and then renders the fallback if there was an error in to handle that behavior I was previously using a react context to share some state: const [, setGlobalError] = useContext(GlobalErrorContext)
const {data, loading, error} = useQuery(...)
useIsomorphicLayoutEffect(() => {
if (error) setGlobalError(error)
}, [error]) (with an error-boundary-like component that looks like) function ChildrenOrGlobalError({ children }: PropsWithChildren<{}>) {
const [error] = useContext(GlobalErrorContext)
if (error) return MyFallbackComponent({ error });
return children;
} but because effects aren't executed during ssr, we get a single render of the component tree before the fallback component takes over any thoughts? |
Beta Was this translation helpful? Give feedback.
-
@Timer I also was very confused by how Next.js 9.4 is handling errors. Even when you have a custom ErrorBoundary, the Next.js error overlay says "Unhandled Runtime Error" which is very confusing because I did indeed handle the error.
Do you have some inside information beyond what's public on how error handling will be used for data fetching? Because the latest React suspense for data fetching docs on error handling indicate the opposite of what you said. Here's the example they have: function ProfilePage() {
return (
<Suspense fallback={<h1>Loading profile...</h1>}>
<ProfileDetails />
<ErrorBoundary fallback={<h2>Could not fetch posts.</h2>}>
<Suspense fallback={<h1>Loading posts...</h1>}>
<ProfileTimeline />
</Suspense>
</ErrorBoundary>
</Suspense>
);
} They are using an ErrorBoundary to catch any type of data loading error. Additionally, the documentation indicates that we should be using error boundaries judiciously, just like a normal try/catch. My use case: I'm using The data fetching hooks can throw a few types of errors like Then we have one or more error boundaries throughout the app to handle these. For example, we can have a top level error boundary in Here's what I would like to see if the error was handled by a custom error boundary:
|
Beta Was this translation helpful? Give feedback.
-
@timneutkens are you open for PRs to solve this issue? Here's my latest request:
|
Beta Was this translation helpful? Give feedback.
-
@flybayer's solution seems like the best approach for me, here! It is really annoying to get ErrorOverlay for handled errors. |
Beta Was this translation helpful? Give feedback.
-
Here's the sibling thread to this for the same issue in Create React App, and Dan Abramov's suggested solution (which is almost exactly like what I last proposed) |
Beta Was this translation helpful? Give feedback.
-
Just stumbled upon this myself and after some digging this is due to and https://stackoverflow.com/a/57200935. @flybayer mentions in facebook/create-react-app#6530 (comment) that deletingerror.stack works which would be explained by
|
Beta Was this translation helpful? Give feedback.
-
I spent some time with this as well. Despite what the error box says, it's handled by my code and this only happens during development. This was a quick and dirty workaround for my use case specifically. if (process.env.NODE_ENV !== "production" && globalThis.window) {
// prevent handled runtime errors from showing up in the error overlay
const oldAddEventListener = window.addEventListener;
try {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const client = (require as any)(
"next/dist/compiled/@next/react-dev-overlay/dist/client"
);
client.unregister();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
window.addEventListener = (type: any, listener: any) => {
if (type == "error") {
oldAddEventListener(type, (e) => {
if (e.message && e.message.includes("usePromise")) {
return;
}
listener(e);
});
} else {
oldAddEventListener(type, listener);
}
};
client.register();
} finally {
window.addEventListener = oldAddEventListener;
}
} I'm not sure about this but it seems that by throwing errors during rendering the error event will be emitted regardless independently from an error boundary. If the order is preserved, so for example an error boundary catches it before it's emitted as an event; the best solution I can only think of is some function we can call during development that takes an error and blacklists it. import { MarkErrorHandled } from "next/dev-overlay"
...
static getDerivedStateFromError(error: Error): ErrorState {
MarkErrorHandled(error)
return {
error
}
} I'd avoid mutating or extending the error object. |
Beta Was this translation helpful? Give feedback.
-
Is this still an issue w/ Next 13 or are there other ways to handle this kind of situation? I'm running into this same behavior; I am wrapping a component hierarchy in a handled error boundary; within the boundary tree, there is a component that can throw an error; if/when the error is thrown, I see a next.js dev-mode-overlay instead of my expected UI. |
Beta Was this translation helpful? Give feedback.
-
on v9.4 with the new ErrorBoundary, i'm seeing it catch errors that are being handled by a child ErrorBoundary (using react-error-boundary)
i assumed this would only happen if my custom error boundary throws during its render function after
componentDidCatch
, but it's not (otherwise react would unmount the entire tree in production, which it isn't).is this expected behavior?
Beta Was this translation helpful? Give feedback.
All reactions