diff --git a/packages/nextjs/src/config/wrappers/withSentryGetInitialProps.ts b/packages/nextjs/src/config/wrappers/withSentryGetInitialProps.ts index 9091628a91a2..dcfca89779dc 100644 --- a/packages/nextjs/src/config/wrappers/withSentryGetInitialProps.ts +++ b/packages/nextjs/src/config/wrappers/withSentryGetInitialProps.ts @@ -7,14 +7,20 @@ type GetInitialProps = Required>['getInitialProps']; /** * Create a wrapped version of the user's exported `getInitialProps` function * - * @param origGetInitialProps: The user's `getInitialProps` function - * @param origGIPropsHost: The user's object on which `getInitialProps` lives (used for `this`) + * @param origGetInitialProps - The user's `getInitialProps` function + * @param parameterizedRoute - The page's parameterized route * @returns A wrapped version of the function */ -export function withSentryGetInitialProps(origGetInitialProps: GetInitialProps, route: string): GetInitialProps { +export function withSentryGetInitialProps( + origGetInitialProps: GetInitialProps, + parameterizedRoute: string, +): GetInitialProps { return async function ( ...getInitialPropsArguments: Parameters ): Promise> { - return await callDataFetcherTraced(origGetInitialProps, getInitialPropsArguments, { route, op: 'getInitialProps' }); + return callDataFetcherTraced(origGetInitialProps, getInitialPropsArguments, { + parameterizedRoute, + dataFetchingMethodName: 'getInitialProps', + }); }; } diff --git a/packages/nextjs/src/config/wrappers/withSentryGetServerSideProps.ts b/packages/nextjs/src/config/wrappers/withSentryGetServerSideProps.ts index c8f0705d347b..1df647145868 100644 --- a/packages/nextjs/src/config/wrappers/withSentryGetServerSideProps.ts +++ b/packages/nextjs/src/config/wrappers/withSentryGetServerSideProps.ts @@ -5,20 +5,20 @@ import { callDataFetcherTraced } from './wrapperUtils'; /** * Create a wrapped version of the user's exported `getServerSideProps` function * - * @param origGetServerSideProps: The user's `getServerSideProps` function - * @param route: The page's parameterized route + * @param origGetServerSideProps - The user's `getServerSideProps` function + * @param parameterizedRoute - The page's parameterized route * @returns A wrapped version of the function */ export function withSentryGetServerSideProps( origGetServerSideProps: GetServerSideProps, - route: string, + parameterizedRoute: string, ): GetServerSideProps { return async function ( ...getServerSidePropsArguments: Parameters ): ReturnType { - return await callDataFetcherTraced(origGetServerSideProps, getServerSidePropsArguments, { - route, - op: 'getServerSideProps', + return callDataFetcherTraced(origGetServerSideProps, getServerSidePropsArguments, { + parameterizedRoute, + dataFetchingMethodName: 'getServerSideProps', }); }; } diff --git a/packages/nextjs/src/config/wrappers/withSentryGetStaticProps.ts b/packages/nextjs/src/config/wrappers/withSentryGetStaticProps.ts index 494faf05ffda..17508e2e87b9 100644 --- a/packages/nextjs/src/config/wrappers/withSentryGetStaticProps.ts +++ b/packages/nextjs/src/config/wrappers/withSentryGetStaticProps.ts @@ -8,16 +8,19 @@ type Props = { [key: string]: unknown }; * Create a wrapped version of the user's exported `getStaticProps` function * * @param origGetStaticProps: The user's `getStaticProps` function - * @param route: The page's parameterized route + * @param parameterizedRoute - The page's parameterized route * @returns A wrapped version of the function */ export function withSentryGetStaticProps( origGetStaticProps: GetStaticProps, - route: string, + parameterizedRoute: string, ): GetStaticProps { return async function ( ...getStaticPropsArguments: Parameters> ): ReturnType> { - return await callDataFetcherTraced(origGetStaticProps, getStaticPropsArguments, { route, op: 'getStaticProps' }); + return callDataFetcherTraced(origGetStaticProps, getStaticPropsArguments, { + parameterizedRoute, + dataFetchingMethodName: 'getStaticProps', + }); }; } diff --git a/packages/nextjs/src/config/wrappers/wrapperUtils.ts b/packages/nextjs/src/config/wrappers/wrapperUtils.ts index ae4b36ed64ff..ab216f34aeaf 100644 --- a/packages/nextjs/src/config/wrappers/wrapperUtils.ts +++ b/packages/nextjs/src/config/wrappers/wrapperUtils.ts @@ -1,6 +1,5 @@ import { captureException } from '@sentry/core'; import { getActiveTransaction } from '@sentry/tracing'; -import { Span } from '@sentry/types'; /** * Call a data fetcher and trace it. Only traces the function if there is an active transaction on the scope. @@ -12,11 +11,11 @@ export async function callDataFetcherTraced Promis origFunction: F, origFunctionArgs: Parameters, options: { - route: string; - op: string; + parameterizedRoute: string; + dataFetchingMethodName: string; }, ): Promise> { - const { route, op } = options; + const { parameterizedRoute, dataFetchingMethodName } = options; const transaction = getActiveTransaction(); @@ -24,24 +23,30 @@ export async function callDataFetcherTraced Promis return origFunction(...origFunctionArgs); } - // Pull off any leading underscores we've added in the process of wrapping the function - const wrappedFunctionName = origFunction.name.replace(/^_*/, ''); - // TODO: Make sure that the given route matches the name of the active transaction (to prevent background data // fetching from switching the name to a completely other route) -- We'll probably switch to creating a transaction // right here so making that check will probabably not even be necessary. // Logic will be: If there is no active transaction, start one with correct name and source. If there is an active // transaction, create a child span with correct name and source. - transaction.name = route; + transaction.name = parameterizedRoute; transaction.metadata.source = 'route'; // Capture the route, since pre-loading, revalidation, etc might mean that this span may happen during another // route's transaction - const span = transaction.startChild({ op: 'nextjs.data', description: `${wrappedFunctionName} (${route})` }); + const span = transaction.startChild({ + op: 'nextjs.data', + description: `${dataFetchingMethodName} (${parameterizedRoute})`, + }); try { return await origFunction(...origFunctionArgs); - } finally { - span.finish(); + } catch (err) { + if (span) { + span.finish(); + } + + // TODO Copy more robust error handling over from `withSentry` + captureException(err); + throw err; } }