From 14df93bcefb71e6464f5eb6e0ed5a620a28c4d51 Mon Sep 17 00:00:00 2001 From: Oliver Eyton-Williams Date: Thu, 13 Jun 2024 19:18:16 +0200 Subject: [PATCH] fix(api): use generic error handler with Sentry (#55176) --- api/src/app.ts | 20 ++++++++++++++++--- client/i18n/locales/english/translations.json | 2 +- .../components/Flash/redux/flash-messages.ts | 2 +- client/src/utils/error-messages.ts | 2 +- client/src/utils/tone/index.ts | 2 +- 5 files changed, 21 insertions(+), 7 deletions(-) diff --git a/api/src/app.ts b/api/src/app.ts index 0048ec8d288..ef1d37e9643 100644 --- a/api/src/app.ts +++ b/api/src/app.ts @@ -105,9 +105,23 @@ export const build = async ( }); // NOTE: Awaited to ensure `.use` is registered on `fastify` await fastify.register(express); - if (SENTRY_DSN) { - await fastify.register(fastifySentry, { dsn: SENTRY_DSN }); - } + + await fastify.register(fastifySentry, { + dsn: SENTRY_DSN, + // No need to initialize if DSN is not provided (e.g. in development and + // test environments) + skipInit: !!SENTRY_DSN, + errorResponse: (error, _request, reply) => { + if (reply.statusCode === 500) { + void reply.send({ + message: 'flash.generic-error', + type: 'danger' + }); + } else { + void reply.send(error); + } + } + }); await fastify.register(cors); await fastify.register(cookies); diff --git a/client/i18n/locales/english/translations.json b/client/i18n/locales/english/translations.json index 14155ed823a..eec759e51c3 100644 --- a/client/i18n/locales/english/translations.json +++ b/client/i18n/locales/english/translations.json @@ -749,7 +749,7 @@ "flash": { "honest-first": "To claim a certification, you must first agree to our academic honesty policy", "really-weird": "Something really weird happened, if it happens again, please consider raising an issue on https://github.com/freeCodeCamp/freeCodeCamp/issues/new", - "not-right": "Something is not quite right. A report has been generated and the freeCodeCamp.org team have been notified", + "generic-error": "Something went wrong. Please try again in a moment or contact support@freecodecamp.org if the error persists.", "went-wrong": "Something went wrong, please check and try again", "account-deleted": "Your account has been successfully deleted", "progress-reset": "Your progress has been reset", diff --git a/client/src/components/Flash/redux/flash-messages.ts b/client/src/components/Flash/redux/flash-messages.ts index d74e6de583f..bd50688b800 100644 --- a/client/src/components/Flash/redux/flash-messages.ts +++ b/client/src/components/Flash/redux/flash-messages.ts @@ -39,7 +39,7 @@ export enum FlashMessages { None = '', NotEligible = 'flash.not-eligible', NotHonest = 'flash.not-honest', - NotRight = 'flash.not-right', + GenericError = 'flash.generic-error', ProfilePrivate = 'flash.profile-private', ProgressReset = 'flash.progress-reset', ProvideUsername = 'flash.provide-username', diff --git a/client/src/utils/error-messages.ts b/client/src/utils/error-messages.ts index 19f49d81d0f..5d2b081d244 100644 --- a/client/src/utils/error-messages.ts +++ b/client/src/utils/error-messages.ts @@ -12,7 +12,7 @@ export const reallyWeirdErrorMessage = { export const reportedErrorMessage = { type: 'danger', - message: FlashMessages.NotRight + message: FlashMessages.GenericError }; export const certificateMissingErrorMessage = { diff --git a/client/src/utils/tone/index.ts b/client/src/utils/tone/index.ts index ae6e543bd5a..d0d0caaa87c 100644 --- a/client/src/utils/tone/index.ts +++ b/client/src/utils/tone/index.ts @@ -54,7 +54,7 @@ const toneUrls = { // [FlashMessages.None]: '', [FlashMessages.NotEligible]: TRY_AGAIN, [FlashMessages.NotHonest]: TRY_AGAIN, - [FlashMessages.NotRight]: TRY_AGAIN, + [FlashMessages.GenericError]: TRY_AGAIN, [FlashMessages.ProfilePrivate]: TRY_AGAIN, [FlashMessages.ProgressReset]: TRY_AGAIN, [FlashMessages.ProvideUsername]: TRY_AGAIN,