From 1f483ea53ab30be0862711974c456c97a7ea63b7 Mon Sep 17 00:00:00 2001 From: aswathy Date: Thu, 7 Nov 2024 16:48:11 +0400 Subject: [PATCH 1/4] feat: Adding the trackerror message method to pass to the event --- src/rudderstack.ts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/rudderstack.ts b/src/rudderstack.ts index 4a522f2..f440d2d 100644 --- a/src/rudderstack.ts +++ b/src/rudderstack.ts @@ -81,6 +81,31 @@ export class RudderStack { } } + //tracking the track js error + + trackConsoleErrors = (): void => { + // Preserve the original console.error function + const originalConsoleError = console.error + + // Override console.error to capture and handle errors + console.error = function (...args: unknown[]): any { + // Log the error to the console as usual + originalConsoleError.apply(console, args) + + // Create a clean error message without __trackjs_state__ + const errorMessage = args + .map(arg => + arg && typeof arg === 'object' && 'message' in arg + ? (arg as Error).message + : typeof arg === 'object' + ? JSON.stringify(arg, (key, value) => (key.startsWith('__trackjs') ? undefined : value)) + : String(arg) + ) + .join(' ') + return errorMessage + } + } + /** * Initializes the Rudderstack SDK. Ensure that the appropriate environment variables are set before this is called. * For local/staging environment, ensure that `RUDDERSTACK_STAGING_KEY` and `RUDDERSTACK_URL` is set. From 93f0b574c6aabbd77f5cf1b187e2f225d888b8fc Mon Sep 17 00:00:00 2001 From: aswathy Date: Thu, 7 Nov 2024 16:52:19 +0400 Subject: [PATCH 2/4] feat: added the method inside the analytics ready --- src/rudderstack.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/rudderstack.ts b/src/rudderstack.ts index f440d2d..0525171 100644 --- a/src/rudderstack.ts +++ b/src/rudderstack.ts @@ -131,6 +131,7 @@ export class RudderStack { this.has_initialized = true this.has_identified = !!(this.getUserId() || this.getAnonymousId()) this.handleCachedEvents() + this.trackConsoleErrors() }) } } From efd82cfdbcd8e877e5c66922600a148d78fa972a Mon Sep 17 00:00:00 2001 From: aswathy Date: Thu, 7 Nov 2024 17:54:12 +0400 Subject: [PATCH 3/4] fix: returning the error message --- src/rudderstack.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/rudderstack.ts b/src/rudderstack.ts index 0525171..9cf2247 100644 --- a/src/rudderstack.ts +++ b/src/rudderstack.ts @@ -6,6 +6,7 @@ export class RudderStack { analytics = new RudderAnalytics() has_identified = false has_initialized = false + track_error = '' current_page = '' rudderstack_anonymous_cookie_key = 'rudder_anonymous_id' private static _instance: RudderStack @@ -83,7 +84,8 @@ export class RudderStack { //tracking the track js error - trackConsoleErrors = (): void => { + trackConsoleErrors = (): any => { + let latestErrorMessage: string | null = null // Preserve the original console.error function const originalConsoleError = console.error @@ -102,8 +104,10 @@ export class RudderStack { : String(arg) ) .join(' ') + latestErrorMessage = errorMessage return errorMessage } + return latestErrorMessage } /** From efb14804449a4d39b24d41b57e3a8cdd56186171 Mon Sep 17 00:00:00 2001 From: aswathy Date: Thu, 7 Nov 2024 17:56:54 +0400 Subject: [PATCH 4/4] fix: globally to access the errormessage variable is defined --- src/rudderstack.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/rudderstack.ts b/src/rudderstack.ts index 9cf2247..a27c119 100644 --- a/src/rudderstack.ts +++ b/src/rudderstack.ts @@ -6,7 +6,6 @@ export class RudderStack { analytics = new RudderAnalytics() has_identified = false has_initialized = false - track_error = '' current_page = '' rudderstack_anonymous_cookie_key = 'rudder_anonymous_id' private static _instance: RudderStack