I have this generic function I have for a library:
const sendResponse = (res: Response, errorTrace: InspectedError) => {
if (res.headersSent) {
log.warning('Headers already sent for response. Error:', errorTrace);
}
try {
res.status(500); // this one
} catch (err) {
log.error(err);
}
try {
res.json({ // this one
foo: 'bar'
});
} catch (err) {
log.error(err);
}
};
I am curious what kinds of errors might be raised by res.status() and res.json() - is the best bet to look at the source or is there a better way to find out?
Some situations I can think of:
res
is invalid and either isn't an object or doesn't have a status
or json
method.res.json()
can fail if you give it an invalid parameter or you give it an object that contains circular references (that can't be represented in JSON).