Skip to content

Commit

Permalink
[FIX] fileUtils: Properly handle exceptions from fs.stats
Browse files Browse the repository at this point in the history
Do not assume every exception is caused by a missing file
  • Loading branch information
RandomByte committed Dec 2, 2024
1 parent d5faadc commit d382253
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/fileUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@ module.exports = function(fs) {
function statFile(filePath) {
return new Promise(function(resolve, reject) {
fs.stat(filePath, function(err, stat) {
// No rejection here as it is ok if the file was not found
resolve(stat ? {path: filePath, stat: stat} : null);
if (err instanceof Error) {
if (err.code === "ENOENT") { // "File or directory does not exist"
return resolve(null);
} else {
return reject(err);
}
}
resolve(stat ? {path: filePath, stat} : null);
});
});
}
Expand Down

0 comments on commit d382253

Please sign in to comment.