mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-03-12 07:02:42 -04:00
chore(api): log growthbook initialization failures (#59889)
Co-authored-by: Mrugesh Mohapatra <1884376+raisedadead@users.noreply.github.com> Co-authored-by: Shaun Hamilton <shauhami020@gmail.com>
This commit is contained in:
committed by
GitHub
parent
6d06eaa5c0
commit
37028f2bb8
29
api/src/plugins/growth-book.test.ts
Normal file
29
api/src/plugins/growth-book.test.ts
Normal file
@@ -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();
|
||||
});
|
||||
});
|
||||
@@ -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<Options> = 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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user