Suppose I have an api.js:
const {somefunction} = require('../controllers/some-controller');
app.get('/data/', somefunction);
some-controller.js:
exports.somefunction = async (req, res,next) => {
const someVariable = req.params.variable
try {
console.log('status',res.statusCode) **//status code is: 200**
const Details = await collectionName.find({}).exec()
res.send(Details)
} catch {
console.log('status',res.statusCode) **//Here also,status code is: 200**
next(ApiError.dbError('Internal Server Error')); //middleware for error handling gives output
//as ApiError { code: 500, message:
//'Internal Server Error' }
return;
}
};
Say I wrote some wrong variable name in res.send(Detaaal) and it goes to catch block Here even in catch block status code is 200.
I'm trying to understand at what condition is status code different. Why is status code response on fail inside catch block didn't give me 500 status code.
ApiError.dbErrorreturn?