Honestly, I'm just wondering if I'm doing this right. If I want custom error messages, do I have to wrap every single async function with a try & catch?
Any input on the following code would be extremely appreciated:
async function read(dir) {
let paths, content;
// read directory of paths to an array
try {
paths = await fs.readdir(dir);
} catch(err) {
throw 'Failed to read directory ' + dir;
}
// loop through paths reading file contents
for(const file of paths) {
try {
content = await fs.readFile(file, 'utf-8');
} catch(err) {
throw 'Failed to read contents of ' + file;
}
try {
// another async function that manipulates content
} catch(err)
throw 'Failed to do whatever';
}
}
return content;
}
// a function down here that calls read and handles thrown errors.