From 37028f2bb80b532bf22092ca837c1f754eda93d1 Mon Sep 17 00:00:00 2001 From: Oliver Eyton-Williams Date: Tue, 22 Apr 2025 18:36:27 +0200 Subject: [PATCH] chore(api): log growthbook initialization failures (#59889) Co-authored-by: Mrugesh Mohapatra <1884376+raisedadead@users.noreply.github.com> Co-authored-by: Shaun Hamilton --- api/src/app.ts | 7 ------- api/src/plugins/growth-book.test.ts | 29 +++++++++++++++++++++++++++++ api/src/plugins/growth-book.ts | 13 ++++++++++++- 3 files changed, 41 insertions(+), 8 deletions(-) create mode 100644 api/src/plugins/growth-book.test.ts diff --git a/api/src/app.ts b/api/src/app.ts index 28eae4a1457..9c98251a8b5 100644 --- a/api/src/app.ts +++ b/api/src/app.ts @@ -2,7 +2,6 @@ import fastifyAccepts from '@fastify/accepts'; import fastifySwagger from '@fastify/swagger'; import fastifySwaggerUI from '@fastify/swagger-ui'; import type { TypeBoxTypeProvider } from '@fastify/type-provider-typebox'; -import { GrowthBook } from '@growthbook/growthbook'; import Ajv from 'ajv'; import addFormats from 'ajv-formats'; import uriResolver from 'fast-uri'; @@ -60,12 +59,6 @@ type FastifyInstanceWithTypeProvider = FastifyInstance< TypeBoxTypeProvider >; -declare module 'fastify' { - interface FastifyInstance { - gb: GrowthBook; - } -} - // Options that fastify uses const ajv = new Ajv({ coerceTypes: 'array', // change data type of data to match type keyword diff --git a/api/src/plugins/growth-book.test.ts b/api/src/plugins/growth-book.test.ts new file mode 100644 index 00000000000..569ce4ac403 --- /dev/null +++ b/api/src/plugins/growth-book.test.ts @@ -0,0 +1,29 @@ +import Fastify, { type FastifyInstance } from 'fastify'; +import growthBook from './growth-book'; + +const captureException = jest.fn(); + +describe('growth-book', () => { + let fastify: FastifyInstance; + beforeAll(() => { + fastify = Fastify(); + // @ts-expect-error we're mocking the Sentry plugin + fastify.Sentry = { captureException }; + }); + + afterAll(async () => { + await fastify.close(); + }); + + it('should log the error if the GrowthBook initialization fails', async () => { + const spy = jest.spyOn(fastify.log, 'error'); + + await fastify.register(growthBook, { + apiHost: 'invalid-url', + clientKey: 'invalid-key' + }); + + expect(spy).toHaveBeenCalled(); + expect(captureException).toHaveBeenCalled(); + }); +}); diff --git a/api/src/plugins/growth-book.ts b/api/src/plugins/growth-book.ts index 3824e95774a..d8a57ac6d5b 100644 --- a/api/src/plugins/growth-book.ts +++ b/api/src/plugins/growth-book.ts @@ -2,9 +2,20 @@ import { GrowthBook, Options } from '@growthbook/growthbook'; import { FastifyPluginAsync } from 'fastify'; import fp from 'fastify-plugin'; +declare module 'fastify' { + interface FastifyInstance { + gb: GrowthBook; + } +} + const growthBook: FastifyPluginAsync = async (fastify, options) => { const gb = new GrowthBook(options); - await gb.init({ timeout: 3000 }); + const res = await gb.init({ timeout: 3000 }); + + if (res.error) { + fastify.log.error(res.error, 'Failed to initialize GrowthBook'); + fastify.Sentry.captureException(res.error); + } fastify.decorate('gb', gb);