I'm using IBM cloud bucket storage for my app. When I'm trying to retrieve a file that doesn't exist the app still crashes even though I handle the error. This is my code.
Exported function:
export const getFileStream = (itemName) => {
return cos.getObject({
Bucket: bucketName,
Key: itemName
}).on('error',(err)=>{
console.log("we've got an error");
throw err;
}).createReadStream();
};
Where it's used
export const getCV = async (req, res) => {
if (!req.params.id) return res.status(404).json({ message: "Invalid profile id!" });
await UserModel.findById(req.params.id, '-password').then(async (details) => {
if (!details) return res.status(404).json({ message: 'User not found!' });
if (!details.CV) return res.status(404).json({ message: 'No CV found!' });
res.attachment('CV.pdf');
res.set("Content-Type", "application/pdf");
getFileStream(details.CV)
.on('error', (err) => {
throw err;
// return res.status(404).json({ message: err.message });
})
.pipe(res);
}).catch(err => { return res.status(404).json({ message: err.message }); });
};
When details.CV
is a bogus item id, then I get the error below.
My logs and the error are displayed in the console, but the server still crashes and requires a restart. This is the error :
Server running on port : 9000
Connected to DB successfully
we've got an error
we've got another error
/home/paul/Documents/ProiectIBM/proiect-ibm/Server/node_modules/ibm-cos-sdk/lib/request.js:31
throw err;
^
NoSuchKey: The specified key does not exist.
at Request.extractError (/home/paul/Documents/ProiectIBM/proiect-ibm/Server/node_modules/ibm-cos-sdk/lib/services/s3.js:585:35)
at Request.callListeners (/home/paul/Documents/ProiectIBM/proiect-ibm/Server/node_modules/ibm-cos-sdk/lib/sequential_executor.js:106:20)
at Request.emit (/home/paul/Documents/ProiectIBM/proiect-ibm/Server/node_modules/ibm-cos-sdk/lib/sequential_executor.js:78:10)
at Request.emit (/home/paul/Documents/ProiectIBM/proiect-ibm/Server/node_modules/ibm-cos-sdk/lib/request.js:683:14)
at Request.transition (/home/paul/Documents/ProiectIBM/proiect-ibm/Server/node_modules/ibm-cos-sdk/lib/request.js:22:10)
at AcceptorStateMachine.runTo (/home/paul/Documents/ProiectIBM/proiect-ibm/Server/node_modules/ibm-cos-sdk/lib/state_machine.js:14:12)
at /home/paul/Documents/ProiectIBM/proiect-ibm/Server/node_modules/ibm-cos-sdk/lib/state_machine.js:26:10
at Request.<anonymous> (/home/paul/Documents/ProiectIBM/proiect-ibm/Server/node_modules/ibm-cos-sdk/lib/request.js:38:9)
at Request.<anonymous> (/home/paul/Documents/ProiectIBM/proiect-ibm/Server/node_modules/ibm-cos-sdk/lib/request.js:685:12)
at Request.callListeners (/home/paul/Documents/ProiectIBM/proiect-ibm/Server/node_modules/ibm-cos-sdk/lib/sequential_executor.js:116:18)
at Request.emit (/home/paul/Documents/ProiectIBM/proiect-ibm/Server/node_modules/ibm-cos-sdk/lib/sequential_executor.js:78:10)
at Request.emit (/home/paul/Documents/ProiectIBM/proiect-ibm/Server/node_modules/ibm-cos-sdk/lib/request.js:683:14)
at Request.transition (/home/paul/Documents/ProiectIBM/proiect-ibm/Server/node_modules/ibm-cos-sdk/lib/request.js:22:10)
at AcceptorStateMachine.runTo (/home/paul/Documents/ProiectIBM/proiect-ibm/Server/node_modules/ibm-cos-sdk/lib/state_machine.js:14:12)
at /home/paul/Documents/ProiectIBM/proiect-ibm/Server/node_modules/ibm-cos-sdk/lib/state_machine.js:26:10
at Request.<anonymous> (/home/paul/Documents/ProiectIBM/proiect-ibm/Server/node_modules/ibm-cos-sdk/lib/request.js:38:9) {
code: 'NoSuchKey',
region: null,
time: 2021-05-10T12:37:47.764Z,
requestId: '1b2c2405-ea63-4a8f-b81c-1746a0214cde',
extendedRequestId: undefined,
cfId: undefined,
statusCode: 404,
retryable: false,
retryDelay: 96.14914686040396
}