0

嗨,我刚刚下载了 ytdl-core 模块,但遇到了无法处理的 Promise Rejection!有人可以帮忙吗?

app.get("/getaudio", async (req, res) => {
  const videoID = req.query.v;
  const quality = req.query.q;
  try {
    ytdl("http://www.youtube.com/watch?v=" + videoID, {
      quality: quality,
      filter: "audioonly",
    }).pipe(res);
  } catch (e) {
    res.status(500).send("Encountered Error: " + e.message);
  }
});

这是我将整个内容包装在 try catch 块中的代码,但仍然无法处理 Promise Rejection 任何指针都值得赞赏。

如果这有帮助,这是堆栈跟踪:

(node:1752) UnhandledPromiseRejectionWarning: Error: No such format found: asdasd
    at Object.exports.chooseFormat (D:\Code and Other Things\YTAudioStream\node_modules\ytdl-core\lib\format-utils.js:168:11)
    at downloadFromInfoCallback (D:\Code and Other Things\YTAudioStream\node_modules\ytdl-core\lib\index.js:86:26)
    at D:\Code and Other Things\YTAudioStream\node_modules\ytdl-core\lib\index.js:20:5
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)

我知道我提供了一个无效的质量参数,这是故意的,我想在我的代码中处理这个拒绝

4

1 回答 1

1

ytdl返回一个可读的流,处理流错误有点棘手。你需要用来.on()检测错误,所以:

ytdl("http://www.youtube.com/watch?v=" + videoID, {
  quality: quality,
  filter: "audioonly",
}).on('error', (err) => console.log(err)).pipe(res);

但是如果你真的想使用 try-catch 那么我想你可以自己扔它:

ytdl("http://www.youtube.com/watch?v=" + videoID, {
  quality: quality,
  filter: "audioonly",
}).on('error', (err) => throw err).pipe(res);
于 2021-05-19T15:21:31.913 回答