Skip to content

Commit 02c1d43

Browse files
committed
Make callDataFetcherTraced async
1 parent 7c93961 commit 02c1d43

File tree

5 files changed

+19
-31
lines changed

5 files changed

+19
-31
lines changed

packages/nextjs/src/config/loaders/dataFetchersLoader.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@ const DATA_FETCHING_FUNCTIONS = {
3838
type LoaderOptions = {
3939
projectDir: string;
4040
pagesDir: string;
41-
underscoreAppRegex: RegExp;
42-
underscoreErrorRegex: RegExp;
43-
underscoreDocumentRegex: RegExp;
4441
};
4542

4643
/**

packages/nextjs/src/config/wrappers/withSentryGetInitialProps.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ type GetInitialProps = Required<NextPage<unknown>>['getInitialProps'];
1212
* @returns A wrapped version of the function
1313
*/
1414
export function withSentryGetInitialProps(origGetInitialProps: GetInitialProps, route: string): GetInitialProps {
15-
return function (...getInitialPropsArguments: Parameters<GetInitialProps>): ReturnType<GetInitialProps> {
16-
return callDataFetcherTraced(origGetInitialProps, getInitialPropsArguments, { route, op: 'getInitialProps' });
15+
return async function (
16+
...getInitialPropsArguments: Parameters<GetInitialProps>
17+
): Promise<ReturnType<GetInitialProps>> {
18+
return await callDataFetcherTraced(origGetInitialProps, getInitialPropsArguments, { route, op: 'getInitialProps' });
1719
};
1820
}

packages/nextjs/src/config/wrappers/withSentryGetServerSideProps.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ export function withSentryGetServerSideProps(
1313
origGetServerSideProps: GetServerSideProps,
1414
route: string,
1515
): GetServerSideProps {
16-
return function (...getServerSidePropsArguments: Parameters<GetServerSideProps>): ReturnType<GetServerSideProps> {
17-
return callDataFetcherTraced(origGetServerSideProps, getServerSidePropsArguments, {
16+
return async function (
17+
...getServerSidePropsArguments: Parameters<GetServerSideProps>
18+
): ReturnType<GetServerSideProps> {
19+
return await callDataFetcherTraced(origGetServerSideProps, getServerSidePropsArguments, {
1820
route,
1921
op: 'getServerSideProps',
2022
});

packages/nextjs/src/config/wrappers/withSentryGetStaticProps.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ export function withSentryGetStaticProps(
1515
origGetStaticProps: GetStaticProps<Props>,
1616
route: string,
1717
): GetStaticProps<Props> {
18-
return function (...getStaticPropsArguments: Parameters<GetStaticProps<Props>>): ReturnType<GetStaticProps<Props>> {
19-
return callDataFetcherTraced(origGetStaticProps, getStaticPropsArguments, { route, op: 'getStaticProps' });
18+
return async function (
19+
...getStaticPropsArguments: Parameters<GetStaticProps<Props>>
20+
): ReturnType<GetStaticProps<Props>> {
21+
return await callDataFetcherTraced(origGetStaticProps, getStaticPropsArguments, { route, op: 'getStaticProps' });
2022
};
2123
}

packages/nextjs/src/config/wrappers/wrapperUtils.ts

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ import { Span } from '@sentry/types';
88
* We only do the following until we move transaction creation into this function: When called, the wrapped function
99
* will also update the name of the active transaction with a parameterized route provided via the `options` argument.
1010
*/
11-
export function callDataFetcherTraced<F extends (...args: any[]) => Promise<any> | any>(
11+
export async function callDataFetcherTraced<F extends (...args: any[]) => Promise<any> | any>(
1212
origFunction: F,
1313
origFunctionArgs: Parameters<F>,
1414
options: {
1515
route: string;
1616
op: string;
1717
},
18-
): ReturnType<F> {
18+
): Promise<ReturnType<F>> {
1919
const { route, op } = options;
2020

2121
const transaction = getActiveTransaction();
@@ -39,24 +39,9 @@ export function callDataFetcherTraced<F extends (...args: any[]) => Promise<any>
3939
// route's transaction
4040
const span = transaction.startChild({ op: 'nextjs.data', description: `${wrappedFunctionName} (${route})` });
4141

42-
const result = origFunction(...origFunctionArgs);
43-
44-
// We do the following instead of `await`-ing the return value of `origFunction`, because that would require us to
45-
// make this function async which might in turn create a mismatch of function signatures between the original
46-
// function and the wrapped one.
47-
// This wraps `result`, which is potentially a Promise, into a Promise.
48-
// If `result` is a non-Promise, the callback of `then` is immediately called and the span is finished.
49-
// If `result` is a Promise, the callback of `then` is only called when `result` resolves
50-
void Promise.resolve(result).then(
51-
() => {
52-
span.finish();
53-
},
54-
err => {
55-
// TODO: Can we somehow associate the error with the span?
56-
span.finish();
57-
throw err;
58-
},
59-
);
60-
61-
return result;
42+
try {
43+
return await origFunction(...origFunctionArgs);
44+
} finally {
45+
span.finish();
46+
}
6247
}

0 commit comments

Comments
 (0)