簡體   English   中英

你如何抓住`錯誤:ENOENT:winston沒有這樣的文件或目錄?

[英]How do you catch `Error: ENOENT: no such file or directory` with winston?

我正在使用快遞與winston記錄器。 我看到一個錯誤Error: ENOENT: no such file or directory當表示嘗試提供路徑不准確的文件時, Error: ENOENT: no such file or directory 這是預期的行為。 但是,錯誤不會通過winston記錄(因此不會被推到我的松弛webhook)。 它會被記錄到節點控制台,但我的winston記錄器不會將其記錄下來。 我嘗試在try / catch中包裝.sendFile() ,但這也不起作用。

const logger = require('winston');

const serveFile = ({ filePath, fileType }, res) => {
  logger.verbose(`serving file: ${filePath}`);
  const sendFileOptions = {
    headers: {
      'X-Content-Type-Options': 'nosniff',
      'Content-Type'          : fileType,
    },
  };
  try {
    res.status(200).sendFile(filePath, sendFileOptions);
  } catch (error) {
    logger.error(error);
  }
};

module.exports = serveFile;

我需要使用sendFile()作為其第三個參數的回調函數,而不是try / catch。

const logger = require('winston');

const serveFile = ({ filePath, fileType }, res) => {
  logger.verbose(`serving file: ${filePath}`);
  const sendFileOptions = {
    headers: {
      'X-Content-Type-Options': 'nosniff',
      'Content-Type'          : fileType,
    },
  };
  res.status(200).sendFile(filePath, sendFileOptions, (error) => {
    if (error) {
      logger.error(error);
    }
  });
};

module.exports = serveFile;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM