Skip to content

Commit 95e7680

Browse files
committed
rethrow error after flushing events
1 parent 3ed8952 commit 95e7680

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

packages/nextjs/src/utils/withSentry.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ export const withSentry = (origHandler: NextApiHandler): WrappedNextApiHandler =
9191
});
9292
captureException(e);
9393
}
94-
throw e;
9594
(res as AugmentedNextApiResponse).__sentryCapturedError = e;
95+
res.end();
9696
}
9797
});
9898

@@ -121,16 +121,21 @@ function wrapEndMethod(origEnd: ResponseEndMethod): WrappedResponseEndMethod {
121121
await transactionFinished;
122122
}
123123

124-
// flush the event queue to ensure that events get sent to Sentry before the response is finished and the lambda
125-
// ends
124+
// Flush the event queue to ensure that events get sent to Sentry before the response is finished and the lambda
125+
// ends. If there was an error, rethrow it so that the normal exception-handling mechanisms can apply.
126126
try {
127127
logger.log('Flushing events...');
128128
await flush(2000);
129129
logger.log('Done flushing events');
130130
} catch (e) {
131131
logger.log(`Error while flushing events:\n${e}`);
132+
} finally {
133+
if (capturedError) {
134+
console.log('about to rethrow error');
135+
throw capturedError;
136+
}
137+
console.log('about to call origEnd');
138+
return origEnd.call(this, ...args);
132139
}
133-
134-
return origEnd.call(this, ...args);
135140
};
136141
}

0 commit comments

Comments
 (0)