/**
 * Copyright (c) Meta Platforms, Inc. and 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);
    });
  }
};

/**
 * Copyright (c) Meta Platforms, Inc. and 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.onerror = (event) => {
      console.error('"onerror" event intercepted:', event);
    };
    window.onunhandledrejection = (event) => {
      console.error('"unhandledrejection" event intercepted:', event.reason);
    };
  }
};

Use event handlers instead of listeners for error handing
Use event handlers instead of listeners for error handing

Summary:
^

Adding listeners was correctly intercepting events but the event object often lacked the information necessary to correctly triage and fix an issue.

I discovered that by subscribing to these events directly, the event object did have the required information.

Changelog: Use global window event handlers instead of listeners

Reviewed By: antonk52

Differential Revision: D39809292

fbshipit-source-id: 8a0fc7b7cd86ea16e40f2dc383bc30364f6fc16c
