Skip to content

Commit f45ceb6

Browse files
committed
Make callDataFetcherTraced async
1 parent ae9cb73 commit f45ceb6

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
@@ -6,14 +6,14 @@ import { getActiveTransaction } from '@sentry/tracing';
66
* We only do the following until we move transaction creation into this function: When called, the wrapped function
77
* will also update the name of the active transaction with a parameterized route provided via the `options` argument.
88
*/
9-
export function callDataFetcherTraced<F extends (...args: any[]) => Promise<any> | any>(
9+
export async function callDataFetcherTraced<F extends (...args: any[]) => Promise<any> | any>(
1010
origFunction: F,
1111
origFunctionArgs: Parameters<F>,
1212
options: {
1313
route: string;
1414
op: string;
1515
},
16-
): ReturnType<F> {
16+
): Promise<ReturnType<F>> {
1717
const { route, op } = options;
1818

1919
const transaction = getActiveTransaction();
@@ -34,24 +34,9 @@ export function callDataFetcherTraced<F extends (...args: any[]) => Promise<any>
3434
// route's transaction
3535
const span = transaction.startChild({ op, data: { route } });
3636

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

0 commit comments

Comments
 (0)