fix(api): catch GB init errors on prod only (#59912)

Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
This commit is contained in:
Mrugesh Mohapatra
2025-04-24 10:26:33 +05:30
committed by GitHub
parent 34ee68faa8
commit 3c822da243
2 changed files with 10 additions and 1 deletions
+7
View File
@@ -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', () => {
+3 -1
View File
@@ -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<Options> = 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);
}