diff --git a/src/platform/telemetry/initSentry.test.ts b/src/platform/telemetry/initSentry.test.ts index a72946d6522..5364fa72b8b 100644 --- a/src/platform/telemetry/initSentry.test.ts +++ b/src/platform/telemetry/initSentry.test.ts @@ -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({ @@ -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) } ) diff --git a/src/platform/telemetry/initSentry.ts b/src/platform/telemetry/initSentry.ts index 5b7f75e3d42..339e5498f4b 100644 --- a/src/platform/telemetry/initSentry.ts +++ b/src/platform/telemetry/initSentry.ts @@ -5,7 +5,7 @@ import { init as sentryInit } from '@sentry/vue' -import { sentryThirdPartyErrorFilter } from './thirdPartyErrorNoise' +import { sentryBeforeSend } from './sentryBeforeSend' export function initSentry({ app, @@ -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 diff --git a/src/platform/telemetry/sentryBeforeSend.test.ts b/src/platform/telemetry/sentryBeforeSend.test.ts new file mode 100644 index 00000000000..885d92e3cc9 --- /dev/null +++ b/src/platform/telemetry/sentryBeforeSend.test.ts @@ -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() + }) +}) diff --git a/src/platform/telemetry/sentryBeforeSend.ts b/src/platform/telemetry/sentryBeforeSend.ts new file mode 100644 index 00000000000..52a5fd35897 --- /dev/null +++ b/src/platform/telemetry/sentryBeforeSend.ts @@ -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) +} diff --git a/src/platform/workspace/api/workspaceApi.test.ts b/src/platform/workspace/api/workspaceApi.test.ts index e967e2e22fd..f9cfd7c68f4 100644 --- a/src/platform/workspace/api/workspaceApi.test.ts +++ b/src/platform/workspace/api/workspaceApi.test.ts @@ -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 }, diff --git a/src/platform/workspace/api/workspaceApi.ts b/src/platform/workspace/api/workspaceApi.ts index c2014246f21..6a519697e12 100644 --- a/src/platform/workspace/api/workspaceApi.ts +++ b/src/platform/workspace/api/workspaceApi.ts @@ -176,7 +176,12 @@ async function requestAuth() { return { headers: await useAuthStore().getWorkspaceAuthHeaderOrThrow() } } -function handleAxiosError(err: unknown): never { +type WorkspaceApiOperation = keyof typeof workspaceApi + +function handleAxiosError( + err: unknown, + operation: WorkspaceApiOperation +): never { if (axios.isAxiosError(err)) { const status = err.response?.status const { code, message } = errorResponseFromBody( @@ -184,14 +189,16 @@ function handleAxiosError(err: unknown): never { err.message ) if (status === 403 && code === NO_WORKSPACE_ACCESS) { - throw new NoWorkspaceAccessError(message, status) + throw new NoWorkspaceAccessError(message, status, operation) } // Callers compare `code` against server-defined values, so the parser's // "no code reported" sentinel must stay out of that contract. throw new WorkspaceApiError( message, status, - code === UNKNOWN_ERROR_CODE ? undefined : code + code === UNKNOWN_ERROR_CODE ? undefined : code, + undefined, + operation ) } throw err @@ -211,7 +218,7 @@ export const workspaceApi = { ) return response.data } catch (err) { - handleAxiosError(err) + handleAxiosError(err, 'list') } }, @@ -228,7 +235,7 @@ export const workspaceApi = { ) return response.data } catch (err) { - handleAxiosError(err) + handleAxiosError(err, 'getCurrentWorkspace') } }, @@ -246,7 +253,7 @@ export const workspaceApi = { ) return response.data } catch (err) { - handleAxiosError(err) + handleAxiosError(err, 'create') } }, @@ -267,7 +274,7 @@ export const workspaceApi = { ) return response.data } catch (err) { - handleAxiosError(err) + handleAxiosError(err, 'update') } }, @@ -283,7 +290,7 @@ export const workspaceApi = { auth ) } catch (err) { - handleAxiosError(err) + handleAxiosError(err, 'delete') } }, @@ -300,7 +307,7 @@ export const workspaceApi = { auth ) } catch (err) { - handleAxiosError(err) + handleAxiosError(err, 'leave') } }, @@ -317,7 +324,7 @@ export const workspaceApi = { ) return response.data } catch (err) { - handleAxiosError(err) + handleAxiosError(err, 'listMembers') } }, @@ -333,7 +340,7 @@ export const workspaceApi = { auth ) } catch (err) { - handleAxiosError(err) + handleAxiosError(err, 'removeMember') } }, @@ -351,7 +358,7 @@ export const workspaceApi = { ) return response.data } catch (err) { - handleAxiosError(err) + handleAxiosError(err, 'updateMemberRole') } }, @@ -368,7 +375,7 @@ export const workspaceApi = { ) return response.data } catch (err) { - handleAxiosError(err) + handleAxiosError(err, 'listInvites') } }, @@ -386,7 +393,7 @@ export const workspaceApi = { ) return response.data } catch (err) { - handleAxiosError(err) + handleAxiosError(err, 'createInvite') } }, @@ -402,7 +409,7 @@ export const workspaceApi = { auth ) } catch (err) { - handleAxiosError(err) + handleAxiosError(err, 'revokeInvite') } }, @@ -418,7 +425,7 @@ export const workspaceApi = { ) return response.data } catch (err) { - handleAxiosError(err) + handleAxiosError(err, 'resendInvite') } }, @@ -437,7 +444,7 @@ export const workspaceApi = { ) return response.data } catch (err) { - handleAxiosError(err) + handleAxiosError(err, 'acceptInvite') } }, @@ -454,7 +461,7 @@ export const workspaceApi = { ) return response.data } catch (err) { - handleAxiosError(err) + handleAxiosError(err, 'getBillingStatus') } }, @@ -471,7 +478,7 @@ export const workspaceApi = { ) return response.data } catch (err) { - handleAxiosError(err) + handleAxiosError(err, 'getBillingBalance') } }, @@ -491,7 +498,7 @@ export const workspaceApi = { ) return response.data } catch (err) { - handleAxiosError(err) + handleAxiosError(err, 'getBillingCapabilities') } }, @@ -508,7 +515,7 @@ export const workspaceApi = { ) return response.data } catch (err) { - handleAxiosError(err) + handleAxiosError(err, 'getBillingPlans') } }, @@ -521,7 +528,7 @@ export const workspaceApi = { ) return response.data } catch (err) { - handleAxiosError(err) + handleAxiosError(err, 'listSavedPaymentMethods') } }, @@ -546,7 +553,7 @@ export const workspaceApi = { ) return response.data } catch (err) { - handleAxiosError(err) + handleAxiosError(err, 'previewSubscribe') } }, @@ -604,7 +611,7 @@ export const workspaceApi = { }) return response.data } catch (err) { - handleAxiosError(err) + handleAxiosError(err, 'subscribe') } }, @@ -627,7 +634,7 @@ export const workspaceApi = { ) return response.data } catch (err) { - handleAxiosError(err) + handleAxiosError(err, 'cancelSubscription') } }, @@ -640,7 +647,7 @@ export const workspaceApi = { ) return churnkeyAuthResponseSchema.parse(response.data) } catch (err) { - handleAxiosError(err) + handleAxiosError(err, 'getChurnkeyAuth') } }, @@ -658,7 +665,7 @@ export const workspaceApi = { ) return response.data } catch (err) { - handleAxiosError(err) + handleAxiosError(err, 'resubscribe') } }, @@ -678,7 +685,7 @@ export const workspaceApi = { ) return response.data } catch (err) { - handleAxiosError(err) + handleAxiosError(err, 'getPaymentPortalUrl') } }, @@ -714,7 +721,7 @@ export const workspaceApi = { }) return response.data } catch (err) { - handleAxiosError(err) + handleAxiosError(err, 'createTopup') } }, @@ -733,7 +740,7 @@ export const workspaceApi = { ) return response.data } catch (err) { - handleAxiosError(err) + handleAxiosError(err, 'getBillingEvents') } }, @@ -750,7 +757,7 @@ export const workspaceApi = { ) return response.data } catch (err) { - handleAxiosError(err) + handleAxiosError(err, 'getBillingOpStatus') } } } diff --git a/src/platform/workspace/api/workspaceApiError.ts b/src/platform/workspace/api/workspaceApiError.ts index dec397d0a43..f5e6ddbadce 100644 --- a/src/platform/workspace/api/workspaceApiError.ts +++ b/src/platform/workspace/api/workspaceApiError.ts @@ -10,7 +10,9 @@ export class WorkspaceApiError extends Error { public readonly status?: number, public readonly code?: string, /** For a failure `status` cannot classify, such as a billing SDK refusal. */ - public readonly failureCategory?: BillingTelemetryFailure['failure_category'] + public readonly failureCategory?: BillingTelemetryFailure['failure_category'], + /** The `workspaceApi` method that failed; unset outside the HTTP client. */ + public readonly operation?: string ) { super(message) this.name = 'WorkspaceApiError' @@ -22,8 +24,8 @@ export const NO_WORKSPACE_ACCESS = 'no_workspace_access' /** The account can enter no workspace: ingest refused with `no_workspace_access`, or listed none. */ export class NoWorkspaceAccessError extends WorkspaceApiError { - constructor(message: string, status?: number) { - super(message, status, NO_WORKSPACE_ACCESS) + constructor(message: string, status?: number, operation?: string) { + super(message, status, NO_WORKSPACE_ACCESS, undefined, operation) this.name = 'NoWorkspaceAccessError' } }