-
Notifications
You must be signed in to change notification settings - Fork 47.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
Can an error boundary prevent React's error logging? #15069
Comments
console.error
?
For now we'd rather not allow it because it makes it too easy to swallow errors (such as if the error boundary itself is buggy). |
Right, I see reasoning in that, preventing error logging should require more than just the existance of a boundary. Browsers allow to suppress logging uncaught errors to the console via |
Error swallowing should be opt-in. I'd like to swallow certain errors and not others:
|
I'd support this feature as it would be nice to de-clutter our test output. |
consider the following production issue we are facing: Naturally, we have our own ErrorBoundary implementation: As a result of this implementation, when a component fails, we have two separate errors logged in our monitoring pipeline: Since this redundancy is confusing and not acceptable If we don't explicitly log, we lose error specific info. if we explicitly log - we get two separate events in our monitoring pipeline, which is confusing and noisy, since our monitoring pipeline is automated and triggers a whole lot of internal processes. I bet that we both believe It's great that I can isolate failures to granular regions in the application- anything other than that, should not be decided for me as a developer. On the other hand, I do understand where you're coming from by saying you don't want it to be easy to swallow errors, so how about this as middle ground: if you think of it, this is the correct thing to do, since
you say The means of logging should be up to app developers, especially if they went through the trouble of setting up Error Boundaries. Since Error Boundaries are basically logging components with some conditional rendering, and since I bet our logging provider is not the only one which logs As for buggy error boundaries, IMHO, you should not care about them. |
@EyalPerry hit the nail on the head. Is there anything more to say? Shall we patch |
@anilanar I completely agree with you, I think we should really have the possibility to suppress the logging of some errors which have been handled in error boundaries. I think that we should have the power and responsibility to decide what to log and what not to log, especially in production. In my case, if my app has been deployed in production and an error boundary catches an error, I wouldn't want to log the error to the console leading to information disclosure for some "more smart" users who open the browser's console to see what went wrong, or at least, I would want to be able to decide whether to log a certain error captured by an error boundary or not. Also, I think that, when using |
It would be nice to have a control on logging errors to console. For example, I like to use Error Boundaries for handling 404 exceptions in my apps. This way I don't need to call a redirect to some route or put the rendering logic inside my components. Regularly I just throw a I would agree with @EyalPerry that moving the logging into default componentDidCatch would be a nice solution. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution. |
Don’t close. We can’t seriously unconditionally log this anymore. |
I have the opposite problem. Error is not logged to console in dev build.
So if I v16.13.1 |
I'd like this to be fixed as well. The solution @EyalPerry suggested would be perfect. |
I vote for this too. I am also using an error handling mechanism like the one described by @ElForastero. |
Does anyone have a workaround for this while testing? I want to real-render a component under test, catch the error, and make an assertion without seeing error logging, especially as one can't hide logging in jest. |
This is my hacky workaround:
Or, you can do as this guy here |
This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment! |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This is resulting in double errors in our logging system (ErrorBoundary logs it but we also capture console.error) – extremely frustrating. |
The errors are also unconditionally logged during tests where I expect components to throw and catch the errors myself with error boundaries. This is frustrating and pollutes test output. |
We are running into a similar problem as @EyalPerry mentioned. It would be great to fix this |
@gaearon Would also like to see this fixed. I think ErrorBoundary should have same semantics as exception handling, once an exception has been caught, it's caught. If you want to log your exceptions, you should log them in the If the exception handler (ErrorBoundary) itself causes an exception, that's a different case, and should cause an exception to bubble up (which presumably could be caught by a higher level handler/ErrorBoundary). For me there's two main reasons:
|
My team ran into this issue as well. Would really like a fix. |
Yeah, being able to control this logging behavior would be awesome. |
I'm exploring a pattern in which I throw specific errors when running into unexpected scenarios (i.e. missing or invalid parameters, invalid routes, ...) with the intention of moving all of this UI state logic to error boundaries instead rather than repeating it in every page/view. It makes things a lot cleaner, but the unfortunate side-effect is indeed that all of these errors now show up as uncaught errors in the console. Would really love a way to be able to bail out of logging expected errors. I do understand that intentionally throwing errors does not mean that they are also caught, if you for example forget to handle them in an error boundary, or simply forget to wrap your tree with an error boundary component. Not really sure what the best way around this would be. |
Error stacks are ludicrous when testing error boundaries 😬 yikes. |
bump |
bump |
The duplicate logging (once from console, once from error boundary), is troublesome for user's who've configured Sentry error monitoring that way. Considering there exists API to do this for hydration errors with I wouldn't even mind if it's a method I can monkeypatch with a Proxy, if labelled appropriately consumers can take the necessary risks. I did something similar with Next.js (vercel/next.js#36641) to log out hydration errors since Next.js framework doesn't expose this themselves. |
This is a huge pain and makes the console unreadable in dev mode as well as distracting/scary in prod mode. Some of our errors are "normal" / "handled" for some users or configurations. For instance, if a component doesn't have the rights to do something, an ErrorBoundary will catch that and show a nice UI. We also use Suspense and the two tie well together. What I ended up doing to workaround this limitation:
It really shouldn't be this complicated and undocumented. |
bump |
I'm running into this as well for reasons similar to those explained by @AlexGalays. This is a rather unintuitive limitation. |
bump |
Also spent several hours trying to figure out why there are still errors in the console after I had added an error boundary. But it looks like React 19 adds options for handling errors 🤞 |
yes it's a good change - I think you can consider this issue closed, you can use the new options to override the default behaviour to ping import { createRoot } from "react-dom/client";
const container = document.getElementById(“app”);
const root = createRoot(container, {
// Callback called when an error is thrown and not caught by an ErrorBoundary.
onUncaughtError: (error, errorInfo) => {
// only log in dev environments
if (IS_DEV) {
console.warn('Uncaught error', error, errorInfo.componentStack);
}
}),
// Callback called when React catches an error in an ErrorBoundary.
onCaughtError: (error, errorInfo) => {
// do not log errors handled by error boundaries
}),
// Callback called when React automatically recovers from errors (for ex. hydration errors)
onRecoverableError: (error, errorInfo) => {
console.warn('Recoverable error', error, errorInfo.componentStack);
}),
});
root.render(); |
Thanks @AbhiPrasad . The default should be changed however. Caught errors shouldn't be logged to the console by default. That's how things work in general everywhere, e.g. in try-catch statements. Now all the app and test developers have to change their implementation of mounting React to avoid unnecessary console logs. |
I noticed this unconditional
console.error
which I'd like to prevent to keep the console clean from errors that are already "caught" in an error boundary.Maybe a condition on
capturedError.errorBoundaryFound
could prevent this logging?The text was updated successfully, but these errors were encountered: