From 2a843cd7d52fa66b921c64dc44b0ca4f7c84fc17 Mon Sep 17 00:00:00 2001 From: Dobrunia Kostrigin <48620984+Dobrunia@users.noreply.github.com> Date: Sat, 15 Mar 2025 13:59:32 +0300 Subject: [PATCH 1/9] consoleCatcher init --- src/addons/consoleCatcher.ts | 92 +++++++++++++++++++++--------------- src/catcher.ts | 8 ++++ 2 files changed, 62 insertions(+), 38 deletions(-) diff --git a/src/addons/consoleCatcher.ts b/src/addons/consoleCatcher.ts index 77de467..415665f 100644 --- a/src/addons/consoleCatcher.ts +++ b/src/addons/consoleCatcher.ts @@ -1,48 +1,64 @@ /** - * @file Integration for catching console logs and other info + * @file Module for intercepting console logs with stack trace capture */ -interface ConsoleLogEvent { - /** - * Window.console object method (i.e. log, info, warn) - */ - method: string; - - /** - * Time when the log was occurred - */ - timestamp: Date; - - /** - * Log argument - */ - // eslint-disable-next-line @typescript-eslint/no-explicit-any - args: any; -} +import { ConsoleLogEvent } from "@hawk.so/types/build/src/base/event/addons/javascript"; -/** - * Contains all data that will be logged by window.console - */ +const MAX_LOGS = 20; const consoleOutput: ConsoleLogEvent[] = []; -// Override console methods -Object.keys(window.console).forEach(key => { - const oldFunction = window.console[key]; +let isInitialized = false; + +export function initConsoleCatcher(): void { + if (isInitialized) return; + isInitialized = true; + + const consoleMethods = ["log", "warn", "error", "info", "debug"]; + + consoleMethods.forEach((method) => { + if (typeof window.console[method] !== "function") return; + + const oldFunction = window.console[method].bind(window.console); + + window.console[method] = function (...args): void { + if (consoleOutput.length >= MAX_LOGS) { + consoleOutput.shift(); + } - window.console[key] = function (...args): void { - consoleOutput.push({ - method: key, + const stack = new Error().stack?.split("\n").slice(2).join("\n") || ""; + + const logEvent: ConsoleLogEvent = { + method, + timestamp: new Date(), + type: method, + message: args.map(arg => typeof arg === 'string' ? arg : JSON.stringify(arg)).join(' '), + stack, + fileLine: stack.split('\n')[0]?.trim(), + }; + + consoleOutput.push(logEvent); + oldFunction(...args); + }; + }); + + window.addEventListener("error", function(event) { + if (consoleOutput.length >= MAX_LOGS) { + consoleOutput.shift(); + } + + const logEvent: ConsoleLogEvent = { + method: 'error', timestamp: new Date(), - args, - }); - oldFunction.apply(window.console, args); - }; -}); + type: event.error?.name || 'Error', + message: event.error?.message || event.message, + stack: event.error?.stack || '', + fileLine: event.filename ? `${event.filename}:${event.lineno}:${event.colno}` : '', + }; -/** - * @param event - event to modify - * @param data - event data - */ -export default function (event, data): void { - data.payload.consoleOutput = consoleOutput; + consoleOutput.push(logEvent); + }); +} + +export function getConsoleLogStack(): ConsoleLogEvent[] { + return [...consoleOutput]; } diff --git a/src/catcher.ts b/src/catcher.ts index fb2207c..6cf92dc 100644 --- a/src/catcher.ts +++ b/src/catcher.ts @@ -16,6 +16,7 @@ import type { JavaScriptCatcherIntegrations } from './types/integrations'; import { EventRejectedError } from './errors'; import type { HawkJavaScriptEvent } from './types'; import { isErrorProcessed, markErrorAsProcessed } from './utils/event'; +import { getConsoleLogStack, initConsoleCatcher } from './addons/consoleCatcher'; /** * Allow to use global VERSION, that will be overwritten by Webpack @@ -130,6 +131,8 @@ export default class Catcher { }, }); + initConsoleCatcher(); + /** * Set global handlers */ @@ -489,6 +492,7 @@ export default class Catcher { const userAgent = window.navigator.userAgent; const location = window.location.href; const getParams = this.getGetParams(); + const consoleLogs = getConsoleLogStack(); const addons: JavaScriptAddons = { window: { @@ -507,6 +511,10 @@ export default class Catcher { addons.RAW_EVENT_DATA = this.getRawData(error); } + if (consoleLogs.length > 0) { + addons.consoleOutput = consoleLogs; + } + return addons; } From 36dd0613e178a162f5806d1ad696c435b718673d Mon Sep 17 00:00:00 2001 From: Dobrunia Kostrigin <48620984+Dobrunia@users.noreply.github.com> Date: Sat, 15 Mar 2025 14:14:16 +0300 Subject: [PATCH 2/9] fix --- src/addons/consoleCatcher.ts | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/addons/consoleCatcher.ts b/src/addons/consoleCatcher.ts index 415665f..1141583 100644 --- a/src/addons/consoleCatcher.ts +++ b/src/addons/consoleCatcher.ts @@ -31,9 +31,11 @@ export function initConsoleCatcher(): void { method, timestamp: new Date(), type: method, - message: args.map(arg => typeof arg === 'string' ? arg : JSON.stringify(arg)).join(' '), + message: args + .map((arg) => (typeof arg === "string" ? arg : JSON.stringify(arg))) + .join(" "), stack, - fileLine: stack.split('\n')[0]?.trim(), + fileLine: stack.split("\n")[0]?.trim(), }; consoleOutput.push(logEvent); @@ -41,18 +43,20 @@ export function initConsoleCatcher(): void { }; }); - window.addEventListener("error", function(event) { + window.addEventListener("error", function (event) { if (consoleOutput.length >= MAX_LOGS) { consoleOutput.shift(); } const logEvent: ConsoleLogEvent = { - method: 'error', + method: "error", timestamp: new Date(), - type: event.error?.name || 'Error', + type: event.error?.name || "Error", message: event.error?.message || event.message, - stack: event.error?.stack || '', - fileLine: event.filename ? `${event.filename}:${event.lineno}:${event.colno}` : '', + stack: event.error?.stack || "", + fileLine: event.filename + ? `${event.filename}:${event.lineno}:${event.colno}` + : "", }; consoleOutput.push(logEvent); From 04d76e78cdfe7fc2539f64e3d79c17c134030177 Mon Sep 17 00:00:00 2001 From: Dobrunia Kostrigin <48620984+Dobrunia@users.noreply.github.com> Date: Sat, 15 Mar 2025 14:46:56 +0300 Subject: [PATCH 3/9] fix --- src/addons/consoleCatcher.ts | 24 ++++++++++++------------ src/catcher.ts | 2 +- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/addons/consoleCatcher.ts b/src/addons/consoleCatcher.ts index 1141583..64337de 100644 --- a/src/addons/consoleCatcher.ts +++ b/src/addons/consoleCatcher.ts @@ -2,7 +2,7 @@ * @file Module for intercepting console logs with stack trace capture */ -import { ConsoleLogEvent } from "@hawk.so/types/build/src/base/event/addons/javascript"; +import type { ConsoleLogEvent } from '@hawk.so/types/build/src/base/event/addons/javascript'; const MAX_LOGS = 20; const consoleOutput: ConsoleLogEvent[] = []; @@ -10,13 +10,13 @@ const consoleOutput: ConsoleLogEvent[] = []; let isInitialized = false; export function initConsoleCatcher(): void { - if (isInitialized) return; + if (isInitialized) { return }; isInitialized = true; - const consoleMethods = ["log", "warn", "error", "info", "debug"]; + const consoleMethods = ['log', 'warn', 'error', 'info', 'debug']; consoleMethods.forEach((method) => { - if (typeof window.console[method] !== "function") return; + if (typeof window.console[method] !== 'function') { return }; const oldFunction = window.console[method].bind(window.console); @@ -25,17 +25,17 @@ export function initConsoleCatcher(): void { consoleOutput.shift(); } - const stack = new Error().stack?.split("\n").slice(2).join("\n") || ""; + const stack = new Error().stack?.split('\n').slice(2).join('\n') || ''; const logEvent: ConsoleLogEvent = { method, timestamp: new Date(), type: method, message: args - .map((arg) => (typeof arg === "string" ? arg : JSON.stringify(arg))) + .map((arg) => (typeof arg === 'string' ? arg : JSON.stringify(arg))) .join(" "), stack, - fileLine: stack.split("\n")[0]?.trim(), + fileLine: stack.split('\n')[0]?.trim(), }; consoleOutput.push(logEvent); @@ -43,20 +43,20 @@ export function initConsoleCatcher(): void { }; }); - window.addEventListener("error", function (event) { + window.addEventListener('error', function (event) { if (consoleOutput.length >= MAX_LOGS) { consoleOutput.shift(); } const logEvent: ConsoleLogEvent = { - method: "error", + method: 'error', timestamp: new Date(), - type: event.error?.name || "Error", + type: event.error?.name || 'Error', message: event.error?.message || event.message, - stack: event.error?.stack || "", + stack: event.error?.stack || '', fileLine: event.filename ? `${event.filename}:${event.lineno}:${event.colno}` - : "", + : '', }; consoleOutput.push(logEvent); diff --git a/src/catcher.ts b/src/catcher.ts index 6cf92dc..b4e5298 100644 --- a/src/catcher.ts +++ b/src/catcher.ts @@ -512,7 +512,7 @@ export default class Catcher { } if (consoleLogs.length > 0) { - addons.consoleOutput = consoleLogs; + addons.consoleOutput = consoleLogs; } return addons; From f1e6031eb71cf1a42cd0faaa1b18554d2ad2b3a5 Mon Sep 17 00:00:00 2001 From: Dobrunia Kostrigin <48620984+Dobrunia@users.noreply.github.com> Date: Sat, 15 Mar 2025 14:56:25 +0300 Subject: [PATCH 4/9] fix --- src/addons/consoleCatcher.ts | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/addons/consoleCatcher.ts b/src/addons/consoleCatcher.ts index 64337de..3ba3efa 100644 --- a/src/addons/consoleCatcher.ts +++ b/src/addons/consoleCatcher.ts @@ -9,14 +9,22 @@ const consoleOutput: ConsoleLogEvent[] = []; let isInitialized = false; +/** + * Initializes the console catcher by overriding console methods + * to capture logs with stack traces. + */ export function initConsoleCatcher(): void { - if (isInitialized) { return }; - isInitialized = true; + if (isInitialized) { + return + }; + isInitialized = true; const consoleMethods = ['log', 'warn', 'error', 'info', 'debug']; consoleMethods.forEach((method) => { - if (typeof window.console[method] !== 'function') { return }; + if (typeof window.console[method] !== 'function') { + return + }; const oldFunction = window.console[method].bind(window.console); @@ -33,7 +41,7 @@ export function initConsoleCatcher(): void { type: method, message: args .map((arg) => (typeof arg === 'string' ? arg : JSON.stringify(arg))) - .join(" "), + .join(' '), stack, fileLine: stack.split('\n')[0]?.trim(), }; @@ -63,6 +71,11 @@ export function initConsoleCatcher(): void { }); } +/** + * Returns the stack of captured console logs. + * + * @returns {ConsoleLogEvent[]} Array of logged console events. + */ export function getConsoleLogStack(): ConsoleLogEvent[] { - return [...consoleOutput]; + return [ ...consoleOutput ]; } From ca73ec9d17bec6d6930a52335ebe6b1564971612 Mon Sep 17 00:00:00 2001 From: Dobrunia Kostrigin <48620984+Dobrunia@users.noreply.github.com> Date: Sat, 15 Mar 2025 14:58:41 +0300 Subject: [PATCH 5/9] fix --- src/addons/consoleCatcher.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/addons/consoleCatcher.ts b/src/addons/consoleCatcher.ts index 3ba3efa..9129e36 100644 --- a/src/addons/consoleCatcher.ts +++ b/src/addons/consoleCatcher.ts @@ -15,7 +15,7 @@ let isInitialized = false; */ export function initConsoleCatcher(): void { if (isInitialized) { - return + return; }; isInitialized = true; @@ -23,7 +23,7 @@ export function initConsoleCatcher(): void { consoleMethods.forEach((method) => { if (typeof window.console[method] !== 'function') { - return + return; }; const oldFunction = window.console[method].bind(window.console); @@ -40,8 +40,7 @@ export function initConsoleCatcher(): void { timestamp: new Date(), type: method, message: args - .map((arg) => (typeof arg === 'string' ? arg : JSON.stringify(arg))) - .join(' '), + .map((arg) => (typeof arg === 'string' ? arg : JSON.stringify(arg))).join(' '), stack, fileLine: stack.split('\n')[0]?.trim(), }; From 0c9a73fca07ada64087711d6a086f03bc61e1511 Mon Sep 17 00:00:00 2001 From: Dobrunia Kostrigin <48620984+Dobrunia@users.noreply.github.com> Date: Sat, 15 Mar 2025 19:35:58 +0300 Subject: [PATCH 6/9] fix --- src/addons/consoleCatcher.ts | 139 ++++++++++++++++++++--------------- src/catcher.ts | 8 +- 2 files changed, 85 insertions(+), 62 deletions(-) diff --git a/src/addons/consoleCatcher.ts b/src/addons/consoleCatcher.ts index 9129e36..b67a8fe 100644 --- a/src/addons/consoleCatcher.ts +++ b/src/addons/consoleCatcher.ts @@ -2,79 +2,96 @@ * @file Module for intercepting console logs with stack trace capture */ -import type { ConsoleLogEvent } from '@hawk.so/types/build/src/base/event/addons/javascript'; +import type { ConsoleLogEvent } from '@hawk.so/types'; -const MAX_LOGS = 20; -const consoleOutput: ConsoleLogEvent[] = []; +const createConsoleCatcher = () => { + const MAX_LOGS = 20; + const consoleOutput: ConsoleLogEvent[] = []; + let isInitialized = false; -let isInitialized = false; - -/** - * Initializes the console catcher by overriding console methods - * to capture logs with stack traces. - */ -export function initConsoleCatcher(): void { - if (isInitialized) { - return; + const addToConsoleOutput = (logEvent: ConsoleLogEvent) => { + if (consoleOutput.length >= MAX_LOGS) { + consoleOutput.shift(); + } + consoleOutput.push(logEvent); }; - isInitialized = true; - const consoleMethods = ['log', 'warn', 'error', 'info', 'debug']; + const createConsoleEventFromError = ( + event: ErrorEvent | PromiseRejectionEvent + ): ConsoleLogEvent => { + if (event instanceof ErrorEvent) { + return { + method: 'error', + timestamp: new Date(), + type: event.error?.name || 'Error', + message: event.error?.message || event.message, + stack: event.error?.stack || '', + fileLine: event.filename + ? `${event.filename}:${event.lineno}:${event.colno}` + : '', + }; + } - consoleMethods.forEach((method) => { - if (typeof window.console[method] !== 'function') { - return; + return { + method: 'error', + timestamp: new Date(), + type: 'UnhandledRejection', + message: event.reason?.message || String(event.reason), + stack: event.reason?.stack || '', + fileLine: '', }; + }; - const oldFunction = window.console[method].bind(window.console); - - window.console[method] = function (...args): void { - if (consoleOutput.length >= MAX_LOGS) { - consoleOutput.shift(); + return { + initConsoleCatcher(): void { + if (isInitialized) { + return; } - const stack = new Error().stack?.split('\n').slice(2).join('\n') || ''; + isInitialized = true; + const consoleMethods = ['log', 'warn', 'error', 'info', 'debug']; - const logEvent: ConsoleLogEvent = { - method, - timestamp: new Date(), - type: method, - message: args - .map((arg) => (typeof arg === 'string' ? arg : JSON.stringify(arg))).join(' '), - stack, - fileLine: stack.split('\n')[0]?.trim(), - }; + consoleMethods.forEach((method) => { + if (typeof window.console[method] !== 'function') { + return; + } - consoleOutput.push(logEvent); - oldFunction(...args); - }; - }); + const oldFunction = window.console[method].bind(window.console); - window.addEventListener('error', function (event) { - if (consoleOutput.length >= MAX_LOGS) { - consoleOutput.shift(); - } + window.console[method] = function (...args): void { + const stack = + new Error().stack?.split('\n').slice(2).join('\n') || ''; - const logEvent: ConsoleLogEvent = { - method: 'error', - timestamp: new Date(), - type: event.error?.name || 'Error', - message: event.error?.message || event.message, - stack: event.error?.stack || '', - fileLine: event.filename - ? `${event.filename}:${event.lineno}:${event.colno}` - : '', - }; + const logEvent: ConsoleLogEvent = { + method, + timestamp: new Date(), + type: method, + message: args + .map((arg) => + typeof arg === 'string' ? arg : JSON.stringify(arg) + ) + .join(' '), + stack, + fileLine: stack.split('\n')[0]?.trim(), + }; - consoleOutput.push(logEvent); - }); -} + addToConsoleOutput(logEvent); + oldFunction(...args); + }; + }); + }, -/** - * Returns the stack of captured console logs. - * - * @returns {ConsoleLogEvent[]} Array of logged console events. - */ -export function getConsoleLogStack(): ConsoleLogEvent[] { - return [ ...consoleOutput ]; -} + addErrorEvent(event: ErrorEvent | PromiseRejectionEvent): void { + const logEvent = createConsoleEventFromError(event); + addToConsoleOutput(logEvent); + }, + + getConsoleLogStack(): ConsoleLogEvent[] { + return [...consoleOutput]; + }, + }; +}; + +const consoleCatcher = createConsoleCatcher(); +export const { initConsoleCatcher, getConsoleLogStack, addErrorEvent } = + consoleCatcher; diff --git a/src/catcher.ts b/src/catcher.ts index b4e5298..400ac6d 100644 --- a/src/catcher.ts +++ b/src/catcher.ts @@ -16,7 +16,7 @@ import type { JavaScriptCatcherIntegrations } from './types/integrations'; import { EventRejectedError } from './errors'; import type { HawkJavaScriptEvent } from './types'; import { isErrorProcessed, markErrorAsProcessed } from './utils/event'; -import { getConsoleLogStack, initConsoleCatcher } from './addons/consoleCatcher'; +import { addErrorEvent, getConsoleLogStack, initConsoleCatcher } from './addons/consoleCatcher'; /** * Allow to use global VERSION, that will be overwritten by Webpack @@ -228,6 +228,12 @@ export default class Catcher { * @param {ErrorEvent|PromiseRejectionEvent} event — (!) both for Error and Promise Rejection */ private async handleEvent(event: ErrorEvent | PromiseRejectionEvent): Promise { + /** + * Add error to console logs + */ + + addErrorEvent(event); + /** * Promise rejection reason is recommended to be an Error, but it can be a string: * - Promise.reject(new Error('Reason message')) ——— recommended From 484f0bceeff4f21b7f3ec9824ea3d6116b0af424 Mon Sep 17 00:00:00 2001 From: Dobrunia Kostrigin <48620984+Dobrunia@users.noreply.github.com> Date: Sat, 15 Mar 2025 19:41:56 +0300 Subject: [PATCH 7/9] fix --- src/addons/consoleCatcher.ts | 43 ++++++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/src/addons/consoleCatcher.ts b/src/addons/consoleCatcher.ts index b67a8fe..2044047 100644 --- a/src/addons/consoleCatcher.ts +++ b/src/addons/consoleCatcher.ts @@ -4,18 +4,35 @@ import type { ConsoleLogEvent } from '@hawk.so/types'; -const createConsoleCatcher = () => { +/** + * Factory function to create a console log catcher + * @returns Object with methods for initializing, retrieving, and adding console logs + */ +const createConsoleCatcher = (): { + initConsoleCatcher: () => void; + addErrorEvent: (event: ErrorEvent | PromiseRejectionEvent) => void; + getConsoleLogStack: () => ConsoleLogEvent[]; +} => { const MAX_LOGS = 20; const consoleOutput: ConsoleLogEvent[] = []; let isInitialized = false; - const addToConsoleOutput = (logEvent: ConsoleLogEvent) => { + /** + * Adds a log event to the console output buffer + * @param logEvent - The log event to store + */ + const addToConsoleOutput = (logEvent: ConsoleLogEvent): void => { if (consoleOutput.length >= MAX_LOGS) { consoleOutput.shift(); } consoleOutput.push(logEvent); }; + /** + * Creates a ConsoleLogEvent from an ErrorEvent or PromiseRejectionEvent + * @param event - The event to process + * @returns The formatted log event + */ const createConsoleEventFromError = ( event: ErrorEvent | PromiseRejectionEvent ): ConsoleLogEvent => { @@ -43,13 +60,16 @@ const createConsoleCatcher = () => { }; return { + /** + * Initializes the console catcher by overriding console methods + */ initConsoleCatcher(): void { if (isInitialized) { return; } isInitialized = true; - const consoleMethods = ['log', 'warn', 'error', 'info', 'debug']; + const consoleMethods: string[] = ['log', 'warn', 'error', 'info', 'debug']; consoleMethods.forEach((method) => { if (typeof window.console[method] !== 'function') { @@ -58,7 +78,7 @@ const createConsoleCatcher = () => { const oldFunction = window.console[method].bind(window.console); - window.console[method] = function (...args): void { + window.console[method] = function (...args: unknown[]): void { const stack = new Error().stack?.split('\n').slice(2).join('\n') || ''; @@ -67,10 +87,7 @@ const createConsoleCatcher = () => { timestamp: new Date(), type: method, message: args - .map((arg) => - typeof arg === 'string' ? arg : JSON.stringify(arg) - ) - .join(' '), + .map((arg) => typeof arg === 'string' ? arg : JSON.stringify(arg)).join(' '), stack, fileLine: stack.split('\n')[0]?.trim(), }; @@ -81,13 +98,21 @@ const createConsoleCatcher = () => { }); }, + /** + * Adds an error event (either an ErrorEvent or a PromiseRejectionEvent) to the log stack + * @param event - The error event to log + */ addErrorEvent(event: ErrorEvent | PromiseRejectionEvent): void { const logEvent = createConsoleEventFromError(event); addToConsoleOutput(logEvent); }, + /** + * Retrieves the current console log stack + * @returns A copy of the console log stack + */ getConsoleLogStack(): ConsoleLogEvent[] { - return [...consoleOutput]; + return [ ...consoleOutput ]; }, }; }; From 545ba8bad0ec4d4bc2d5fe486edc02710c80370a Mon Sep 17 00:00:00 2001 From: Dobrunia Kostrigin <48620984+Dobrunia@users.noreply.github.com> Date: Sat, 15 Mar 2025 19:44:49 +0300 Subject: [PATCH 8/9] fix --- src/addons/consoleCatcher.ts | 24 ------------------------ 1 file changed, 24 deletions(-) diff --git a/src/addons/consoleCatcher.ts b/src/addons/consoleCatcher.ts index 2044047..b121043 100644 --- a/src/addons/consoleCatcher.ts +++ b/src/addons/consoleCatcher.ts @@ -4,10 +4,6 @@ import type { ConsoleLogEvent } from '@hawk.so/types'; -/** - * Factory function to create a console log catcher - * @returns Object with methods for initializing, retrieving, and adding console logs - */ const createConsoleCatcher = (): { initConsoleCatcher: () => void; addErrorEvent: (event: ErrorEvent | PromiseRejectionEvent) => void; @@ -17,10 +13,6 @@ const createConsoleCatcher = (): { const consoleOutput: ConsoleLogEvent[] = []; let isInitialized = false; - /** - * Adds a log event to the console output buffer - * @param logEvent - The log event to store - */ const addToConsoleOutput = (logEvent: ConsoleLogEvent): void => { if (consoleOutput.length >= MAX_LOGS) { consoleOutput.shift(); @@ -28,11 +20,6 @@ const createConsoleCatcher = (): { consoleOutput.push(logEvent); }; - /** - * Creates a ConsoleLogEvent from an ErrorEvent or PromiseRejectionEvent - * @param event - The event to process - * @returns The formatted log event - */ const createConsoleEventFromError = ( event: ErrorEvent | PromiseRejectionEvent ): ConsoleLogEvent => { @@ -60,9 +47,6 @@ const createConsoleCatcher = (): { }; return { - /** - * Initializes the console catcher by overriding console methods - */ initConsoleCatcher(): void { if (isInitialized) { return; @@ -98,19 +82,11 @@ const createConsoleCatcher = (): { }); }, - /** - * Adds an error event (either an ErrorEvent or a PromiseRejectionEvent) to the log stack - * @param event - The error event to log - */ addErrorEvent(event: ErrorEvent | PromiseRejectionEvent): void { const logEvent = createConsoleEventFromError(event); addToConsoleOutput(logEvent); }, - /** - * Retrieves the current console log stack - * @returns A copy of the console log stack - */ getConsoleLogStack(): ConsoleLogEvent[] { return [ ...consoleOutput ]; }, From 286f16fda21855fbfb721efe7428cb4d97a42644 Mon Sep 17 00:00:00 2001 From: Dobrunia Kostrigin <48620984+Dobrunia@users.noreply.github.com> Date: Sat, 15 Mar 2025 19:47:04 +0300 Subject: [PATCH 9/9] fix --- src/addons/consoleCatcher.ts | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/addons/consoleCatcher.ts b/src/addons/consoleCatcher.ts index b121043..fc8fa13 100644 --- a/src/addons/consoleCatcher.ts +++ b/src/addons/consoleCatcher.ts @@ -63,15 +63,13 @@ const createConsoleCatcher = (): { const oldFunction = window.console[method].bind(window.console); window.console[method] = function (...args: unknown[]): void { - const stack = - new Error().stack?.split('\n').slice(2).join('\n') || ''; + const stack = new Error().stack?.split('\n').slice(2).join('\n') || ''; const logEvent: ConsoleLogEvent = { method, timestamp: new Date(), type: method, - message: args - .map((arg) => typeof arg === 'string' ? arg : JSON.stringify(arg)).join(' '), + message: args.map((arg) => typeof arg === 'string' ? arg : JSON.stringify(arg)).join(' '), stack, fileLine: stack.split('\n')[0]?.trim(), }; @@ -84,6 +82,7 @@ const createConsoleCatcher = (): { addErrorEvent(event: ErrorEvent | PromiseRejectionEvent): void { const logEvent = createConsoleEventFromError(event); + addToConsoleOutput(logEvent); }, @@ -94,5 +93,4 @@ const createConsoleCatcher = (): { }; const consoleCatcher = createConsoleCatcher(); -export const { initConsoleCatcher, getConsoleLogStack, addErrorEvent } = - consoleCatcher; +export const { initConsoleCatcher, getConsoleLogStack, addErrorEvent } = consoleCatcher;