From 3c822da24372ea9b0c0e40bf2252c70c76396427 Mon Sep 17 00:00:00 2001 From: Mrugesh Mohapatra <1884376+raisedadead@users.noreply.github.com> Date: Thu, 24 Apr 2025 10:26:33 +0530 Subject: [PATCH] fix(api): catch GB init errors on prod only (#59912) Co-authored-by: Oliver Eyton-Williams --- api/src/plugins/growth-book.test.ts | 7 +++++++ api/src/plugins/growth-book.ts | 4 +++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/api/src/plugins/growth-book.test.ts b/api/src/plugins/growth-book.test.ts index 569ce4ac403..3fe5740b4e8 100644 --- a/api/src/plugins/growth-book.test.ts +++ b/api/src/plugins/growth-book.test.ts @@ -1,6 +1,13 @@ import Fastify, { type FastifyInstance } from 'fastify'; import growthBook from './growth-book'; +// eslint-disable-next-line @typescript-eslint/no-unsafe-return +jest.mock('../utils/env', () => ({ + ...jest.requireActual('../utils/env'), + // We're only interested in the production behaviour + FREECODECAMP_NODE_ENV: 'production' +})); + const captureException = jest.fn(); describe('growth-book', () => { diff --git a/api/src/plugins/growth-book.ts b/api/src/plugins/growth-book.ts index d8a57ac6d5b..6d81bc19ad4 100644 --- a/api/src/plugins/growth-book.ts +++ b/api/src/plugins/growth-book.ts @@ -2,6 +2,8 @@ import { GrowthBook, Options } from '@growthbook/growthbook'; import { FastifyPluginAsync } from 'fastify'; import fp from 'fastify-plugin'; +import { FREECODECAMP_NODE_ENV } from '../utils/env'; + declare module 'fastify' { interface FastifyInstance { gb: GrowthBook; @@ -12,7 +14,7 @@ const growthBook: FastifyPluginAsync = async (fastify, options) => { const gb = new GrowthBook(options); const res = await gb.init({ timeout: 3000 }); - if (res.error) { + if (res.error && FREECODECAMP_NODE_ENV === 'production') { fastify.log.error(res.error, 'Failed to initialize GrowthBook'); fastify.Sentry.captureException(res.error); }