Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions src/platform/telemetry/initSentry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { beforeEach, expect, it, vi } from 'vitest'
vi.mock(import('@sentry/vue'), { spy: true })

import { initSentry } from './initSentry'
import { sentryThirdPartyErrorFilter } from './thirdPartyErrorNoise'
import { sentryBeforeSend } from './sentryBeforeSend'

function initOptions(isCloud: boolean) {
initSentry({
Expand Down Expand Up @@ -42,9 +42,9 @@ beforeEach(() => {
})

it.for([true, false])(
'installs the third-party error filter (isCloud: %s)',
'installs the beforeSend hook (isCloud: %s)',
(isCloud) => {
expect(initOptions(isCloud).beforeSend).toBe(sentryThirdPartyErrorFilter)
expect(initOptions(isCloud).beforeSend).toBe(sentryBeforeSend)
}
)

Expand Down
4 changes: 2 additions & 2 deletions src/platform/telemetry/initSentry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
init as sentryInit
} from '@sentry/vue'

import { sentryThirdPartyErrorFilter } from './thirdPartyErrorNoise'
import { sentryBeforeSend } from './sentryBeforeSend'

export function initSentry({
app,
Expand All @@ -27,7 +27,7 @@ export function initSentry({
tracesSampleRate: isCloud ? 1.0 : 0,
replaysSessionSampleRate: 0,
replaysOnErrorSampleRate: 0,
beforeSend: sentryThirdPartyErrorFilter,
beforeSend: sentryBeforeSend,
denyUrls: [/^(?:chrome|moz|safari(?:-web)?|ms-browser)-extension:\/\//],
// Only set these for non-cloud builds
...(isCloud
Expand Down
112 changes: 112 additions & 0 deletions src/platform/telemetry/sentryBeforeSend.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import type { ErrorEvent } from '@sentry/vue'
import { describe, expect, it } from 'vitest'

import {
NoWorkspaceAccessError,
WorkspaceApiError
} from '@/platform/workspace/api/workspaceApiError'

import { sentryBeforeSend } from './sentryBeforeSend'

function eventFor(error: Error): ErrorEvent {
return {
type: undefined,
tags: { surface: 'billing' },
exception: { values: [{ type: error.name, value: error.message }] }
}
}

describe('sentryBeforeSend', () => {
it.for([
{
name: 'a server rejection',
error: new WorkspaceApiError(
'Bad',
409,
'ALREADY_SUBSCRIBED',
undefined,
'subscribe'
),
fingerprint: [
'WorkspaceApiError',
'subscribe',
'409',
'ALREADY_SUBSCRIBED'
],
tags: {
workspace_api_operation: 'subscribe',
workspace_api_status: '409',
workspace_api_code: 'ALREADY_SUBSCRIBED'
}
},
{
name: 'a request that got no response',
error: new WorkspaceApiError(
'Network Error',
undefined,
undefined,
undefined,
'getBillingStatus'
),
fingerprint: ['WorkspaceApiError', 'getBillingStatus', 'none', 'none'],
tags: {
workspace_api_operation: 'getBillingStatus',
workspace_api_status: 'none',
workspace_api_code: 'none'
}
},
{
name: 'a no-workspace refusal',
error: new NoWorkspaceAccessError('No workspace', 403, 'list'),
fingerprint: [
'NoWorkspaceAccessError',
'list',
'403',
'no_workspace_access'
],
tags: {
workspace_api_operation: 'list',
workspace_api_status: '403',
workspace_api_code: 'no_workspace_access'
}
}
])(
'groups $name by operation, status and code',
({ error, fingerprint, tags }) => {
expect(
sentryBeforeSend(eventFor(error), { originalException: error })
).toMatchObject({
fingerprint,
tags: { surface: 'billing', ...tags }
})
}
)

it('tags but keeps default grouping when the operation is unknown', () => {
const error = new WorkspaceApiError('declined', 402, 'card_declined')

const sent = sentryBeforeSend(eventFor(error), { originalException: error })

expect(sent?.fingerprint).toBeUndefined()
expect(sent?.tags).toEqual({
surface: 'billing',
workspace_api_status: '402',
workspace_api_code: 'card_declined'
})
})

it('leaves other errors untouched', () => {
const error = new Error('boom')
const event = eventFor(error)

expect(sentryBeforeSend(event, { originalException: error })).toBe(event)
})

it('still drops third-party noise', () => {
const error = new DOMException('aborted', 'AbortError')

expect(
sentryBeforeSend(eventFor(error), { originalException: error })
).toBeNull()
})
})
42 changes: 42 additions & 0 deletions src/platform/telemetry/sentryBeforeSend.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import type { ErrorEvent, EventHint } from '@sentry/vue'

import { WorkspaceApiError } from '@/platform/workspace/api/workspaceApiError'

import { sentryThirdPartyErrorFilter } from './thirdPartyErrorNoise'

/**
* Groups a `WorkspaceApiError` by what failed rather than where it was
* caught, so each endpoint and server response gets its own Sentry issue.
* Errors built outside the HTTP client have no `operation` and keep Sentry's
* default grouping.
*/
function groupWorkspaceApiError(
event: ErrorEvent,
hint: EventHint
): ErrorEvent {
const error = hint.originalException
if (!(error instanceof WorkspaceApiError)) return event

const status = error.status === undefined ? 'none' : String(error.status)
const code = error.code ?? 'none'
const { operation } = error

return {
...event,
tags: {
...event.tags,
workspace_api_status: status,
workspace_api_code: code,
...(operation && { workspace_api_operation: operation })
},
...(operation && { fingerprint: [error.name, operation, status, code] })
}
}

export function sentryBeforeSend(
event: ErrorEvent,
hint: EventHint
): ErrorEvent | null {
const kept = sentryThirdPartyErrorFilter(event, hint)
return kept && groupWorkspaceApiError(kept, hint)
}
31 changes: 31 additions & 0 deletions src/platform/workspace/api/workspaceApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,37 @@ describe('workspaceApi', () => {
})
})

it.for([
{
operation: 'getBillingOpStatus',
call: () => workspaceApi.getBillingOpStatus('op-1'),
code: undefined
},
{
operation: 'acceptInvite',
call: () => workspaceApi.acceptInvite('token'),
code: undefined
},
{
operation: 'getCurrentWorkspace',
call: () => workspaceApi.getCurrentWorkspace(),
code: 'no_workspace_access'
}
])(
'names $operation as the failed operation',
async ({ operation, call, code }) => {
const axiosErr = {
isAxiosError: true,
response: { status: 403, data: { message: 'Forbidden', code } },
message: 'Request failed'
}
mockAxiosInstance.get.mockRejectedValue(axiosErr)
mockAxiosInstance.post.mockRejectedValue(axiosErr)

await expect(call()).rejects.toMatchObject({ operation })
}
)

it.for([
{ status: 403, code: 'no_workspace_access', typed: true },
{ status: 403, code: undefined, typed: false },
Expand Down
Loading
Loading