0

我正在为我的应用程序使用 IBM 云存储桶存储。当我试图检索一个不存在的文件时,即使我处理了错误,应用程序仍然崩溃。这是我的代码。

导出函数:

export const getFileStream = (itemName) => {
    return cos.getObject({
        Bucket: bucketName,
        Key: itemName
    }).on('error',(err)=>{
        console.log("we've got an error");
        throw err;
    }).createReadStream();
};

在哪里使用

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 }); });
};

什么时候details.CV是伪造的项目 ID,然后我收到以下错误。

我的日志和错误显示在控制台中,但服务器仍然崩溃并需要重新启动。这是错误:

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
}
4

0 回答 0