Skip to content

Commit 484f0bc

Browse files
committed
fix
1 parent 0c9a73f commit 484f0bc

File tree

1 file changed

+34
-9
lines changed

1 file changed

+34
-9
lines changed

src/addons/consoleCatcher.ts

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,35 @@
44

55
import type { ConsoleLogEvent } from '@hawk.so/types';
66

7-
const createConsoleCatcher = () => {
7+
/**
8+
* Factory function to create a console log catcher
9+
* @returns Object with methods for initializing, retrieving, and adding console logs
10+
*/
11+
const createConsoleCatcher = (): {
12+
initConsoleCatcher: () => void;
13+
addErrorEvent: (event: ErrorEvent | PromiseRejectionEvent) => void;
14+
getConsoleLogStack: () => ConsoleLogEvent[];
15+
} => {
816
const MAX_LOGS = 20;
917
const consoleOutput: ConsoleLogEvent[] = [];
1018
let isInitialized = false;
1119

12-
const addToConsoleOutput = (logEvent: ConsoleLogEvent) => {
20+
/**
21+
* Adds a log event to the console output buffer
22+
* @param logEvent - The log event to store
23+
*/
24+
const addToConsoleOutput = (logEvent: ConsoleLogEvent): void => {
1325
if (consoleOutput.length >= MAX_LOGS) {
1426
consoleOutput.shift();
1527
}
1628
consoleOutput.push(logEvent);
1729
};
1830

31+
/**
32+
* Creates a ConsoleLogEvent from an ErrorEvent or PromiseRejectionEvent
33+
* @param event - The event to process
34+
* @returns The formatted log event
35+
*/
1936
const createConsoleEventFromError = (
2037
event: ErrorEvent | PromiseRejectionEvent
2138
): ConsoleLogEvent => {
@@ -43,13 +60,16 @@ const createConsoleCatcher = () => {
4360
};
4461

4562
return {
63+
/**
64+
* Initializes the console catcher by overriding console methods
65+
*/
4666
initConsoleCatcher(): void {
4767
if (isInitialized) {
4868
return;
4969
}
5070

5171
isInitialized = true;
52-
const consoleMethods = ['log', 'warn', 'error', 'info', 'debug'];
72+
const consoleMethods: string[] = ['log', 'warn', 'error', 'info', 'debug'];
5373

5474
consoleMethods.forEach((method) => {
5575
if (typeof window.console[method] !== 'function') {
@@ -58,7 +78,7 @@ const createConsoleCatcher = () => {
5878

5979
const oldFunction = window.console[method].bind(window.console);
6080

61-
window.console[method] = function (...args): void {
81+
window.console[method] = function (...args: unknown[]): void {
6282
const stack =
6383
new Error().stack?.split('\n').slice(2).join('\n') || '';
6484

@@ -67,10 +87,7 @@ const createConsoleCatcher = () => {
6787
timestamp: new Date(),
6888
type: method,
6989
message: args
70-
.map((arg) =>
71-
typeof arg === 'string' ? arg : JSON.stringify(arg)
72-
)
73-
.join(' '),
90+
.map((arg) => typeof arg === 'string' ? arg : JSON.stringify(arg)).join(' '),
7491
stack,
7592
fileLine: stack.split('\n')[0]?.trim(),
7693
};
@@ -81,13 +98,21 @@ const createConsoleCatcher = () => {
8198
});
8299
},
83100

101+
/**
102+
* Adds an error event (either an ErrorEvent or a PromiseRejectionEvent) to the log stack
103+
* @param event - The error event to log
104+
*/
84105
addErrorEvent(event: ErrorEvent | PromiseRejectionEvent): void {
85106
const logEvent = createConsoleEventFromError(event);
86107
addToConsoleOutput(logEvent);
87108
},
88109

110+
/**
111+
* Retrieves the current console log stack
112+
* @returns A copy of the console log stack
113+
*/
89114
getConsoleLogStack(): ConsoleLogEvent[] {
90-
return [...consoleOutput];
115+
return [ ...consoleOutput ];
91116
},
92117
};
93118
};

0 commit comments

Comments
 (0)