From 0799edbca052fdac4de6dbe477201d6c993d8079 Mon Sep 17 00:00:00 2001 From: Andrey Goncharov Date: Thu, 2 Dec 2021 10:06:51 -0800 Subject: [PATCH] Fix excessive error reporting Summary: Currently, we register global error handlers inside of ErrorReporter. To make these global error visible to the user we pipe them to `console.error`. All console methods are monkey-patched in our Logger. Logger uses ErrorReporter to report errors to LogView. It leads to duplicate errors in LogView. In this diff, we extract startGlobalErrorHandling which should help us avoid duplication. Reviewed By: passy Differential Revision: D32759909 fbshipit-source-id: 6376e193c0ba6f0b46aaccc139ecf5066a04da11 --- .../src/startFlipperDesktop.tsx | 3 +++ .../src/utils/globalErrorHandling.tsx | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 desktop/flipper-ui-core/src/utils/globalErrorHandling.tsx diff --git a/desktop/flipper-ui-core/src/startFlipperDesktop.tsx b/desktop/flipper-ui-core/src/startFlipperDesktop.tsx index aaaaf058c..e2ffd24d5 100644 --- a/desktop/flipper-ui-core/src/startFlipperDesktop.tsx +++ b/desktop/flipper-ui-core/src/startFlipperDesktop.tsx @@ -53,6 +53,7 @@ import { } from 'flipper-common'; import {internGraphPOSTAPIRequest} from './fb-stubs/user'; import {getRenderHostInstance} from './RenderHost'; +import {startGlobalErrorHandling} from './utils/globalErrorHandling'; class AppFrame extends React.Component< {logger: Logger; persistor: Persistor}, @@ -176,6 +177,8 @@ function init() { const logger = initLogger(store); setLoggerInstance(logger); + startGlobalErrorHandling(); + // rehydrate app state before exposing init const persistor = persistStore(store, undefined, () => { // Make sure process state is set before dispatchers run diff --git a/desktop/flipper-ui-core/src/utils/globalErrorHandling.tsx b/desktop/flipper-ui-core/src/utils/globalErrorHandling.tsx new file mode 100644 index 000000000..62540bdbd --- /dev/null +++ b/desktop/flipper-ui-core/src/utils/globalErrorHandling.tsx @@ -0,0 +1,19 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @format + */ + +export const startGlobalErrorHandling = () => { + if (typeof window !== 'undefined') { + window.addEventListener('error', (event) => { + console.error('"error" event intercepted:', event.error); + }); + window.addEventListener('unhandledrejection', (event) => { + console.error('"unhandledrejection" event intercepted:', event.reason); + }); + } +};