Skip to content

Commit aa98868

Browse files
VIA-357 MD/DB Add event listener for unhandled asynchronous client side error logging
Note that this will result in all unhandled errors in the client outputting a log line stating that an unhandled error has happened. COntext on what happened is impossible to pass in without accepting raw text from an endpoint which may present a security risk. Error types can be intercepted and directly specified in client-error-logger if required.
1 parent d5c76e0 commit aa98868

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"use client";
2+
3+
import logClientSideError from "@src/utils/client-side-error-logger/client-side-error-logger";
4+
import { ClientSideErrorTypes } from "@src/utils/constants";
5+
import { useEffect } from "react";
6+
7+
const ClientUnhandledErrorLogger = (): undefined => {
8+
useEffect(() => {
9+
window.addEventListener("unhandledrejection", function () {
10+
logClientSideError(ClientSideErrorTypes.UNHANDLED_ERROR).catch(() => {
11+
// do not show anything to the user; catching prevents an infinite loop if the logger itself throws an error which is unhandled
12+
});
13+
});
14+
}, []);
15+
};
16+
17+
export { ClientUnhandledErrorLogger };

src/app/layout.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import "@public/css/app.css";
22
import "@public/css/nhsapp-3.1.0.min.css";
33
import "@public/css/nhsuk-9.6.1.min.css";
4+
import { ClientUnhandledErrorLogger } from "@src/app/_components/client-unhandled-error-logger/ClientUnhandledErrorLogger";
45
import { ClientProviders } from "@src/app/_components/context/ClientProviders";
56
import { NoJsMessage } from "@src/app/_components/static/NoJsMessage";
67
import { StaticFooter } from "@src/app/_components/static/StaticFooter";
@@ -43,6 +44,7 @@ export default function RootLayout({
4344
<NoJsMessage />
4445
<StaticFooter />
4546
</noscript>
47+
<ClientUnhandledErrorLogger />
4648
<div id="app-root" className="appLayout">
4749
<ClientProviders>{children}</ClientProviders>
4850
</div>

0 commit comments

Comments
 (0)