Skip to content

Commit c1a81f2

Browse files
committed
fix
1 parent e79e9c3 commit c1a81f2

5 files changed

Lines changed: 20 additions & 34 deletions

File tree

packages/javascript/src/catcher.ts

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export default class Catcher {
7676
* This Method allows developer to filter any data you don't want sending to Hawk.
7777
* - Return modified event — it will be sent instead of the original.
7878
* - Return `false` — the event will be dropped entirely.
79-
* - Return nothing (`void` / `undefined` / `null`) — the original event is sent as-is (a warning is logged).
79+
* - Any other value is invalid — the original event is sent as-is (a warning is logged).
8080
*/
8181
private readonly beforeSend: undefined | ((event: HawkJavaScriptEvent) => HawkJavaScriptEvent | false | void);
8282

@@ -441,35 +441,23 @@ export default class Catcher {
441441
const result = this.beforeSend(payload);
442442

443443
/**
444-
* Allow user to intentionally drop event by returning false
444+
* false → drop event
445445
*/
446446
if (result === false) {
447447
throw new EventRejectedError('Event rejected by beforeSend method.');
448448
}
449449

450450
/**
451-
* If user returned nothing (void/undefined/null) — warn and keep original payload
451+
* Valid event payload → use it
452452
*/
453-
if (result === undefined || result === null) {
454-
log(`Invalid beforeSend value: (${String(result)}). It should return event or false. Event is sent without changes.`, 'warn');
455-
} else if (isValidEventPayload(result)) {
456-
payload = result;
453+
if (isValidEventPayload(result)) {
454+
payload = result as HawkJavaScriptEvent;
457455
} else {
458-
let received: string;
459-
460-
try {
461-
received = JSON.stringify(result);
462-
} catch {
463-
try {
464-
received = String(result);
465-
} catch {
466-
received = Object.prototype.toString.call(result);
467-
}
468-
}
469-
456+
/**
457+
* Anything else is invalid — warn and send original
458+
*/
470459
log(
471-
'beforeSend produced invalid payload (missing required fields), sending original. '
472-
+ `Received: ${received}`,
460+
`Invalid beforeSend value: (${String(result)}). It should return event or false. Event is sent without changes.`,
473461
'warn'
474462
);
475463
}

packages/javascript/src/types/hawk-initial-settings.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export interface HawkInitialSettings {
6767
* This Method allows you to filter any data you don't want sending to Hawk.
6868
* - Return modified event — it will be sent instead of the original.
6969
* - Return `false` — the event will be dropped entirely.
70-
* - Return nothing (`void` / `undefined` / `null`) — the original event is sent as-is (a warning is logged).
70+
* - Any other value is invalid — the original event is sent as-is (a warning is logged).
7171
*/
7272
beforeSend?(event: HawkJavaScriptEvent): HawkJavaScriptEvent | false | void;
7373

packages/javascript/src/utils/validation.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,11 @@ export function isValidEventPayload(payload: unknown): payload is EventData<Java
7575
* @param breadcrumb - value to validate
7676
*/
7777
export function isValidBreadcrumb(breadcrumb: unknown): breadcrumb is Breadcrumb {
78-
if (typeof breadcrumb !== 'object' || breadcrumb === null || Array.isArray(breadcrumb)) {
78+
if (!isPlainObject(breadcrumb)) {
7979
return false;
8080
}
8181

82-
const record = breadcrumb as Record<string, unknown>;
83-
84-
if (record.timestamp !== undefined && typeof record.timestamp !== 'number') {
82+
if (breadcrumb.timestamp !== undefined && typeof breadcrumb.timestamp !== 'number') {
8583
return false;
8684
}
8785

packages/javascript/tests/before-send.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ describe('beforeSend', () => {
132132

133133
expect(socketSendSpy).toHaveBeenCalledOnce();
134134
expect(warnSpy).toHaveBeenCalledWith(
135-
expect.stringContaining('[Hawk] Invalid beforeSend value: (undefined)'),
135+
expect.stringContaining('Invalid beforeSend value: (undefined)'),
136136
expect.anything(),
137137
expect.anything()
138138
);
@@ -145,7 +145,7 @@ describe('beforeSend', () => {
145145

146146
expect(socketSendSpy).toHaveBeenCalledOnce();
147147
expect(warnSpy).toHaveBeenCalledWith(
148-
expect.stringContaining('[Hawk] Invalid beforeSend value: (null)'),
148+
expect.stringContaining('Invalid beforeSend value: (null)'),
149149
expect.anything(),
150150
expect.anything()
151151
);
@@ -158,7 +158,7 @@ describe('beforeSend', () => {
158158

159159
expect(socketSendSpy).toHaveBeenCalledOnce();
160160
expect(warnSpy).toHaveBeenCalledWith(
161-
expect.stringContaining('[Hawk] beforeSend produced invalid payload'),
161+
expect.stringContaining('Invalid beforeSend value:'),
162162
expect.anything(),
163163
expect.anything()
164164
);
@@ -171,7 +171,7 @@ describe('beforeSend', () => {
171171

172172
expect(socketSendSpy).toHaveBeenCalledOnce();
173173
expect(warnSpy).toHaveBeenCalledWith(
174-
expect.stringContaining('[Hawk] beforeSend produced invalid payload'),
174+
expect.stringContaining('Invalid beforeSend value:'),
175175
expect.anything(),
176176
expect.anything()
177177
);

packages/javascript/tests/breadcrumbs.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ describe('beforeBreadcrumb', () => {
199199

200200
expect(manager.getBreadcrumbs()[0].message).toBe('void');
201201
expect(warnSpy).toHaveBeenCalledWith(
202-
expect.stringContaining('[Hawk] beforeBreadcrumb returned nothing'),
202+
expect.stringContaining('beforeBreadcrumb returned nothing'),
203203
expect.anything(),
204204
expect.anything()
205205
);
@@ -210,7 +210,7 @@ describe('beforeBreadcrumb', () => {
210210

211211
expect(manager.getBreadcrumbs()[0].message).toBe('null');
212212
expect(warnSpy).toHaveBeenCalledWith(
213-
expect.stringContaining('[Hawk] beforeBreadcrumb returned nothing'),
213+
expect.stringContaining('beforeBreadcrumb returned nothing'),
214214
expect.anything(),
215215
expect.anything()
216216
);
@@ -221,7 +221,7 @@ describe('beforeBreadcrumb', () => {
221221

222222
expect(manager.getBreadcrumbs()[0].message).toBe('invalid');
223223
expect(warnSpy).toHaveBeenCalledWith(
224-
expect.stringContaining('[Hawk] beforeBreadcrumb produced invalid breadcrumb'),
224+
expect.stringContaining('beforeBreadcrumb produced invalid breadcrumb'),
225225
expect.anything(),
226226
expect.anything()
227227
);
@@ -235,7 +235,7 @@ describe('beforeBreadcrumb', () => {
235235
expect(bc.message).toBe('bad-ts');
236236
expect(bc.timestamp).toBeTypeOf('number');
237237
expect(warnSpy).toHaveBeenCalledWith(
238-
expect.stringContaining('[Hawk] beforeBreadcrumb produced invalid breadcrumb'),
238+
expect.stringContaining('beforeBreadcrumb produced invalid breadcrumb'),
239239
expect.anything(),
240240
expect.anything()
241241
);

0 commit comments

Comments
 (0)