-
Notifications
You must be signed in to change notification settings - Fork 143
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
fix: capture when argument is not a string #1724
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR Summary
This PR improves error handling in the PostHog SDK by fixing cases where captureException()
is called with a string argument, preventing undefined type and value properties when autocapture is disabled.
- Added new
ErrorConversionArgs
type insrc/types.ts
to replace tuple-basedErrorEventArgs
for better type safety - Modified
errorToProperties
insrc/extensions/exception-autocapture/error-conversion.ts
to handle string inputs with proper fallback values - Added test coverage in
playwright/error-tracking.spec.ts
specifically for manual string exception capture scenarios - Added
isError
type guard insrc/utils/type-utils.ts
to properly check for Error instances - Updated error handling interface in
globals.ts
to use structuredErrorConversionArgs
type instead of tuples
8 file(s) reviewed, 4 comment(s)
Edit PR Review Bot Settings | Greptile
page, | ||
context | ||
) | ||
test.describe('Exception autocapture enabled', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logic: The describe block title 'Exception autocapture enabled' is incorrect since autocaptureExceptions is set to false in the config
test.describe('Exception autocapture enabled', () => { | |
test.describe('Exception autocapture disabled', () => { |
expect(captures[2].properties.$exception_personURL).toBeUndefined() | ||
expect(captures[2].properties.$exception_list[0].value).toEqual('I am a plain old string') | ||
expect(captures[2].properties.$exception_list[0].type).toEqual('Error') | ||
}) | ||
}) | ||
|
||
test.describe('Exception autocapture enabled', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: Duplicate describe block title 'Exception autocapture enabled' makes it unclear which tests are for which scenario
win.onerror = function (args: ErrorEventArgs): boolean { | ||
const errorProperties = errorToProperties({ event: args[0], error: args[4] }) | ||
captureFn(errorProperties) | ||
return originalOnError?.(...args) ?? false | ||
return originalOnError?.(args) ?? false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logic: The function signature changed from spread args to a single args parameter, but ErrorEventArgs is still treated as an array with indexed access. This could cause type safety issues.
win.onerror = function (args: ErrorEventArgs): boolean { | |
const errorProperties = errorToProperties({ event: args[0], error: args[4] }) | |
captureFn(errorProperties) | |
return originalOnError?.(...args) ?? false | |
return originalOnError?.(args) ?? false | |
win.onerror = function (message: string | Event, source?: string, lineno?: number, colno?: number, error?: Error): boolean { | |
const errorProperties = errorToProperties({ event: message, error: error }) | |
captureFn(errorProperties) | |
return originalOnError?.(message, source, lineno, colno, error) ?? false |
? { | ||
...assignableWindow.__PosthogExtensions__.parseErrorAsProperties( | ||
[error.message, undefined, undefined, undefined, error], | ||
isError(error) ? { error, event: error.message } : { event: error as Event | string }, | ||
// create synthetic error to get stack in cases where user input does not contain one |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: Check if window.PosthogExtensions exists before using parseErrorAsProperties to prevent undefined errors
? { | |
...assignableWindow.__PosthogExtensions__.parseErrorAsProperties( | |
[error.message, undefined, undefined, undefined, error], | |
isError(error) ? { error, event: error.message } : { event: error as Event | string }, | |
// create synthetic error to get stack in cases where user input does not contain one | |
? isFunction(assignableWindow.__PosthogExtensions__?.parseErrorAsProperties) | |
? { | |
...assignableWindow.__PosthogExtensions__.parseErrorAsProperties( | |
isError(error) ? { error, event: error.message } : { event: error as Event | string }, | |
// create synthetic error to get stack in cases where user input does not contain one |
Size Change: +873 B (+0.03%) Total Size: 3.3 MB
ℹ️ View Unchanged
|
Changes
Because you could call
captureException()
with a string it is possible that the type and value would be undefined if autocapture was disabled because of:Adds a fix to check for an error type and if not just use the provided value. This is best effort given we don't import the full
exception-conversion
file for this case