Skip to content

Commit f3ab44b

Browse files
authored
feat(nextjs): Add mechanism to error-logger-caught errors (#4061)
Add context data for errors caught by wrapping the nextjs server's error logger. Note: This method of capturing errors only works off of vercel.
1 parent d51855e commit f3ab44b

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

packages/nextjs/src/utils/instrumentServer.ts

+28-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
import { captureException, deepReadDirSync, getCurrentHub, Handlers, startTransaction } from '@sentry/node';
1+
import {
2+
captureException,
3+
configureScope,
4+
deepReadDirSync,
5+
getCurrentHub,
6+
Handlers,
7+
startTransaction,
8+
} from '@sentry/node';
29
import { extractTraceparentData, getActiveTransaction, hasTracingEnabled } from '@sentry/tracing';
3-
import { fill, isString, logger, stripUrlQueryAndFragment } from '@sentry/utils';
10+
import { addExceptionMechanism, fill, isString, logger, stripUrlQueryAndFragment } from '@sentry/utils';
411
import * as domain from 'domain';
512
import * as http from 'http';
613
import { default as createNextServer } from 'next';
@@ -145,8 +152,26 @@ function makeWrappedHandlerGetter(origHandlerGetter: HandlerGetter): WrappedHand
145152
*/
146153
function makeWrappedErrorLogger(origErrorLogger: ErrorLogger): WrappedErrorLogger {
147154
return function(this: Server, err: Error): void {
148-
// TODO add context data here
155+
// TODO add more context data here
156+
157+
// We can use `configureScope` rather than `withScope` here because we're using domains to ensure that each request
158+
// gets its own scope. (`configureScope` has the advantage of not creating a clone of the current scope before
159+
// modifying it, which in this case is unnecessary.)
160+
configureScope(scope => {
161+
scope.addEventProcessor(event => {
162+
addExceptionMechanism(event, {
163+
type: 'instrument',
164+
handled: true,
165+
data: {
166+
function: 'logError',
167+
},
168+
});
169+
return event;
170+
});
171+
});
172+
149173
captureException(err);
174+
150175
return origErrorLogger.call(this, err);
151176
};
152177
}

0 commit comments

Comments
 (0)