Prevent sending response headers if already sent in default error han…#2006
Prevent sending response headers if already sent in default error han…#2006seratch merged 2 commits intoslackapi:mainfrom suhailgupta03:fix/error-handler-check-headers-sent
Conversation
…dler 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.
|
Thanks for the contribution! Before we can merge this, we need @suhailgupta03 to sign the Salesforce Inc. Contributor License Agreement. |
Codecov ReportAttention:
Additional details and impacted files@@ Coverage Diff @@
## main #2006 +/- ##
==========================================
- Coverage 82.21% 82.00% -0.22%
==========================================
Files 18 18
Lines 1524 1528 +4
Branches 438 439 +1
==========================================
Hits 1253 1253
- Misses 175 178 +3
- Partials 96 97 +1 ☔ View full report in Codecov by Sentry. |
seratch
left a comment
There was a problem hiding this comment.
This is a good catch! Thanks for sending this PR. Could you amend the logging part a little?
src/receivers/HTTPModuleFunctions.ts
Outdated
|
|
||
| // Check if the response headers have already been sent | ||
| if (response.headersSent) { | ||
| logger.error('Headers already sent, cannot send another response'); |
There was a problem hiding this comment.
Can you adjust the logging for consistency with the following logic?
| logger.error('Headers already sent, cannot send another response'); | |
| logger.error('An unhandled error occurred after ack() called in a listener'); | |
| logger.debug(`Error details: ${error}, storedResponse: ${storedResponse}`); |
|
Also, perhap adding unit tests for this pattern may not be an easy task. It's okay to skip the codecov error this time |
seratch
left a comment
There was a problem hiding this comment.
Thank you! Looks good to me 👍
Summary
This pull request introduces a safeguard in the
defaultProcessEventErrorHandlerto prevent attempts to send HTTP response headers after they have already been transmitted. This situation can occur whenack()is invoked before an error is thrown during event processing, leading to potential conflicts in the response handling.The goal of this PR is to enhance the robustness of the error handling mechanism by ensuring that the server does not encounter
ERR_HTTP_HEADERS_SENTerrors, which can disrupt the normal flow of Slack event processing. By checking theresponse.headersSentflag before setting headers or ending the response, we maintain the integrity of the HTTP transaction and provide a more reliable experience for developers using Bolt.js.Example Scenario
Consider a custom event handler that acknowledges an event and then performs an asynchronous operation which fails:
Requirements (place an
xin each[ ])