node.js - NodeJs 无法捕获错误

标签 node.js error-handling

我使用模块omdb我在使用它时很难发现错误。该错误是由以下两种方法之一引起的,但堆栈跟踪未显示是哪一种。此外,没有一个 console.log 被触发,所以我假设我在尝试处理可能的错误时犯了一些错误。 omdb.poster 的描述是:

Returns a readable stream of the poster JPEG.

function downloadImage(show, callback) {
    var filename = show.title + + " " + show.year + ".jpg";
    var path = "./images/" + filename;
    omdb.poster(show)
        .pipe(fs.createWriteStream(path))
        .on('error', function(err) {
            console.log("Download Omdb image err: " + err + " for " + show.title + " " + show.year);
            callback(err, null);
        })
        .on('close', function() {
            callback(null, path);
        });

}

function getOmdbInfo(show, callback) {
    omdb.get(show, true, function(err, movie) {
        if(err) {
            console.log("err:" + show.title + " " + show.year);
            callback(err, null);
        }
        if(!movie) {
            console.log("No OMDB info results for: " + show.title + " " + show.year);
            callback('Movie not found!', null);
        } else {
            callback(null, movie);
        }
    });
}

错误:

\node_modules\needle\lib\needle.js:81
      throw new TypeError('URL must be a string, not ' + uri);
            ^
TypeError: URL must be a string, not null
    at Object.Needle.request (\node
_modules\omdb\node_modules\needle\lib\needle.js:81:13)
    at Object.exports.(anonymous function) [as get] (\node_modules\omdb\node_modules\needle\lib\needle.js:425:19)
    at \node_modules\omdb\index.js:251:26
    at \node_modules\omdb\index.js:198:13
    at done (\node_modules\omdb\node_modules\needle\lib\needle.js:234:9)
    at PassThrough.<anonymous> (\node_modules\needle\lib\needle.js:363:11)
    at PassThrough.emit (events.js:129:20)
    at _stream_readable.js:908:16
    at process._tickCallback (node.js:355:11)

最佳答案

出现问题是因为我在尝试通过管道传输文件后捕获了订单。 通过更改此:

omdb.poster(show)
        .pipe(fs.createWriteStream(path))
        .on('error', function(err) {
            console.log("Download Omdb image err: " + err + " for " + show.title + " " + show.year);
            callback(err, null);
        })
        .on('close', function() {
            callback(null, path);
        });

对此:

omdb.poster(show)
        .on('error', function(err) {
            console.log("Download OMDB image err: " + err + " for " + show.title + " " + show.year);
            callback(err, null);
        })
        .pipe(fs.createWriteStream(path))
        .on('close', function() {
            callback(null, path);
        });

错误已被捕获。

关于node.js - NodeJs 无法捕获错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33486445/

相关文章:

asp.net - IExceptionHandler不处理UnsupportedMediaTypeException

excel - 错误消息没有出现,尽管没有错误,并且在vba中的错误hadeler语句之前放置了退出子

linux - 在 Gentoo Linux 发行版上构建和安装 NodeJs

node.js - 禁用蓝图路线 sails js

node.js - 推送通知的实现

node.js - Angular2 QuickStart npm start 无法正常工作

node.js - 找不到名称 __values

angularjs - nginx错误页面在使用路由的 Angular 应用程序中不起作用

ruby-on-rails - 开始救援未捕获错误

c# - ASP.NET MVC 中的全局错误处理。可能的?