Skip to content

Commit eba517d

Browse files
committed
refactor(javascript): MessageProcessor abstraction added and implemented in catcher event processing pipeline
- MessageProcessor interface and MessageHint type - BrowserMessageProcessor, BreadcrumbsMessageProcessor, ConsoleCatcherMessageProcessor, DebugMessageProcessor - replaced inline addon logic with sequential MessageProcessor pipeline
1 parent afc73fa commit eba517d

14 files changed

+465
-193
lines changed

packages/core/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ export { buildElementSelector } from './utils/selector';
1313
export { EventRejectedError } from './errors';
1414
export { isErrorProcessed, markErrorAsProcessed } from './utils/event';
1515
export type { BreadcrumbStore, BreadcrumbsAPI, BreadcrumbHint, BreadcrumbInput } from './breadcrumbs/breadcrumb-store';
16+
export type { MessageHint, MessageProcessor, ProcessingPayload } from './messages/message-processor';
17+
export { BreadcrumbsMessageProcessor } from './messages/breadcrumbs-message-processor';;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import type { MessageHint, MessageProcessor, ProcessingPayload } from './message-processor';
2+
3+
/**
4+
* Attaches breadcrumbs snapshot from {@link hint} to payload.
5+
*/
6+
export class BreadcrumbsMessageProcessor implements MessageProcessor<'errors/javascript'> {
7+
/**
8+
* Sets `payload.breadcrumbs` from hint snapshot if non-empty; skips otherwise.
9+
*
10+
* @param payload - event message payload to enrich
11+
* @param hint - hint carrying breadcrumbs snapshot captured at error time
12+
* @returns modified payload with breadcrumbs set, or original payload unchanged
13+
*/
14+
public apply(
15+
payload: ProcessingPayload<'errors/javascript'>,
16+
hint?: MessageHint
17+
): ProcessingPayload<'errors/javascript'> | null {
18+
if (hint?.breadcrumbs && hint.breadcrumbs.length > 0) {
19+
payload.breadcrumbs = hint.breadcrumbs;
20+
}
21+
22+
return payload;
23+
}
24+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import type { Breadcrumb, CatcherMessagePayload, CatcherMessageType } from '@hawk.so/types';
2+
3+
/**
4+
* Extracted addons type from catcher message payload.
5+
*
6+
* @typeParam T - catcher message type
7+
*/
8+
type ExtractAddons<T extends CatcherMessageType> =
9+
CatcherMessagePayload<T> extends { addons?: infer A } ? A : never;
10+
11+
/**
12+
* Payload type used during message processing pipeline.
13+
*
14+
* Same as {@link CatcherMessagePayload} but with `addons` always defined and partially filled —
15+
* processors may contribute individual addon fields independently of each other.
16+
*
17+
* @typeParam T - catcher message type this payload belongs to
18+
*/
19+
export type ProcessingPayload<T extends CatcherMessageType> =
20+
Omit<CatcherMessagePayload<T>, 'addons'> & {
21+
addons: Partial<ExtractAddons<T>>;
22+
};
23+
24+
/**
25+
* Snapshot of event context captured synchronously at error time,
26+
* before any processing.
27+
*/
28+
export interface MessageHint {
29+
/**
30+
* Original caught error.
31+
*/
32+
error?: Error | string;
33+
34+
/**
35+
* Breadcrumbs captured at error time.
36+
*/
37+
breadcrumbs?: Breadcrumb[];
38+
}
39+
40+
/**
41+
* Single step in message processing pipeline before message is sent.
42+
*
43+
* @typeParam T - catcher message type this processor handles
44+
*/
45+
export interface MessageProcessor<T extends CatcherMessageType = CatcherMessageType> {
46+
/**
47+
* Handles input message. May mutate or replace it.
48+
*
49+
* @param payload - processed event message payload with partially-built addons
50+
* @param hint - additional context about original error
51+
* @returns modified payload, or `null` to drop event
52+
*/
53+
apply(
54+
payload: ProcessingPayload<T>,
55+
hint?: MessageHint,
56+
): ProcessingPayload<T> | null
57+
}

packages/core/src/modules/sanitizer.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,6 @@ export class Sanitizer {
154154
depth: number,
155155
seen: WeakSet<object>
156156
): Record<string, any> | '<deep object>' | '<big object>' {
157-
158157
// If the maximum depth is reached, return a placeholder
159158
if (depth > Sanitizer.maxDepth) {
160159
return '<deep object>';

packages/javascript/src/addons/userAgentInfo.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)