Skip to content

Commit 4279d77

Browse files
committed
refactor: improve handling of beforeSend and beforeBreadcrumb hooks by using clones for payload modifications
1 parent e02ffd5 commit 4279d77

2 files changed

Lines changed: 16 additions & 18 deletions

File tree

packages/javascript/src/addons/breadcrumbs.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -236,30 +236,29 @@ export class BreadcrumbManager {
236236
* Apply beforeBreadcrumb hook
237237
*/
238238
if (this.options.beforeBreadcrumb) {
239-
const original = structuredClone(bc);
240-
const result = this.options.beforeBreadcrumb(bc, hint);
239+
const breadcrumbClone = structuredClone(bc);
240+
const modified = this.options.beforeBreadcrumb(breadcrumbClone, hint);
241241

242242
/**
243243
* false means discard
244244
*/
245-
if (result === false) {
245+
if (modified === false) {
246246
return;
247247
}
248248

249249
/**
250-
* Valid breadcrumb → use it
250+
* Valid breadcrumb → apply changes from hook
251251
*/
252-
if (isValidBreadcrumb(result)) {
253-
Object.assign(bc, result);
252+
if (isValidBreadcrumb(modified)) {
253+
Object.assign(bc, modified);
254254
} else {
255255
/**
256-
* Anything else is invalid — warn and restore original
256+
* Anything else is invalid — warn, bc stays untouched (hook only received a clone)
257257
*/
258258
log(
259-
`Invalid beforeBreadcrumb value. It should return breadcrumb or false. Breadcrumb is stored without changes.`,
259+
'Invalid beforeBreadcrumb value. It should return breadcrumb or false. Breadcrumb is stored without changes.',
260260
'warn'
261261
);
262-
Object.assign(bc, original);
263262
}
264263
}
265264

packages/javascript/src/catcher.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -438,30 +438,29 @@ export default class Catcher {
438438
* Filter sensitive data
439439
*/
440440
if (typeof this.beforeSend === 'function') {
441-
const original = structuredClone(payload);
442-
const result = this.beforeSend(payload);
441+
const eventClone = structuredClone(payload);
442+
const modified = this.beforeSend(eventClone);
443443

444444
/**
445445
* false → drop event
446446
*/
447-
if (result === false) {
447+
if (modified === false) {
448448
throw new EventRejectedError('Event rejected by beforeSend method.');
449449
}
450450

451451
/**
452-
* Valid event payload → use it
452+
* Valid event payload → use it instead of original
453453
*/
454-
if (isValidEventPayload(result)) {
455-
payload = result as HawkJavaScriptEvent;
454+
if (isValidEventPayload(modified)) {
455+
payload = modified as HawkJavaScriptEvent;
456456
} else {
457457
/**
458-
* Anything else is invalid — warn and send original
458+
* Anything else is invalid — warn, payload stays untouched (hook only received a clone)
459459
*/
460460
log(
461-
`Invalid beforeSend value. It should return event or false. Event is sent without changes.`,
461+
'Invalid beforeSend value. It should return event or false. Event is sent without changes.',
462462
'warn'
463463
);
464-
payload = original;
465464
}
466465
}
467466

0 commit comments

Comments
 (0)