Skip to content

Commit 91e0d1e

Browse files
committed
rethrow error after flushing events
1 parent b988544 commit 91e0d1e

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

packages/nextjs/src/utils/withSentry.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ export const withSentry = (origHandler: NextApiHandler): WrappedNextApiHandler =
9090
});
9191
captureException(e);
9292
}
93-
throw e;
9493
(res as AugmentedNextApiResponse).__sentryCapturedError = e;
94+
res.end();
9595
}
9696
});
9797

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

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

0 commit comments

Comments
 (0)