Skip to content

Commit e92e574

Browse files
VIA-545 AJ/DB Server decides whether to print console logs on client
1 parent 9baa21a commit e92e574

File tree

9 files changed

+49
-35
lines changed

9 files changed

+49
-35
lines changed

.env.template

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ SSM_PREFIX=/local/
33

44
# logging
55
PINO_LOG_LEVEL=info
6-
NEXT_PUBLIC_SUPRESS_CONSOLE=false
76
PROFILE_PERFORMANCE=true
87
DEPLOY_ENVIRONMENT=local
98

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,4 @@ terraform.tfstate*
124124

125125
# Content validation
126126
*.temp.json
127+
/performance/report/

eslint.config.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ const eslintConfig = [{
2323
"**/.terraform/**",
2424
".open-next/**",
2525
"playwright-report/**",
26-
"pact/**"
26+
"pact/**",
27+
"performance/report",
2728
]
2829
}, ...compat.extends(
2930
"next/core-web-vitals",

infrastructure/environments/dev/locals.tf

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@ locals {
2525
application_environment_variables = {
2626
SSM_PREFIX = "/${local.prefix}/"
2727

28-
PINO_LOG_LEVEL = "info"
29-
NEXT_PUBLIC_SUPRESS_CONSOLE = "false"
30-
DEPLOY_ENVIRONMENT = local.environment
31-
PROFILE_PERFORMANCE = "true"
28+
PINO_LOG_LEVEL = "info"
29+
DEPLOY_ENVIRONMENT = local.environment
30+
PROFILE_PERFORMANCE = "true"
3231

3332
CONTENT_API_ENDPOINT = "https://int.api.service.nhs.uk/"
3433
CONTENT_CACHE_PATH = "s3://${local.content_cache_bucket_name}"

infrastructure/environments/preprod/locals.tf

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@ locals {
2525
application_environment_variables = {
2626
SSM_PREFIX = "/${local.prefix}/"
2727

28-
PINO_LOG_LEVEL = "info"
29-
NEXT_PUBLIC_SUPRESS_CONSOLE = "false"
30-
DEPLOY_ENVIRONMENT = local.environment
31-
PROFILE_PERFORMANCE = "true"
28+
PINO_LOG_LEVEL = "info"
29+
DEPLOY_ENVIRONMENT = local.environment
30+
PROFILE_PERFORMANCE = "true"
3231

3332
CONTENT_API_ENDPOINT = "https://int.api.service.nhs.uk/"
3433
CONTENT_CACHE_PATH = "s3://${local.content_cache_bucket_name}"

infrastructure/environments/prod/locals.tf

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@ locals {
2525
application_environment_variables = {
2626
SSM_PREFIX = "/${local.prefix}/"
2727

28-
PINO_LOG_LEVEL = "info"
29-
NEXT_PUBLIC_SUPRESS_CONSOLE = "true"
30-
DEPLOY_ENVIRONMENT = local.environment
31-
PROFILE_PERFORMANCE = "true"
28+
PINO_LOG_LEVEL = "info"
29+
DEPLOY_ENVIRONMENT = local.environment
30+
PROFILE_PERFORMANCE = "true"
3231

3332
CONTENT_API_ENDPOINT = "https://api.service.nhs.uk/"
3433
CONTENT_CACHE_PATH = "s3://${local.content_cache_bucket_name}"

infrastructure/environments/test/locals.tf

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@ locals {
2525
application_environment_variables = {
2626
SSM_PREFIX = "/${local.prefix}/"
2727

28-
PINO_LOG_LEVEL = "debug"
29-
NEXT_PUBLIC_SUPRESS_CONSOLE = "true"
30-
DEPLOY_ENVIRONMENT = local.environment
31-
PROFILE_PERFORMANCE = "true"
28+
PINO_LOG_LEVEL = "debug"
29+
DEPLOY_ENVIRONMENT = local.environment
30+
PROFILE_PERFORMANCE = "true"
3231

3332
CONTENT_API_ENDPOINT = "https://int.api.service.nhs.uk/"
3433
CONTENT_CACHE_PATH = "s3://${local.content_cache_bucket_name}"

src/app/_components/client-unhandled-error-logger/ClientUnhandledErrorLogger.tsx

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,53 @@ import { ClientSideErrorTypes } from "@src/utils/constants";
55
import { useRouter } from "next/navigation";
66
import { useEffect } from "react";
77

8-
const supressConsole = (process.env.NEXT_PUBLIC_SUPRESS_CONSOLE ?? "true") !== "false";
98
let router;
109

1110
const reportClientSideUnhandledError = (errorEvent: ErrorEvent) => {
12-
if (supressConsole) errorEvent.preventDefault();
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+
});
1322

14-
logClientSideError(ClientSideErrorTypes.UNHANDLED_ERROR).catch((err: Error) => {
15-
if (!supressConsole) console.error(err.message);
16-
// do not show anything to the user; catching prevents an infinite loop if the logger itself throws an error which is unhandled
17-
});
1823
router.push("/service-failure");
1924
};
2025

21-
const reportClientSideUnhandledPromiseRejectionError = () => {
22-
logClientSideError(ClientSideErrorTypes.UNHANDLED_PROMISE_REJECT_ERROR).catch((err: Error) => {
23-
if (!supressConsole) console.error(err.message);
24-
// do not show anything to the user; catching prevents an infinite loop if the logger itself throws an error which is unhandled
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+
2639
router.push("/service-failure");
2740
};
2841

2942
const ClientUnhandledErrorLogger = (): undefined => {
3043
router = useRouter();
3144

3245
useEffect(() => {
33-
window.addEventListener("unhandledrejection", reportClientSideUnhandledPromiseRejectionError);
34-
window.addEventListener("error", (event: ErrorEvent) => reportClientSideUnhandledError(event));
46+
window.addEventListener("unhandledrejection", (event) => reportClientSideUnhandledPromiseRejectionError(event));
47+
window.addEventListener("error", (event) => reportClientSideUnhandledError(event));
3548

3649
// on component unmount
3750
return () => {
38-
window.removeEventListener("unhandledrejection", reportClientSideUnhandledPromiseRejectionError);
39-
window.removeEventListener("error", (event: ErrorEvent) => reportClientSideUnhandledError(event));
51+
window.removeEventListener("unhandledrejection", (event) =>
52+
reportClientSideUnhandledPromiseRejectionError(event),
53+
);
54+
window.removeEventListener("error", (event) => reportClientSideUnhandledError(event));
4055
};
4156
}, []);
4257
};

src/utils/client-side-error-logger/client-side-error-logger.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,19 @@ import { Logger } from "pino";
77

88
const log: Logger = logger.child({ module: "client-side-error-logger" });
99

10-
const logClientSideError = async (clientSideErrorType: ClientSideErrorTypes): Promise<void> => {
10+
const logClientSideError = async (clientSideErrorType: ClientSideErrorTypes): Promise<boolean> => {
1111
return requestScopedStorageWrapper(logClientSideErrorAction, clientSideErrorType);
1212
};
1313

14-
const logClientSideErrorAction = async (clientSideErrorType: ClientSideErrorTypes): Promise<void> => {
14+
const logClientSideErrorAction = async (clientSideErrorType: ClientSideErrorTypes): Promise<boolean> => {
1515
const validClientSideErrorTypes = Object.values(ClientSideErrorTypes) as string[];
1616
const validatedErrorType = validClientSideErrorTypes.includes(clientSideErrorType)
1717
? clientSideErrorType
1818
: ClientSideErrorTypes.UNKNOWN_ERROR_REASON;
1919

2020
log.error({ context: { clientSideErrorType: validatedErrorType } }, "Client side error occurred");
21+
22+
return process.env.DEPLOY_ENVIRONMENT !== "prod" && process.env.DEPLOY_ENVIRONMENT !== "production";
2123
};
2224

2325
export default logClientSideError;

0 commit comments

Comments
 (0)