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

fix: capture when argument is not a string #1724

Merged
merged 3 commits into from
Feb 10, 2025
Merged

Conversation

daibhin
Copy link
Contributor

@daibhin daibhin commented Feb 10, 2025

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:

...
type: error.name,
value: error.message,
...

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

Copy link

vercel bot commented Feb 10, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
posthog-js ✅ Ready (Inspect) Visit Preview Feb 10, 2025 1:13pm

Copy link
Contributor

@greptile-apps greptile-apps bot left a 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 in src/types.ts to replace tuple-based ErrorEventArgs for better type safety
  • Modified errorToProperties in src/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 in src/utils/type-utils.ts to properly check for Error instances
  • Updated error handling interface in globals.ts to use structured ErrorConversionArgs type instead of tuples

8 file(s) reviewed, 4 comment(s)
Edit PR Review Bot Settings | Greptile

page,
context
)
test.describe('Exception autocapture enabled', () => {
Copy link
Contributor

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

Suggested change
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', () => {
Copy link
Contributor

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

Comment on lines 15 to 18
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
Copy link
Contributor

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.

Suggested change
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

Comment on lines 1841 to 1844
? {
...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
Copy link
Contributor

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

Suggested change
? {
...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

Copy link

github-actions bot commented Feb 10, 2025

Size Change: +873 B (+0.03%)

Total Size: 3.3 MB

Filename Size Change
dist/all-external-dependencies.js 216 kB +34 B (+0.02%)
dist/array.full.es5.js 268 kB +77 B (+0.03%)
dist/array.full.js 370 kB +88 B (+0.02%)
dist/array.full.no-external.js 369 kB +88 B (+0.02%)
dist/array.js 183 kB +109 B (+0.06%)
dist/array.no-external.js 181 kB +52 B (+0.03%)
dist/exception-autocapture.js 9.51 kB +34 B (+0.36%)
dist/main.js 184 kB +109 B (+0.06%)
dist/module.full.js 370 kB +88 B (+0.02%)
dist/module.full.no-external.js 369 kB +88 B (+0.02%)
dist/module.js 183 kB +53 B (+0.03%)
dist/module.no-external.js 181 kB +53 B (+0.03%)
ℹ️ View Unchanged
Filename Size
dist/customizations.full.js 13.8 kB
dist/dead-clicks-autocapture.js 14.5 kB
dist/external-scripts-loader.js 2.64 kB
dist/recorder-v2.js 115 kB
dist/recorder.js 115 kB
dist/surveys-preview.js 69.4 kB
dist/surveys.js 72.4 kB
dist/tracing-headers.js 1.76 kB
dist/web-vitals.js 10.4 kB

compressed-size-action

@daibhin daibhin added the bump patch Bump patch version when this PR gets merged label Feb 10, 2025
@daibhin daibhin merged commit 992acbc into main Feb 10, 2025
28 checks passed
@daibhin daibhin deleted the dn-fix/manual-string-capture branch February 10, 2025 14:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bump patch Bump patch version when this PR gets merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants