1*26ad19fcSEvan Baconimport React from 'react';
2*26ad19fcSEvan Bacon
3*26ad19fcSEvan Baconimport ExceptionsManager from './modules/ExceptionsManager';
4*26ad19fcSEvan Bacon
5*26ad19fcSEvan Baconfunction useStackTraceLimit(limit: number) {
6*26ad19fcSEvan Bacon  const current = React.useRef(0);
7*26ad19fcSEvan Bacon  React.useEffect(() => {
8*26ad19fcSEvan Bacon    try {
9*26ad19fcSEvan Bacon      // @ts-expect-error: StackTraceLimit is not defined in the Error type
10*26ad19fcSEvan Bacon      const currentLimit = Error.stackTraceLimit;
11*26ad19fcSEvan Bacon      // @ts-expect-error: StackTraceLimit is not defined in the Error type
12*26ad19fcSEvan Bacon      Error.stackTraceLimit = limit;
13*26ad19fcSEvan Bacon      current.current = currentLimit;
14*26ad19fcSEvan Bacon    } catch {}
15*26ad19fcSEvan Bacon    return () => {
16*26ad19fcSEvan Bacon      try {
17*26ad19fcSEvan Bacon        // @ts-expect-error: StackTraceLimit is not defined in the Error type
18*26ad19fcSEvan Bacon        Error.stackTraceLimit = current.current;
19*26ad19fcSEvan Bacon      } catch {}
20*26ad19fcSEvan Bacon    };
21*26ad19fcSEvan Bacon  }, [limit]);
22*26ad19fcSEvan Bacon}
23*26ad19fcSEvan Bacon
24*26ad19fcSEvan Baconexport function useRejectionHandler() {
25*26ad19fcSEvan Bacon  const hasError = React.useRef(false);
26*26ad19fcSEvan Bacon
27*26ad19fcSEvan Bacon  useStackTraceLimit(35);
28*26ad19fcSEvan Bacon
29*26ad19fcSEvan Bacon  React.useEffect(() => {
30*26ad19fcSEvan Bacon    function onUnhandledError(ev: ErrorEvent) {
31*26ad19fcSEvan Bacon      hasError.current = true;
32*26ad19fcSEvan Bacon
33*26ad19fcSEvan Bacon      const error = ev?.error;
34*26ad19fcSEvan Bacon      if (!error || !(error instanceof Error) || typeof error.stack !== 'string') {
35*26ad19fcSEvan Bacon        return;
36*26ad19fcSEvan Bacon      }
37*26ad19fcSEvan Bacon
38*26ad19fcSEvan Bacon      ExceptionsManager.handleException(error);
39*26ad19fcSEvan Bacon    }
40*26ad19fcSEvan Bacon
41*26ad19fcSEvan Bacon    function onUnhandledRejection(ev: PromiseRejectionEvent) {
42*26ad19fcSEvan Bacon      hasError.current = true;
43*26ad19fcSEvan Bacon
44*26ad19fcSEvan Bacon      const reason = ev?.reason;
45*26ad19fcSEvan Bacon      if (!reason || !(reason instanceof Error) || typeof reason.stack !== 'string') {
46*26ad19fcSEvan Bacon        return;
47*26ad19fcSEvan Bacon      }
48*26ad19fcSEvan Bacon
49*26ad19fcSEvan Bacon      ExceptionsManager.handleException(reason);
50*26ad19fcSEvan Bacon    }
51*26ad19fcSEvan Bacon
52*26ad19fcSEvan Bacon    window.addEventListener('unhandledrejection', onUnhandledRejection);
53*26ad19fcSEvan Bacon    window.addEventListener('error', onUnhandledError);
54*26ad19fcSEvan Bacon    return () => {
55*26ad19fcSEvan Bacon      window.removeEventListener('error', onUnhandledError);
56*26ad19fcSEvan Bacon      window.removeEventListener('unhandledrejection', onUnhandledRejection);
57*26ad19fcSEvan Bacon    };
58*26ad19fcSEvan Bacon  }, []);
59*26ad19fcSEvan Bacon
60*26ad19fcSEvan Bacon  return hasError;
61*26ad19fcSEvan Bacon}
62