Skip to content

Commit fd4db0d

Browse files
authored
fix(nextjs): Use Proxies to wrap to preserve static methods (#7002)
1 parent f352f97 commit fd4db0d

15 files changed

+402
-430
lines changed

packages/nextjs/src/client/wrapAppGetInitialPropsWithSentry.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ type AppGetInitialProps = typeof App['getInitialProps'];
77
* so we are consistent with the serverside implementation.
88
*/
99
export function wrapAppGetInitialPropsWithSentry(origAppGetInitialProps: AppGetInitialProps): AppGetInitialProps {
10-
return async function (this: unknown, ...args: Parameters<AppGetInitialProps>): ReturnType<AppGetInitialProps> {
11-
return await origAppGetInitialProps.apply(this, args);
12-
};
10+
return new Proxy(origAppGetInitialProps, {
11+
apply: async (wrappingTarget, thisArg, args: Parameters<AppGetInitialProps>) => {
12+
return await wrappingTarget.apply(thisArg, args);
13+
},
14+
});
1315
}
1416

1517
/**

packages/nextjs/src/client/wrapDocumentGetInitialPropsWithSentry.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@ type DocumentGetInitialProps = typeof Document.getInitialProps;
99
export function wrapDocumentGetInitialPropsWithSentry(
1010
origDocumentGetInitialProps: DocumentGetInitialProps,
1111
): DocumentGetInitialProps {
12-
return async function (
13-
this: unknown,
14-
...args: Parameters<DocumentGetInitialProps>
15-
): ReturnType<DocumentGetInitialProps> {
16-
return await origDocumentGetInitialProps.apply(this, args);
17-
};
12+
return new Proxy(origDocumentGetInitialProps, {
13+
apply: async (wrappingTarget, thisArg, args: Parameters<DocumentGetInitialProps>) => {
14+
return await wrappingTarget.apply(thisArg, args);
15+
},
16+
});
1817
}
1918

2019
/**

packages/nextjs/src/client/wrapErrorGetInitialPropsWithSentry.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ type ErrorGetInitialProps = (context: NextPageContext) => Promise<ErrorProps>;
1010
export function wrapErrorGetInitialPropsWithSentry(
1111
origErrorGetInitialProps: ErrorGetInitialProps,
1212
): ErrorGetInitialProps {
13-
return async function (this: unknown, ...args: Parameters<ErrorGetInitialProps>): ReturnType<ErrorGetInitialProps> {
14-
return await origErrorGetInitialProps.apply(this, args);
15-
};
13+
return new Proxy(origErrorGetInitialProps, {
14+
apply: async (wrappingTarget, thisArg, args: Parameters<ErrorGetInitialProps>) => {
15+
return await wrappingTarget.apply(thisArg, args);
16+
},
17+
});
1618
}
1719

1820
/**

packages/nextjs/src/client/wrapGetInitialPropsWithSentry.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ type GetInitialProps = Required<NextPage>['getInitialProps'];
77
* so we are consistent with the serverside implementation.
88
*/
99
export function wrapGetInitialPropsWithSentry(origGetInitialProps: GetInitialProps): GetInitialProps {
10-
return async function (this: unknown, ...args: Parameters<GetInitialProps>): Promise<ReturnType<GetInitialProps>> {
11-
return origGetInitialProps.apply(this, args);
12-
};
10+
return new Proxy(origGetInitialProps, {
11+
apply: async (wrappingTarget, thisArg, args: Parameters<GetInitialProps>) => {
12+
return await wrappingTarget.apply(thisArg, args);
13+
},
14+
});
1315
}
1416

1517
/**

packages/nextjs/src/edge/wrapApiHandlerWithSentry.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,24 @@ export function wrapApiHandlerWithSentry<H extends EdgeRouteHandler>(
1010
handler: H,
1111
parameterizedRoute: string,
1212
): (...params: Parameters<H>) => Promise<ReturnType<H>> {
13-
return async function (this: unknown, ...args: Parameters<H>): Promise<ReturnType<H>> {
14-
const req = args[0];
13+
return new Proxy(handler, {
14+
apply: async (wrappingTarget, thisArg, args: Parameters<H>) => {
15+
const req = args[0];
1516

16-
const activeSpan = !!getCurrentHub().getScope()?.getSpan();
17+
const activeSpan = !!getCurrentHub().getScope()?.getSpan();
1718

18-
const wrappedHandler = withEdgeWrapping(handler, {
19-
spanDescription:
20-
activeSpan || !(req instanceof Request)
21-
? `handler (${parameterizedRoute})`
22-
: `${req.method} ${parameterizedRoute}`,
23-
spanOp: activeSpan ? 'function' : 'http.server',
24-
mechanismFunctionName: 'wrapApiHandlerWithSentry',
25-
});
19+
const wrappedHandler = withEdgeWrapping(wrappingTarget, {
20+
spanDescription:
21+
activeSpan || !(req instanceof Request)
22+
? `handler (${parameterizedRoute})`
23+
: `${req.method} ${parameterizedRoute}`,
24+
spanOp: activeSpan ? 'function' : 'http.server',
25+
mechanismFunctionName: 'wrapApiHandlerWithSentry',
26+
});
2627

27-
return await wrappedHandler.apply(this, args);
28-
};
28+
return await wrappedHandler.apply(thisArg, args);
29+
},
30+
});
2931
}
3032

3133
/**

packages/nextjs/src/edge/wrapMiddlewareWithSentry.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@ import { withEdgeWrapping } from './utils/edgeWrapperUtils';
1010
export function wrapMiddlewareWithSentry<H extends EdgeRouteHandler>(
1111
middleware: H,
1212
): (...params: Parameters<H>) => Promise<ReturnType<H>> {
13-
return withEdgeWrapping(middleware, {
14-
spanDescription: 'middleware',
15-
spanOp: 'middleware.nextjs',
16-
mechanismFunctionName: 'withSentryMiddleware',
13+
return new Proxy(middleware, {
14+
apply: async (wrappingTarget, thisArg, args: Parameters<H>) => {
15+
return withEdgeWrapping(wrappingTarget, {
16+
spanDescription: 'middleware',
17+
spanOp: 'middleware.nextjs',
18+
mechanismFunctionName: 'withSentryMiddleware',
19+
}).apply(thisArg, args);
20+
},
1721
});
1822
}

packages/nextjs/src/server/utils/nextLogger.ts

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)