|
3 | 3 | import logClientSideError from "@src/utils/client-side-error-logger/client-side-error-logger"; |
4 | 4 | import { ClientSideErrorTypes } from "@src/utils/constants"; |
5 | 5 | import { useRouter } from "next/navigation"; |
6 | | -import { useEffect } from "react"; |
7 | | - |
8 | | -let router; |
9 | | - |
10 | | -const reportClientSideUnhandledError = (errorEvent: ErrorEvent) => { |
11 | | - errorEvent.preventDefault(); |
12 | | - |
13 | | - logClientSideError(ClientSideErrorTypes.UNHANDLED_ERROR) |
14 | | - .then((logOnClientConsole: boolean) => { |
15 | | - if (logOnClientConsole) { |
16 | | - console.log("Unhandled error event", errorEvent); |
17 | | - } |
18 | | - }) |
19 | | - .catch(() => { |
20 | | - // do not show anything to the user; catching prevents an infinite loop if the logger itself throws an error which is unhandled |
21 | | - }); |
22 | | - |
23 | | - router.push("/service-failure"); |
24 | | -}; |
25 | | - |
26 | | -const reportClientSideUnhandledPromiseRejectionError = (promiseRejectionEvent: PromiseRejectionEvent) => { |
27 | | - promiseRejectionEvent.preventDefault(); |
28 | | - |
29 | | - logClientSideError(ClientSideErrorTypes.UNHANDLED_PROMISE_REJECT_ERROR) |
30 | | - .then((logOnClientConsole: boolean) => { |
31 | | - if (logOnClientConsole) { |
32 | | - console.log("Unhandled promise rejection event", promiseRejectionEvent); |
33 | | - } |
34 | | - }) |
35 | | - .catch(() => { |
36 | | - // do not show anything to the user; catching prevents an infinite loop if the logger itself throws an error which is unhandled |
37 | | - }); |
38 | | - |
39 | | - router.push("/service-failure"); |
40 | | -}; |
| 6 | +import { useCallback, useEffect } from "react"; |
41 | 7 |
|
42 | 8 | const ClientUnhandledErrorLogger = (): null => { |
43 | | - router = useRouter(); |
| 9 | + const router = useRouter(); |
| 10 | + |
| 11 | + const reportUnhandledError = useCallback((errorEvent: ErrorEvent) => { |
| 12 | + errorEvent.preventDefault(); |
| 13 | + |
| 14 | + logClientSideError(ClientSideErrorTypes.UNHANDLED_ERROR) |
| 15 | + .then((logOnClientConsole: boolean) => { |
| 16 | + if (logOnClientConsole) { |
| 17 | + console.log("Unhandled error event", errorEvent); |
| 18 | + } |
| 19 | + }) |
| 20 | + .catch(() => { |
| 21 | + // do not show anything to the user; catching prevents an infinite loop if the logger itself throws an error which is unhandled |
| 22 | + }); |
| 23 | + |
| 24 | + router.push("/service-failure"); |
| 25 | + }, [router]); |
| 26 | + |
| 27 | + const reportPromiseRejectionError = useCallback((promiseRejectionEvent: PromiseRejectionEvent) => { |
| 28 | + promiseRejectionEvent.preventDefault(); |
| 29 | + |
| 30 | + logClientSideError(ClientSideErrorTypes.UNHANDLED_PROMISE_REJECT_ERROR) |
| 31 | + .then((logOnClientConsole: boolean) => { |
| 32 | + if (logOnClientConsole) { |
| 33 | + console.log("Unhandled promise rejection event", promiseRejectionEvent); |
| 34 | + } |
| 35 | + }) |
| 36 | + .catch(() => { |
| 37 | + // do not show anything to the user; catching prevents an infinite loop if the logger itself throws an error which is unhandled |
| 38 | + }); |
| 39 | + |
| 40 | + router.push("/service-failure"); |
| 41 | + }, [router]); |
44 | 42 |
|
45 | 43 | useEffect(() => { |
46 | | - window.addEventListener("unhandledrejection", (event) => reportClientSideUnhandledPromiseRejectionError(event)); |
47 | | - window.addEventListener("error", (event) => reportClientSideUnhandledError(event)); |
| 44 | + console.log("mounted"); |
| 45 | + window.addEventListener("unhandledrejection", reportPromiseRejectionError); |
| 46 | + window.addEventListener("error", reportUnhandledError); |
48 | 47 |
|
49 | 48 | // on component unmount |
50 | 49 | return () => { |
51 | | - window.removeEventListener("unhandledrejection", (event) => |
52 | | - reportClientSideUnhandledPromiseRejectionError(event), |
53 | | - ); |
54 | | - window.removeEventListener("error", (event) => reportClientSideUnhandledError(event)); |
| 50 | + console.log("unmounted"); |
| 51 | + window.removeEventListener("unhandledrejection", reportPromiseRejectionError); |
| 52 | + window.removeEventListener("error", reportUnhandledError); |
55 | 53 | }; |
56 | | - }, []); |
| 54 | + }, [reportUnhandledError, reportPromiseRejectionError]); |
57 | 55 |
|
58 | 56 | return null; |
59 | 57 | }; |
|
0 commit comments