diff --git a/packages/nextjs/src/common/captureRequestError.ts b/packages/nextjs/src/common/captureRequestError.ts index e0e1d094a211..8f8ed48a19fb 100644 --- a/packages/nextjs/src/common/captureRequestError.ts +++ b/packages/nextjs/src/common/captureRequestError.ts @@ -1,5 +1,5 @@ import type { RequestEventData } from '@sentry/core'; -import { captureException, headersToDict, withScope } from '@sentry/core'; +import { captureException, filterCollectedUrl, headersToDict, withScope } from '@sentry/core'; import { isPrerenderControlFlowError } from './nextNavigationErrorUtils'; import { flushSafelyWithTimeout, waitUntil } from './utils/responseEnd'; @@ -32,7 +32,7 @@ export function captureRequestError(error: unknown, request: RequestInfo, errorC }); scope.setContext('nextjs', { - request_path: request.path, + request_path: filterCollectedUrl(request.path, scope.getClient()), router_kind: errorContext.routerKind, router_path: errorContext.routePath, route_type: errorContext.routeType, diff --git a/packages/nextjs/test/common/captureRequestError.test.ts b/packages/nextjs/test/common/captureRequestError.test.ts new file mode 100644 index 000000000000..abc6a0c5f44c --- /dev/null +++ b/packages/nextjs/test/common/captureRequestError.test.ts @@ -0,0 +1,41 @@ +import type { Event } from '@sentry/core'; +import { requestDataIntegration, withScope } from '@sentry/core'; +import { NodeClient } from '@sentry/node'; +import { describe, expect, it } from 'vitest'; +import { captureRequestError } from '../../src/common/captureRequestError'; + +describe('captureRequestError', () => { + it('omits the query from the request context when query collection is disabled', async () => { + const events: Event[] = []; + const client = new NodeClient({ + dsn: 'https://public@example.com/1', + integrations: [requestDataIntegration()], + stackParser: () => [], + transport: () => ({ send: async () => ({}), flush: async () => true }), + dataCollection: { urlQueryParams: false }, + beforeSend: event => { + events.push(event); + return null; + }, + }); + client.init(); + + await withScope(async scope => { + scope.setClient(client); + captureRequestError( + new Error('Search failed'), + { path: '/search?token=secret&category=books', method: 'GET', headers: {} }, + { routerKind: 'App Router', routePath: '/search', routeType: 'render' }, + ); + await client.flush(); + }); + + expect(events).toHaveLength(1); + expect(events[0]?.contexts?.nextjs).toEqual({ + request_path: '/search', + router_kind: 'App Router', + router_path: '/search', + route_type: 'render', + }); + }); +});