【问题标题】:Meteor doesn't throw error but crashesMeteor 不会抛出错误但会崩溃
【发布时间】:2015-01-04 06:38:31
【问题描述】:

我想在我的应用程序中实现错误处理,但是当我抛出 Meteor.Error 时,我的服务器崩溃了。这可能是因为我正在使用未来来等待结果。我怎样才能让它运行?

Meteor.methods({
  '/app/pdf/download': function (url, name) {
    check(url, String);
    check(name, Match.Any);

    if ( ! name) {
      name = url.split('/').pop();
    } else {
      name += '.pdf';
    }

    var Future = Meteor.npmRequire('fibers/future');
    var Download = Meteor.npmRequire('download');

    var future = new Future();

    var download = new Download({ extract: true, strip: 1 })
      .get(url)
      .dest(process.env.PWD + '/staticFiles')
      .rename(name);

    // Run download
    download.run(function (err, files, stream) {
      if (err) {
        throw new Meteor.Error(500, 'Couldn\'t download file');
      }

      future.return(name);
    });

    return future.wait();
  }
});

【问题讨论】:

    标签: asynchronous meteor npm node-fibers


    【解决方案1】:

    是的,这是因为您将它扔到另一个调用堆栈中。

    你可以试试:

    var error;
    
    download.run(function (err, files, stream) {
      if (err) {
        error = err;
      }
    
      future.return(name);
    });
    
    var result = future.wait();
    
    if (error)
        throw new Meteor.Error(500, 'Couldn\'t download file');
    
    return result;
    

    无论哪种方式,我都建议您使用 Meteor.wrapAsync 来达到您的目的。

     var sync = Meteor.wrapAsync(function (done) {
        download.run(function (err, files, stream) {
          if (err) {
            done(new Meteor.Error(500, 'Couldn\'t download file'));
          }
          else {
            done(err, name);
          }
        });
     };
    
     return sync();
    

    如果你使用 Meteor Meteor._wrapAsync()。

    【讨论】:

      猜你喜欢
      • 2016-03-28
      • 2018-12-11
      • 2019-12-06
      • 2016-09-11
      • 2022-12-09
      • 2021-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多