From 6729214ab7963071c54b28fbaee737a2f306e33d Mon Sep 17 00:00:00 2001 From: Charly Gomez Date: Mon, 4 Nov 2024 12:41:45 +0100 Subject: [PATCH] fix(core): Silently fail maybeInstrument (#14140) Co-authored-by: Francesco Novy --- .size-limit.js | 2 +- packages/utils/src/instrument/handlers.ts | 6 +++++- packages/utils/test/instrument.test.ts | 13 +++++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 packages/utils/test/instrument.test.ts diff --git a/.size-limit.js b/.size-limit.js index 75545fd89194..cd2a3630ef5c 100644 --- a/.size-limit.js +++ b/.size-limit.js @@ -224,7 +224,7 @@ module.exports = [ import: createImport('init'), ignore: ['next/router', 'next/constants'], gzip: true, - limit: '39.1 KB', + limit: '40 KB', }, // SvelteKit SDK (ESM) { diff --git a/packages/utils/src/instrument/handlers.ts b/packages/utils/src/instrument/handlers.ts index b22f01318b0a..672c819e17a6 100644 --- a/packages/utils/src/instrument/handlers.ts +++ b/packages/utils/src/instrument/handlers.ts @@ -37,8 +37,12 @@ export function resetInstrumentationHandlers(): void { /** Maybe run an instrumentation function, unless it was already called. */ export function maybeInstrument(type: InstrumentHandlerType, instrumentFn: () => void): void { if (!instrumented[type]) { - instrumentFn(); instrumented[type] = true; + try { + instrumentFn(); + } catch (e) { + DEBUG_BUILD && logger.error(`Error while instrumenting ${type}`, e); + } } } diff --git a/packages/utils/test/instrument.test.ts b/packages/utils/test/instrument.test.ts new file mode 100644 index 000000000000..440a19121a1a --- /dev/null +++ b/packages/utils/test/instrument.test.ts @@ -0,0 +1,13 @@ +import { maybeInstrument } from '../src'; + +describe('maybeInstrument', () => { + test('does not throw when instrumenting fails', () => { + maybeInstrument('xhr', () => { + throw new Error('test'); + }); + }); + + test('does not throw when instrumenting fn is not a function', () => { + maybeInstrument('xhr', undefined as any); + }); +});