Skip to content

Commit

Permalink
fix(errors): Build endpoint for sentry integration & exception observ…
Browse files Browse the repository at this point in the history
…er correctly (#1390)
  • Loading branch information
neilkakkar authored Aug 28, 2024
1 parent 7f0dc89 commit d23bb17
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export class PromiseRejectionEvent extends Event {
describe('Exception Observer', () => {
let exceptionObserver: ExceptionObserver
let posthog: PostHog
let sendRequestSpy: jest.SpyInstance
const mockCapture = jest.fn()
const loadScriptMock = jest.fn()

Expand All @@ -53,6 +54,8 @@ describe('Exception Observer', () => {
posthog = await createPosthogInstance(uuidv7(), { _onCapture: mockCapture })
posthog.requestRouter.loadScript = loadScriptMock

sendRequestSpy = jest.spyOn(posthog, '_send_request')

exceptionObserver = new ExceptionObserver(posthog)
})

Expand Down Expand Up @@ -121,6 +124,25 @@ describe('Exception Observer', () => {
},
})
})

it('sends captured events to the right URL', () => {
const error = new Error('test error')
window!.onerror?.call(window, 'message', 'source', 0, 0, error)

expect(sendRequestSpy).toHaveBeenCalled()
const request = sendRequestSpy.mock.calls[0][0]
expect(request.url).toBe('http://localhost/e/?ip=1')
expect(request.data).toMatchObject({
event: '$exception',
properties: {
$exception_message: 'test error',
$exception_type: 'Error',
$exception_personURL: expect.any(String),
$exception_stack_trace_raw: expect.any(String),
},
})
expect(request.batchKey).toBe('exceptionEvent')
})
})

describe('when there are handlers already', () => {
Expand Down
11 changes: 6 additions & 5 deletions src/extensions/exception-autocapture/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const BASE_ERROR_ENDPOINT = '/e/'
const LOGGER_PREFIX = '[Exception Capture]'

export class ExceptionObserver {
private _endpoint: string
private _endpointSuffix: string
instance: PostHog
remoteEnabled: boolean | undefined
private originalOnUnhandledRejectionHandler: Window['onunhandledrejection'] | null | undefined = undefined
Expand All @@ -24,13 +24,14 @@ export class ExceptionObserver {
this.remoteEnabled = !!this.instance.persistence?.props[EXCEPTION_CAPTURE_ENABLED_SERVER_SIDE]

// TODO: once BASE_ERROR_ENDPOINT is no longer /e/ this can be removed
this._endpoint = this.instance.persistence?.props[EXCEPTION_CAPTURE_ENDPOINT] || BASE_ERROR_ENDPOINT
this._endpointSuffix = this.instance.persistence?.props[EXCEPTION_CAPTURE_ENDPOINT] || BASE_ERROR_ENDPOINT

this.startIfEnabled()
}

get endpoint() {
return this._endpoint
// Always respect any api_host set by the client config
return this.instance.requestRouter.endpointFor('api', this._endpointSuffix)
}

get isEnabled() {
Expand Down Expand Up @@ -98,7 +99,7 @@ export class ExceptionObserver {

// store this in-memory in case persistence is disabled
this.remoteEnabled = !!autocaptureExceptionsResponse || false
this._endpoint = isObject(autocaptureExceptionsResponse)
this._endpointSuffix = isObject(autocaptureExceptionsResponse)
? autocaptureExceptionsResponse.endpoint || BASE_ERROR_ENDPOINT
: BASE_ERROR_ENDPOINT

Expand All @@ -110,7 +111,7 @@ export class ExceptionObserver {
// 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]: this._endpoint,
[EXCEPTION_CAPTURE_ENDPOINT]: this._endpointSuffix,
})
}

Expand Down
5 changes: 4 additions & 1 deletion src/extensions/sentry-integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,10 @@ export function createEventProcessor(
// we take the URL from the exception observer
// so that when we add error specific URL for ingestion
// these errors are sent there too
_posthog.capture('$exception', data, { _url: _posthog.exceptionObserver?.endpoint || BASE_ERROR_ENDPOINT })
_posthog.capture('$exception', data, {
_url:
_posthog.exceptionObserver?.endpoint || _posthog.requestRouter.endpointFor('api', BASE_ERROR_ENDPOINT),
})

return event
}
Expand Down

0 comments on commit d23bb17

Please sign in to comment.