From 6f93a4a8c5accb4ecb2e66e1cd19ef4e586a8a4a Mon Sep 17 00:00:00 2001 From: Jannik Schönartz Date: Mon, 31 Aug 2020 17:46:33 +0000 Subject: [ipxe builder] Add try-catch when reading the builds directory --- server/api/ipxe.js | 12 ++++++++---- server/lib/shell.js | 16 ++++++++++------ 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/server/api/ipxe.js b/server/api/ipxe.js index e232cda..0b6acc3 100644 --- a/server/api/ipxe.js +++ b/server/api/ipxe.js @@ -195,11 +195,15 @@ router.get('/status', (req, res) => { router.get('/builds', (req, res) => { // Reads directory of the builded ipxe targets /ipxe/builds let recursiveDirectory = shell.readdirRecursive(buildsPath) - let linkname = fs.readlinkSync(buildLinkPath).split('/').slice(-1)[0] - for (let buildDir of recursiveDirectory) { - buildDir.selected = (buildDir.name === linkname) + try { + let linkname = fs.readlinkSync(buildLinkPath).split('/').slice(-1)[0] + for (let buildDir of recursiveDirectory) { + buildDir.selected = (buildDir.name === linkname) + } + res.send(recursiveDirectory) + } catch (error) { + res.send([]) } - res.send(recursiveDirectory) }) /* diff --git a/server/lib/shell.js b/server/lib/shell.js index 29986de..4f315e4 100644 --- a/server/lib/shell.js +++ b/server/lib/shell.js @@ -122,13 +122,17 @@ module.exports = { readdirRecursive: function (buildsPath) { let structure = [] - const directory = fs.readdirSync(buildsPath) - for (let obj of directory) { - if (!fs.lstatSync(path.join(buildsPath, obj)).isDirectory()) { - structure.push({ name: obj, type: 'file' }) - } else { - structure.push({ name: obj, type: 'directory', children: this.readdirRecursive(path.join(buildsPath, obj)) }) + try { + const directory = fs.readdirSync(buildsPath) + for (let obj of directory) { + if (!fs.lstatSync(path.join(buildsPath, obj)).isDirectory()) { + structure.push({ name: obj, type: 'file' }) + } else { + structure.push({ name: obj, type: 'directory', children: this.readdirRecursive(path.join(buildsPath, obj)) }) + } } + } catch (error) { + return [] } return structure }, -- cgit v1.2.3-55-g7522