Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot use res in unhandled errors if produced error with resp.status() #4148

Closed
DawnImpulse opened this issue Jan 3, 2020 · 6 comments
Closed

Comments

@DawnImpulse
Copy link

Issue:
I am using an unhandled error handler in express. Now I am facing an issue when I try to send a response (somewhere in the code) and give an undefined status code, it fails as it should and throw an error which is then intercepted by the unhandled error handler.

Everything is working the way it should except now the unhandled error handler cannot send a response back to user using res anymore. It is just skipping over it.

The issue only persist when using undefined status code in the res beforehand.

@fed135
Copy link

fed135 commented Jan 3, 2020

Hi there! Can you share a code sample and the error you are getting, please. This would help us troubleshoot the issue.

@DawnImpulse
Copy link
Author

how to produce the issue :

import express from "express";  
const app = express();  
  
app.get("/", (req, resp) => {  
  const code = undefined;  
  resp.status(code).send({ success: true });  
});  
  
app.use((err, req, resp, next) => {  
  console.log(err);  
  resp.status(500).send(err);  
});  
  
app.listen(8989);  
console.log("server started");

Since I am providing an undefined code to req.status it throws an error which is caught by the error handler but the line resp.status(500).send(err); is not executing at all & nothing is happening.
I have added console.log(err) to know what is happending & here is the error as follows

RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: undefined
    at ServerResponse.writeHead (_http_server.js:209:11)
    at ServerResponse._implicitHeader (_http_server.js:200:8)
    at write_ (_http_outgoing.js:585:9)
    at ServerResponse.end (_http_outgoing.js:702:5)
    at ServerResponse.send (C:\Users\Saksham\data\work\code\multi-code\node_modules\express\lib\response.js:221:10)
    at ServerResponse.json (C:\Users\Saksham\data\work\code\multi-code\node_modules\express\lib\response.js:267:15)
    at ServerResponse.send (C:\Users\Saksham\data\work\code\multi-code\node_modules\express\lib\response.js:158:21)
    at app.get (C:\Users\Saksham\data\work\code\multi-code\app\temp.ts:6:23)
    at Layer.handle [as handle_request] (C:\Users\Saksham\data\work\code\multi-code\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\Users\Saksham\data\work\code\multi-code\node_modules\express\lib\router\route.js:137:13)

Once again, even after this is logged the code on the next line should work and send a 500 error but nothing is happening & after few seconds I get , Could not get any response in POSTMAN (postman's own handler)

@wesleytodd
Copy link
Member

Related:

#2795
#2797
#3111
#3137
#3143

@dougwilson
Copy link
Contributor

We tried to fix this in 4.17.0, but had to revert in 4.17.1 due to it breaking existing apps.

@dougwilson
Copy link
Contributor

Ultimately, there is no guarantee you can send a response from the express error handler, as the response may no longer be in a good state. In your case, setting status to undefined causes the core node.js code to get into a bad state and node.js will no longer allow you to respond.

@DawnImpulse
Copy link
Author

Ohh okay. Thanks for your help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants