@@ -14,30 +14,11 @@ import type {
1414} from '@hawk.so/types' ;
1515import EventPayload from './modules/event.js' ;
1616import { BreadcrumbManager , type BreadcrumbInput , type BreadcrumbHint } from './modules/breadcrumbs.js' ;
17+ import { isValidEventPayload } from './modules/validate-event.js' ;
1718import type { AxiosResponse } from 'axios' ;
1819import axios from 'axios' ;
1920import { VERSION } from './version.js' ;
2021
21- /**
22- * Checks if value is a plain object (not array, Date, etc.)
23- */
24- function isPlainObject ( v : unknown ) : v is Record < string , unknown > {
25- return Object . prototype . toString . call ( v ) === '[object Object]' ;
26- }
27-
28- /**
29- * Minimal required fields check:
30- * - payload must be a plain object
31- * - payload.title must be a non-empty string
32- */
33- function hasRequiredEventFields ( v : unknown ) : v is { title : string } {
34- if ( ! isPlainObject ( v ) ) {
35- return false ;
36- }
37-
38- return typeof v . title === 'string' && v . title . trim ( ) !== '' ;
39- }
40-
4122/**
4223 * Class for throwing errors inside unhandledRejection processor
4324 */
@@ -290,31 +271,20 @@ class Catcher {
290271 }
291272
292273 /**
293- * If user returned nothing — keep original payload
274+ * If user returned a value — use it; if undefined (no return / in-place mutation) — keep payload reference
294275 */
295- if ( result !== undefined ) {
296- /**
297- * Accept only payloads that still have required fields (minimal check)
298- */
299- if ( hasRequiredEventFields ( result ) ) {
300- payload = result as EventData < NodeJSAddons > ;
301- } else {
302- console . warn (
303- `[Hawk] beforeSend returned invalid payload. ` +
304- `Keeping original payload. Received: ${ Object . prototype . toString . call ( result ) } `
305- ) ;
306- }
307- }
276+ const candidate = result ?? payload ;
308277
309- /**
310- * Final safety check:
311- * protects from in-place mutation of `payload` when beforeSend returns undefined
312- */
313- if ( ! hasRequiredEventFields ( payload ) ) {
314- console . warn ( '[Hawk] payload corrupted after beforeSend, event dropped' ) ;
278+ if ( ! isValidEventPayload ( candidate ) ) {
279+ console . warn (
280+ '[Hawk] beforeSend produced invalid payload (missing required fields), event dropped. '
281+ + `Received: ${ Object . prototype . toString . call ( candidate ) } `
282+ ) ;
315283
316284 return ;
317285 }
286+
287+ payload = candidate ;
318288 }
319289
320290 void this . sendErrorFormatted ( {
0 commit comments