Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Adding the trackerror message method to pass to the event #142

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/rudderstack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,34 @@ export class RudderStack {
}
}

//tracking the track js error

trackConsoleErrors = (): any => {
let latestErrorMessage: string | null = null
// 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(' ')
latestErrorMessage = errorMessage
return errorMessage
}
return latestErrorMessage
}
Comment on lines +84 to +110
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Aswathy Menon we need to add this in cacheTrackEvents instead if we want to capture all errors. Also this function won't work you are returning the message but it will always be empty, it will only have value when error occurs but when that happened you won't get the hold of the value anymore.


/**
* 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.
Expand All @@ -106,6 +134,7 @@ export class RudderStack {
this.has_initialized = true
this.has_identified = !!(this.getUserId() || this.getAnonymousId())
this.handleCachedEvents()
this.trackConsoleErrors()
})
}
}
Expand Down
Loading