Skip to content

Commit 9fb7f4e

Browse files
w01fgangclaude
andcommitted
fix: always warn on thrown breadcrumb transformer
Transformer throw returned {} and tripped the empty-data guard in addBreadcrumbsIfConfigured, silently dropping the breadcrumb unless debug was enabled. Warn unconditionally so production loss is visible; keep the detailed console.error gated on debug. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 3b8676d commit 9fb7f4e

2 files changed

Lines changed: 44 additions & 10 deletions

File tree

src/__tests__/flexible-breadcrumbs.test.ts

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,12 @@ describe('Flexible Breadcrumbs System', () => {
305305

306306
describe('Error Handling', () => {
307307
it('should handle transformer errors gracefully with debug enabled', async () => {
308-
const consoleSpy = vi
308+
const errorSpy = vi
309309
.spyOn(console, 'error')
310310
.mockImplementation(() => {});
311+
const warnSpy = vi
312+
.spyOn(console, 'warn')
313+
.mockImplementation(() => {});
311314

312315
function testFunction(data: string) {
313316
throw new Error('test');
@@ -325,20 +328,28 @@ describe('Flexible Breadcrumbs System', () => {
325328
])
326329
.value();
327330

328-
expect(consoleSpy).toHaveBeenCalledWith(
331+
expect(warnSpy).toHaveBeenCalledWith(
332+
'[try-catch] breadcrumb transformer threw; breadcrumb dropped:',
333+
expect.any(Error),
334+
);
335+
expect(errorSpy).toHaveBeenCalledWith(
329336
'Error in breadcrumb transformer:',
330337
expect.any(Error),
331338
);
332339
// Transformer error yields empty data — reporter call is short-circuited.
333340
expect(Sentry.addBreadcrumb).not.toHaveBeenCalled();
334341

335-
consoleSpy.mockRestore();
342+
errorSpy.mockRestore();
343+
warnSpy.mockRestore();
336344
});
337345

338-
it('should handle transformer errors gracefully with debug disabled', async () => {
339-
const consoleSpy = vi
346+
it('should always warn on transformer errors even with debug disabled', async () => {
347+
const errorSpy = vi
340348
.spyOn(console, 'error')
341349
.mockImplementation(() => {});
350+
const warnSpy = vi
351+
.spyOn(console, 'warn')
352+
.mockImplementation(() => {});
342353

343354
function testFunction(data: string) {
344355
throw new Error('test');
@@ -355,17 +366,25 @@ describe('Flexible Breadcrumbs System', () => {
355366
])
356367
.value();
357368

358-
expect(consoleSpy).not.toHaveBeenCalled();
369+
expect(warnSpy).toHaveBeenCalledWith(
370+
'[try-catch] breadcrumb transformer threw; breadcrumb dropped:',
371+
expect.any(Error),
372+
);
373+
expect(errorSpy).not.toHaveBeenCalled();
359374
// Transformer error yields empty data — reporter call is short-circuited.
360375
expect(Sentry.addBreadcrumb).not.toHaveBeenCalled();
361376

362-
consoleSpy.mockRestore();
377+
errorSpy.mockRestore();
378+
warnSpy.mockRestore();
363379
});
364380

365381
it('should handle predefined transformer errors gracefully', async () => {
366-
const consoleSpy = vi
382+
const errorSpy = vi
367383
.spyOn(console, 'error')
368384
.mockImplementation(() => {});
385+
const warnSpy = vi
386+
.spyOn(console, 'warn')
387+
.mockImplementation(() => {});
369388

370389
function testFunction(data: any) {
371390
throw new Error('test');
@@ -382,11 +401,16 @@ describe('Flexible Breadcrumbs System', () => {
382401
.breadcrumbs([{ param: 0, as: 'toString' }])
383402
.value();
384403

385-
expect(consoleSpy).toHaveBeenCalledWith(
404+
expect(warnSpy).toHaveBeenCalledWith(
405+
'[try-catch] predefined breadcrumb transformer threw; breadcrumb dropped:',
406+
expect.any(Error),
407+
);
408+
expect(errorSpy).toHaveBeenCalledWith(
386409
'Error in predefined transformer:',
387410
expect.any(Error),
388411
);
389-
consoleSpy.mockRestore();
412+
errorSpy.mockRestore();
413+
warnSpy.mockRestore();
390414
});
391415

392416
it('should handle non-object parameters for key extraction gracefully', async () => {

src/utils/transformers.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ export class TransformerRegistry {
6666
'custom',
6767
);
6868

69+
// Always warn: a thrown transformer yields empty data, which trips the
70+
// guard in addBreadcrumbsIfConfigured and silently drops the breadcrumb.
71+
console.warn(
72+
'[try-catch] breadcrumb transformer threw; breadcrumb dropped:',
73+
transformationError,
74+
);
6975
if (debug) {
7076
console.error('Error in breadcrumb transformer:', transformationError);
7177
}
@@ -93,6 +99,10 @@ export class TransformerRegistry {
9399
paramIndex,
94100
);
95101

102+
console.warn(
103+
'[try-catch] predefined breadcrumb transformer threw; breadcrumb dropped:',
104+
transformationError,
105+
);
96106
if (debug) {
97107
console.error('Error in predefined transformer:', transformationError);
98108
}

0 commit comments

Comments
 (0)