function getDirFromPath(req) {
var path = req.header('path')
if (path == null) {
res.status(400).send('Path required')
}
path = 'files/' + path
try {
var dir = fs.opendirSync(path)
return dir;
} catch {
if (fs.accessSync(path) != undefined) {
console.log('Non-existing dir tried to be accessed.')
res.status(400).send('Invalid path')
}
res.status(500).send('Failed to open directory')
return -1;
}
}
I can then check for -1 in the caller of this function. This seems inelegant though. Should I be doing some kind of exception throwing out of this function?