Skip to content

Commit 58de615

Browse files
committed
split exec state and breadcrumbs
1 parent 2a568a1 commit 58de615

2 files changed

Lines changed: 57 additions & 8 deletions

File tree

src/__tests__/flexible-breadcrumbs.test.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,5 +566,51 @@ describe('Flexible Breadcrumbs System', () => {
566566
expect(transformSpy).toHaveBeenCalledTimes(1);
567567
expect(Sentry.addBreadcrumb).toHaveBeenCalledTimes(1);
568568
});
569+
570+
it('child inherits breadcrumb config set before .default()', async () => {
571+
const params = { userId: 123, action: 'update' };
572+
573+
// breadcrumbs configured before default() — not overridden after
574+
const result = await new Try(throwingFunction, params)
575+
.breadcrumbs(['userId', 'action'])
576+
.default({ ok: false })
577+
.value();
578+
579+
expect(result).toEqual({ ok: false });
580+
expect(Sentry.addBreadcrumb).toHaveBeenCalledTimes(1);
581+
expect(Sentry.addBreadcrumb).toHaveBeenCalledWith(
582+
expect.objectContaining({
583+
data: { userId: 123, action: 'update' },
584+
}),
585+
);
586+
});
587+
588+
it('parent and child (via .default()) can have divergent breadcrumb configs', async () => {
589+
const params = { userId: 123, action: 'update', role: 'admin' };
590+
591+
const parent = new Try(throwingFunction, params).breadcrumbs([
592+
'userId',
593+
'action',
594+
]);
595+
const child = parent.default({ ok: false }).breadcrumbs(['role']);
596+
597+
// Parent runs first and caches its breadcrumbs.
598+
await parent.value();
599+
await child.value();
600+
601+
expect(Sentry.addBreadcrumb).toHaveBeenCalledTimes(2);
602+
expect(Sentry.addBreadcrumb).toHaveBeenNthCalledWith(
603+
1,
604+
expect.objectContaining({
605+
data: { userId: 123, action: 'update' },
606+
}),
607+
);
608+
expect(Sentry.addBreadcrumb).toHaveBeenNthCalledWith(
609+
2,
610+
expect.objectContaining({
611+
data: { role: 'admin' },
612+
}),
613+
);
614+
});
569615
});
570616
});

src/core/Try.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,11 @@ export class Try<
8484
result?: TryResult<TReturn>;
8585
promise?: Promise<TryResult<TReturn>>;
8686
isAsync?: boolean;
87+
finallyRan: boolean;
88+
};
89+
private local: {
8790
breadcrumbData?: Record<string, unknown>;
8891
breadcrumbsAdded: boolean;
89-
finallyRan: boolean;
9092
};
9193
private static ignoreErrorTypes: string[] = [];
9294
private static defaultReporter: Reporter = new NoopReporter();
@@ -144,7 +146,8 @@ export class Try<
144146
this.fn = fn;
145147
this.args = args;
146148
this.config = { tags: {} };
147-
this.exec = { state: 'pending', breadcrumbsAdded: false, finallyRan: false };
149+
this.exec = { state: 'pending', finallyRan: false };
150+
this.local = { breadcrumbsAdded: false };
148151
// Install a thenable `.then` at runtime whenever the wrapped function may
149152
// produce a Promise. `AsyncFunction` is the fast path; non-async functions
150153
// that still return a Promise are detected lazily via a getter that
@@ -833,7 +836,7 @@ export class Try<
833836
Try.defaultReporter.report(error, {
834837
message: this.config.message,
835838
tags: this.config.tags,
836-
breadcrumbData: this.exec.breadcrumbData,
839+
breadcrumbData: this.local.breadcrumbData,
837840
functionName: this.fn.name,
838841
});
839842
}
@@ -842,18 +845,18 @@ export class Try<
842845
* Add breadcrumbs using the configured reporter if configured.
843846
*/
844847
private addBreadcrumbsIfConfigured(): void {
845-
if (!this.config.breadcrumbConfig || this.exec.breadcrumbsAdded) {
848+
if (!this.config.breadcrumbConfig || this.local.breadcrumbsAdded) {
846849
return;
847850
}
848851

849-
if (!this.exec.breadcrumbData) {
850-
this.exec.breadcrumbData = this.extractAllBreadcrumbData();
852+
if (!this.local.breadcrumbData) {
853+
this.local.breadcrumbData = this.extractAllBreadcrumbData();
851854
}
852855

853856
const functionName = this.fn.name || 'anonymous';
854857

855-
Try.defaultReporter.addBreadcrumbs(this.exec.breadcrumbData, functionName);
856-
this.exec.breadcrumbsAdded = true;
858+
Try.defaultReporter.addBreadcrumbs(this.local.breadcrumbData, functionName);
859+
this.local.breadcrumbsAdded = true;
857860
}
858861

859862
/**

0 commit comments

Comments
 (0)