From 02c1d437db49ceab64dd778fcf5a96068fc581fc Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Tue, 16 Aug 2022 19:08:37 +0000 Subject: [PATCH] Make `callDataFetcherTraced` async --- .../src/config/loaders/dataFetchersLoader.ts | 3 -- .../wrappers/withSentryGetInitialProps.ts | 6 ++-- .../wrappers/withSentryGetServerSideProps.ts | 6 ++-- .../wrappers/withSentryGetStaticProps.ts | 6 ++-- .../src/config/wrappers/wrapperUtils.ts | 29 +++++-------------- 5 files changed, 19 insertions(+), 31 deletions(-) diff --git a/packages/nextjs/src/config/loaders/dataFetchersLoader.ts b/packages/nextjs/src/config/loaders/dataFetchersLoader.ts index 4c5688491ae1..d1608cbb8415 100644 --- a/packages/nextjs/src/config/loaders/dataFetchersLoader.ts +++ b/packages/nextjs/src/config/loaders/dataFetchersLoader.ts @@ -38,9 +38,6 @@ const DATA_FETCHING_FUNCTIONS = { type LoaderOptions = { projectDir: string; pagesDir: string; - underscoreAppRegex: RegExp; - underscoreErrorRegex: RegExp; - underscoreDocumentRegex: RegExp; }; /** diff --git a/packages/nextjs/src/config/wrappers/withSentryGetInitialProps.ts b/packages/nextjs/src/config/wrappers/withSentryGetInitialProps.ts index 046b8c32aa4f..9091628a91a2 100644 --- a/packages/nextjs/src/config/wrappers/withSentryGetInitialProps.ts +++ b/packages/nextjs/src/config/wrappers/withSentryGetInitialProps.ts @@ -12,7 +12,9 @@ type GetInitialProps = Required>['getInitialProps']; * @returns A wrapped version of the function */ export function withSentryGetInitialProps(origGetInitialProps: GetInitialProps, route: string): GetInitialProps { - return function (...getInitialPropsArguments: Parameters): ReturnType { - return callDataFetcherTraced(origGetInitialProps, getInitialPropsArguments, { route, op: 'getInitialProps' }); + return async function ( + ...getInitialPropsArguments: Parameters + ): Promise> { + return await callDataFetcherTraced(origGetInitialProps, getInitialPropsArguments, { route, op: 'getInitialProps' }); }; } diff --git a/packages/nextjs/src/config/wrappers/withSentryGetServerSideProps.ts b/packages/nextjs/src/config/wrappers/withSentryGetServerSideProps.ts index 0c5ea439b964..c8f0705d347b 100644 --- a/packages/nextjs/src/config/wrappers/withSentryGetServerSideProps.ts +++ b/packages/nextjs/src/config/wrappers/withSentryGetServerSideProps.ts @@ -13,8 +13,10 @@ export function withSentryGetServerSideProps( origGetServerSideProps: GetServerSideProps, route: string, ): GetServerSideProps { - return function (...getServerSidePropsArguments: Parameters): ReturnType { - return callDataFetcherTraced(origGetServerSideProps, getServerSidePropsArguments, { + return async function ( + ...getServerSidePropsArguments: Parameters + ): ReturnType { + return await callDataFetcherTraced(origGetServerSideProps, getServerSidePropsArguments, { route, op: 'getServerSideProps', }); diff --git a/packages/nextjs/src/config/wrappers/withSentryGetStaticProps.ts b/packages/nextjs/src/config/wrappers/withSentryGetStaticProps.ts index bf391921d1c2..494faf05ffda 100644 --- a/packages/nextjs/src/config/wrappers/withSentryGetStaticProps.ts +++ b/packages/nextjs/src/config/wrappers/withSentryGetStaticProps.ts @@ -15,7 +15,9 @@ export function withSentryGetStaticProps( origGetStaticProps: GetStaticProps, route: string, ): GetStaticProps { - return function (...getStaticPropsArguments: Parameters>): ReturnType> { - return callDataFetcherTraced(origGetStaticProps, getStaticPropsArguments, { route, op: 'getStaticProps' }); + return async function ( + ...getStaticPropsArguments: Parameters> + ): ReturnType> { + return await callDataFetcherTraced(origGetStaticProps, getStaticPropsArguments, { route, op: 'getStaticProps' }); }; } diff --git a/packages/nextjs/src/config/wrappers/wrapperUtils.ts b/packages/nextjs/src/config/wrappers/wrapperUtils.ts index 4604c692e4bf..ae4b36ed64ff 100644 --- a/packages/nextjs/src/config/wrappers/wrapperUtils.ts +++ b/packages/nextjs/src/config/wrappers/wrapperUtils.ts @@ -8,14 +8,14 @@ import { Span } from '@sentry/types'; * We only do the following until we move transaction creation into this function: When called, the wrapped function * will also update the name of the active transaction with a parameterized route provided via the `options` argument. */ -export function callDataFetcherTraced Promise | any>( +export async function callDataFetcherTraced Promise | any>( origFunction: F, origFunctionArgs: Parameters, options: { route: string; op: string; }, -): ReturnType { +): Promise> { const { route, op } = options; const transaction = getActiveTransaction(); @@ -39,24 +39,9 @@ export function callDataFetcherTraced Promise // route's transaction const span = transaction.startChild({ op: 'nextjs.data', description: `${wrappedFunctionName} (${route})` }); - const result = origFunction(...origFunctionArgs); - - // We do the following instead of `await`-ing the return value of `origFunction`, because that would require us to - // make this function async which might in turn create a mismatch of function signatures between the original - // function and the wrapped one. - // This wraps `result`, which is potentially a Promise, into a Promise. - // If `result` is a non-Promise, the callback of `then` is immediately called and the span is finished. - // If `result` is a Promise, the callback of `then` is only called when `result` resolves - void Promise.resolve(result).then( - () => { - span.finish(); - }, - err => { - // TODO: Can we somehow associate the error with the span? - span.finish(); - throw err; - }, - ); - - return result; + try { + return await origFunction(...origFunctionArgs); + } finally { + span.finish(); + } }