Just attempted to handle ENOENT
by using a naive approach like this:
try {
res.sendFile(path);
} catch (e) {
if (e.code === 'ENOENT') {
res.send('placeholder');
} else { throw e; }
}
Unfortunately, this method is ineffective!
I'm aware that the correct way is to utilize the error callback of sendFile
, but it's quite surprising and frustrating that exceptions, a fundamental language feature, are not working in this scenario.
It seems like express itself might be handling the errors. Perhaps they don't want errors to immediately shut down the server. It makes sense.
However, all I receive is this inadequate message:
Error: ENOENT: no such file or directory, stat '<file>'
at Error (native)
Far from ideal.