Skip to content

Commit a1e560b

Browse files
committed
use util
1 parent 5a64fb9 commit a1e560b

File tree

1 file changed

+26
-40
lines changed

1 file changed

+26
-40
lines changed

packages/nextjs/src/common/wrapServerComponentWithSentry.ts

Lines changed: 26 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
captureException,
44
continueTrace,
55
getCurrentScope,
6+
handleCallbackErrors,
67
runWithAsyncContext,
78
startSpanManual,
89
} from '@sentry/core';
@@ -69,50 +70,35 @@ export function wrapServerComponentWithSentry<F extends (...args: any[]) => any>
6970
},
7071
},
7172
span => {
72-
try {
73-
const res = originalFunction.apply(thisArg, args);
74-
span?.end();
75-
return res;
76-
} catch (error) {
77-
if (isNotFoundNavigationError(error)) {
78-
// We don't want to report "not-found"s
79-
span?.setStatus('not_found');
80-
} else if (isRedirectNavigationError(error)) {
81-
// We don't want to report redirects
82-
// Since `startSpan` will automatically set the span status to "internal_error" when an error is thrown,
83-
// We cannot set it to `ok` as that will lead to `startSpanManual` overwriting it when the error bubbles up,
84-
// so instead we temp. set this to `cancelled` and handle this later before we end the span.
85-
span?.setStatus('ok');
86-
} else {
87-
span?.setStatus('internal_error');
73+
return handleCallbackErrors(
74+
() => originalFunction.apply(thisArg, args),
75+
error => {
76+
if (isNotFoundNavigationError(error)) {
77+
// We don't want to report "not-found"s
78+
span?.setStatus('not_found');
79+
} else if (isRedirectNavigationError(error)) {
80+
// We don't want to report redirects
81+
span?.setStatus('ok');
82+
} else {
83+
span?.setStatus('internal_error');
8884

89-
captureException(error, {
90-
mechanism: {
91-
handled: false,
92-
},
93-
});
94-
}
95-
96-
span?.end();
97-
98-
// flushQueue should not throw
99-
// eslint-disable-next-line @typescript-eslint/no-floating-promises
100-
flushQueue();
101-
102-
// flushQueue should not throw
103-
// eslint-disable-next-line @typescript-eslint/no-floating-promises
104-
flushQueue();
85+
captureException(error, {
86+
mechanism: {
87+
handled: false,
88+
},
89+
});
90+
}
91+
},
92+
() => {
93+
span?.end();
10594

106-
throw error;
107-
}
95+
// flushQueue should not throw
96+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
97+
flushQueue();
98+
},
99+
);
108100
},
109101
);
110-
111-
// flushQueue should not throw
112-
// eslint-disable-next-line @typescript-eslint/no-floating-promises
113-
flushQueue();
114-
115-
return res;
116102
});
117103
},
118104
});

0 commit comments

Comments
 (0)