-
Notifications
You must be signed in to change notification settings - Fork 120
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: manually capture errors #1374
Merged
Merged
Changes from 15 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
86c9a95
feat: manually capture errors
daibhin 5cc5b58
remove debugger
daibhin f8080f3
Merge branch 'main' into dn-feat/manual-error-capture
daibhin 37708f3
log if error autocapture not enabled
daibhin 72bd056
Merge branch 'main' into dn-feat/manual-error-capture
daibhin bf66497
limited exception capture in core bundle
daibhin 8de44cc
update sentry integration
daibhin 48163ef
remove import
daibhin 9330098
Merge branch 'main' into dn-feat/manual-error-capture
daibhin 0e643ca
inline error creation
daibhin 8a89499
cleanup comments
daibhin 306490b
include stack trace when autocapture is enabled
daibhin 880e3d1
cypress tests
daibhin a270d5a
remove observer test
daibhin 469b1f4
remove unused import
daibhin 0e39b1a
Merge branch 'main' into dn-feat/manual-error-capture
daibhin c70c1e6
more test conditions
daibhin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,75 @@ | ||
import { start } from '../support/setup' | ||
|
||
describe('Exception autocapture', () => { | ||
beforeEach(() => { | ||
cy.on('uncaught:exception', () => { | ||
// otherwise the exception we throw on purpose causes the test to fail | ||
return false | ||
}) | ||
|
||
describe('Exception capture', () => { | ||
it('manual exception capture', () => { | ||
start({ | ||
decideResponseOverrides: { | ||
autocaptureExceptions: true, | ||
autocaptureExceptions: false, | ||
}, | ||
url: './playground/cypress', | ||
}) | ||
cy.wait('@exception-autocapture-script') | ||
}) | ||
|
||
it('captures exceptions', () => { | ||
cy.get('[data-cy-button-throws-error]').click() | ||
cy.get('[data-cy-exception-button]').click() | ||
|
||
// ugh | ||
cy.wait(1500) | ||
|
||
cy.phCaptures({ full: true }).then((captures) => { | ||
expect(captures.map((c) => c.event)).to.deep.equal(['$pageview', '$autocapture', '$exception']) | ||
expect(captures[2].event).to.be.eql('$exception') | ||
expect(captures[2].properties.$exception_message).to.be.eql('This is an error') | ||
expect(captures[2].properties.$exception_message).to.be.eql('wat even am I') | ||
expect(captures[2].properties.$exception_type).to.be.eql('Error') | ||
expect(captures[2].properties.$exception_source).to.match(/http:\/\/localhost:\d+\/playground\/cypress\//) | ||
expect(captures[2].properties.$exception_personURL).to.match( | ||
/http:\/\/localhost:\d+\/project\/test_token\/person\/[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}/ | ||
) | ||
expect(captures[2].properties.$exception_source).to.eql(undefined) | ||
expect(captures[2].properties.$exception_personURL).to.eql(undefined) | ||
expect(captures[2].properties.$exception_stack_trace_raw).not.to.exist | ||
}) | ||
}) | ||
|
||
describe('Exception autocapture enabled', () => { | ||
beforeEach(() => { | ||
cy.on('uncaught:exception', () => { | ||
// otherwise the exception we throw on purpose causes the test to fail | ||
return false | ||
}) | ||
|
||
start({ | ||
decideResponseOverrides: { | ||
autocaptureExceptions: true, | ||
}, | ||
url: './playground/cypress', | ||
}) | ||
cy.wait('@exception-autocapture-script') | ||
}) | ||
|
||
it('autocaptures exceptions', () => { | ||
cy.get('[data-cy-button-throws-error]').click() | ||
|
||
// ugh | ||
cy.wait(1500) | ||
|
||
cy.phCaptures({ full: true }).then((captures) => { | ||
expect(captures.map((c) => c.event)).to.deep.equal(['$pageview', '$autocapture', '$exception']) | ||
expect(captures[2].event).to.be.eql('$exception') | ||
expect(captures[2].properties.$exception_message).to.be.eql('This is an error') | ||
expect(captures[2].properties.$exception_type).to.be.eql('Error') | ||
expect(captures[2].properties.$exception_source).to.match( | ||
/http:\/\/localhost:\d+\/playground\/cypress\// | ||
) | ||
expect(captures[2].properties.$exception_personURL).to.match( | ||
/http:\/\/localhost:\d+\/project\/test_token\/person\/[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}/ | ||
) | ||
}) | ||
}) | ||
|
||
it('sets stacktrace on manual captures if autocapture enabled', () => { | ||
cy.get('[data-cy-exception-button]').click() | ||
|
||
// ugh | ||
cy.wait(1500) | ||
|
||
cy.phCaptures({ full: true }).then((captures) => { | ||
expect(captures[2].properties.$exception_stack_trace_raw).to.exist | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: confirm this is also |
||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { EXCEPTION_CAPTURE_ENDPOINT_SUFFIX } from './constants' | ||
import { PostHog } from './posthog-core' | ||
import { DecideResponse, Properties } from './types' | ||
import { isObject } from './utils/type-utils' | ||
|
||
// TODO: move this to /x/ as default | ||
export const BASE_ERROR_ENDPOINT_SUFFIX = '/e/' | ||
|
||
export class PostHogExceptions { | ||
private _endpointSuffix: string | ||
|
||
constructor(private readonly instance: PostHog) { | ||
// TODO: once BASE_ERROR_ENDPOINT_SUFFIX is no longer /e/ this can be removed | ||
this._endpointSuffix = | ||
this.instance.persistence?.props[EXCEPTION_CAPTURE_ENDPOINT_SUFFIX] || BASE_ERROR_ENDPOINT_SUFFIX | ||
} | ||
|
||
get endpoint() { | ||
// Always respect any api_host set by the client config | ||
return this.instance.requestRouter.endpointFor('api', this._endpointSuffix) | ||
} | ||
|
||
afterDecideResponse(response: DecideResponse) { | ||
const autocaptureExceptionsResponse = response.autocaptureExceptions | ||
|
||
this._endpointSuffix = isObject(autocaptureExceptionsResponse) | ||
? autocaptureExceptionsResponse.endpoint || BASE_ERROR_ENDPOINT_SUFFIX | ||
: BASE_ERROR_ENDPOINT_SUFFIX | ||
|
||
if (this.instance.persistence) { | ||
// when we come to moving the endpoint to not /e/ | ||
// we'll want that to persist between startup and decide response | ||
// TODO: once BASE_ENDPOINT is no longer /e/ this can be removed | ||
this.instance.persistence.register({ | ||
[EXCEPTION_CAPTURE_ENDPOINT_SUFFIX]: this._endpointSuffix, | ||
}) | ||
} | ||
} | ||
|
||
/** | ||
* :TRICKY: Make sure we batch these requests | ||
*/ | ||
sendExceptionEvent(properties: Properties) { | ||
this.instance.capture('$exception', properties, { | ||
_noTruncate: true, | ||
_batchKey: 'exceptionEvent', | ||
_url: this.endpoint, | ||
}) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
minor nit: check extra_prop is set too?