Home > Blockchain >  With ESLint, how would you require a `return` after a `res.[...].send({...})`?
With ESLint, how would you require a `return` after a `res.[...].send({...})`?

Time:11-12

We have a few controllers that were written in a way that allows the Error [ERR_HTTP_HEADERS_SENT] error to be sent.

For context this happens in code similar to... This is a very contrived example

try{
  if(someCheck) { res.status(400).send({someStatus}); }
  /* other code */
}catch(e){ res.status(500).send({error}); }

In the example above if the someCheck causes the 400 to be sent and then the "other code" triggers an exception we would receive the headers sent error as mentioned.

My question to help mitigate this... Is there an eslint rule or could a custom rule be created that would require either...

return res.status(400).send({someStatus});
/* or */
res.status(400).send({someStatus});
return;

I'd prefer the later, but really the goal is to warn/error if send() is called and doesn't have a correlated return statement.

CodePudding user response:

You can use the tool I'm working on:

  • Related