I have an expressjs server which send's files to its clients using the sendFile function. I use the function's callback to know when errors occur. Now it seems that whenever sendFile decides to respond with 304 (which is desired behaviour in my case), its callback is called with the following error:
Error: Request aborted: sendFile error
at Object._onImmediate (.../node_modules/express/lib/response.js:941:17)
at processImmediate [as _immediateCallback] (timers.js:336:15)
{ [Error: Request aborted: sendFile error] code: 'ECONNABORT' }
I can't find any documentation about this however, and I wonder how to determine when to ignore this error and when to react to it as it seems there could be other cases where such an error is a symptom of a problem, not just the side-effect of desired behaviour.
Is this indeed what sendFile is supposed to do? Is there any documentation on this? How can I differentiate this error from other ECONNABORT errors?
The response.statusCode can help detect this condition, though I don't know if this also catches other errors. Here's my modified code adapted from the docs
res.sendFile(filename, options, function (err) {
if (err) {
if (err.code === "ECONNABORT" && res.statusCode == 304) {
// No problem, 304 means client cache hit, so no data sent.
console.log('304 cache hit for ' + filename);
return;
}
console.error("SendFile error:", err, " (status: " + err.status + ")");
if (err.status) {
res.status(err.status).end();
}
}
else {
console.log('Sent:', filename);
}