Fixed error handling in createDirecctory (#311)

The error handling in `createDirecctory` had three major issues:

* Racy check for existence of a directory - the directory could've been
  created or deleted in the meantime.
* Attempt to handle race outside the reduce function wouldn't let it
  continue creating the remaining directories.
* All errors that were **not** `EEXISTS` or `ENOENT` were silently
  ignored, leading to lot of frustration and hair pulling, because the
  code continued executing and then failed at attempt to open a file.

These problems were fixed by moving `try` into the reduce closure,
ignoring `EEXISTS` only and rethrowing all other errors. The logic
modifying the error message for `ENOENT` was kept.
This commit is contained in:
Martin Habovštiak 2020-05-03 21:54:26 +02:00 committed by GitHub
parent a980d9c3fa
commit 99b3dbb798
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -230,23 +230,22 @@ connect.setSSOParams = (config) => {
};
connect.createDirectory = (dirname) => {
try {
const initDir = path.isAbsolute(dirname) ? path.sep : '';
dirname.split(path.sep).reduce((parentDir, childDir) => {
const curDir = path.resolve(parentDir, childDir);
if (!fs.existsSync(curDir)) {
fs.mkdirSync(curDir);
const initDir = path.isAbsolute(dirname) ? path.sep : '';
dirname.split(path.sep).reduce((parentDir, childDir) => {
const curDir = path.resolve(parentDir, childDir);
try {
fs.mkdirSync(curDir);
} catch (err) {
if (err.code !== 'EEXIST') {
if (err.code === 'ENOENT') {
throw new Error(`ENOENT: No such file or directory, mkdir '${dirname}'. Ensure that channel backup path separator is '${(platform === 'win32') ? '\\\\' : '/'}'`);
} else {
throw err;
}
}
return curDir;
}, initDir);
} catch (err) {
if (err.code === 'EEXIST') {
return dirname;
}
if (err.code === 'ENOENT') {
throw new Error(`ENOENT: No such file or directory, mkdir '${dirname}'. Ensure that channel backup path separator is '${(platform === 'win32') ? '\\\\' : '/'}'`);
}
}
return curDir;
}, initDir);
}
connect.readCookie = (cookieFile) => {