I Have a route in my NESTJS Node API server to get files, however when there's no File specified on the path, I recieve the error :
Error: ENOENT: no such file or directory, open '(the path specified)'
After the error occurs my server stop working and I have to restart the process again with "nest start".
How do I catch this error, and return it to the request, instead of just breaking the API?
The code:
try {
const file = createReadStream(join(process.cwd(), filesrc.path));
res.set({
"Content-Type": `${filesrc.type}`,
"Content-Disposition": `attachment;filename="${filesrc.original_name}"`,
"Content-Length": +filesrc.size
})
res.status(200);
return new StreamableFile(file);
} catch (err) {
throw new HttpException('test', 500)
}
Note: the "filesrc" is the variable that holds some file information.