Skip to content

Commit

Permalink
Prevent sending response headers if already sent in default error han… (
Browse files Browse the repository at this point in the history
#2006)

* Prevent sending response headers if already sent in default error handler

This change addresses a bug where the defaultProcessEventErrorHandler
could attempt to send HTTP response headers after they've already been
sent, leading to a server error. The fix adds a check for the
response.headersSent property before attempting to write headers or
end the response.

The update ensures that the error handler behaves correctly even if
ack() has been called previously during request processing.

* Update log messages as per code review feedback
  • Loading branch information
suhailgupta03 authored Dec 1, 2023
1 parent 3142f9a commit 5587d01
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/receivers/HTTPModuleFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,14 @@ export class HTTPModuleFunctions {
args: ReceiverProcessEventErrorHandlerArgs,
): Promise<boolean> {
const { error, response, logger, storedResponse } = args;

// Check if the response headers have already been sent
if (response.headersSent) {
logger.error('An unhandled error occurred after ack() called in a listener');
logger.debug(`Error details: ${error}, storedResponse: ${storedResponse}`);
return false;
}

if ('code' in error) {
// CodedError has code: string
const errorCode = (error as CodedError).code;
Expand Down

0 comments on commit 5587d01

Please sign in to comment.