Skip to content

Commit c588f78

Browse files
author
Luca Forstner
committed
Wrap under the assumption that server components may return promises and non-promises
1 parent 57b44df commit c588f78

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

packages/nextjs/src/edge/wrapAppDirComponentWithSentry.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,28 @@ import { captureException } from '@sentry/core';
44
* Wraps an `app` directory server component with Sentry error instrumentation.
55
*/
66
export function wrapAppDirComponentWithSentry(wrappingTarget: any): any {
7-
// Super interesting: even though users may define server components as async functions, Next.js will turn them into
8-
// synchronous functions and it will transform any`await`s into instances of the`use` hook. 🤯
7+
// Even though users may define server components as async functions, for the client bundles
8+
// Next.js will turn them into synchronous functionsf and it will transform any`await`s into instances of the`use`
9+
// hook. 🤯
910
return function sentryWrappedServerComponent(this: unknown, ...args: any[]) {
11+
let maybePromiseResult;
12+
1013
try {
1114
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
12-
return wrappingTarget.apply(this, args);
15+
maybePromiseResult = wrappingTarget.apply(this, args);
1316
} catch (e) {
1417
captureException(e);
1518
throw e;
1619
}
20+
21+
if (typeof maybePromiseResult === 'object' && maybePromiseResult !== null && 'then' in maybePromiseResult) {
22+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
23+
return maybePromiseResult.then(null, (e: Error) => {
24+
captureException(e);
25+
throw e;
26+
});
27+
} else {
28+
return maybePromiseResult;
29+
}
1730
};
1831
}

packages/nextjs/src/server/wrapAppDirComponentWithSentry.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,28 @@ import { captureException } from '@sentry/core';
44
* Wraps an `app` directory server component with Sentry error instrumentation.
55
*/
66
export function wrapAppDirComponentWithSentry(wrappingTarget: any): any {
7-
// Super interesting: even though users may define server components as async functions, Next.js will turn them into
8-
// synchronous functions and it will transform any`await`s into instances of the`use` hook. 🤯
7+
// Even though users may define server components as async functions, for the client bundles
8+
// Next.js will turn them into synchronous functionsf and it will transform any`await`s into instances of the`use`
9+
// hook. 🤯
910
return function sentryWrappedServerComponent(this: unknown, ...args: any[]) {
11+
let maybePromiseResult;
12+
1013
try {
1114
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
12-
return wrappingTarget.apply(this, args);
15+
maybePromiseResult = wrappingTarget.apply(this, args);
1316
} catch (e) {
1417
captureException(e);
1518
throw e;
1619
}
20+
21+
if (typeof maybePromiseResult === 'object' && maybePromiseResult !== null && 'then' in maybePromiseResult) {
22+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
23+
return maybePromiseResult.then(null, (e: Error) => {
24+
captureException(e);
25+
throw e;
26+
});
27+
} else {
28+
return maybePromiseResult;
29+
}
1730
};
1831
}

0 commit comments

Comments
 (0)