简体   繁体   English

使用Future使用异步调用正确处理流星错误

[英]Proper Meteor error handling with async calls using Future

I am wondering how to properly handle errors with Meteor when using async methods. 我想知道使用异步方法时如何正确处理Meteor的错误。 I have tried the following, but the error is being returned in the result parameter on the client callback instead of the error parameter. 我尝试了以下操作,但是错误是在客户端回调的result参数而不是error参数中返回的。

Server code: 服务器代码:

Future = Npm.require('fibers/future');

Meteor.methods({
    'myServerMethod': function(){
        var future = new Future();

        // URL to some remote API
        var url = UrlOfTheApiIWantToCall;

        HTTP.get(url, {//other params as a hash},
            function (error, result) {
                if (!error) {
                    future.return(result);
                } else {
                    future.return(error);
                }
            }
        );

        return future.wait();
    }
});

Client code: 客户代码:

Meteor.call('myServerMethod', function (error, result) {
    if(error){
        console.warn(error);
    }

    console.log('result', result);
});

As I was saying above, 'error' is always undefined on the client side event when the HTTP.get() on the server side returned an error. 就像我在上面说的那样,当服务器端的HTTP.get()返回错误时,在客户端事件中始终未定义“错误”。 I also tried replacing future.return(error); 我也尝试替换future.return(error); with future.throw(error); future.throw(error); on the server side, but this really throws an error on the server side. 在服务器端,但这确实会在服务器端引发错误。 The client side error parameter then gets a 500 Server Error, although the error thrown on the server was a 401 Unauthorized error. 尽管服务器上引发的错误是“ 401未经授权”错误,但是客户端错误参数然后得到“ 500服务器错误”。

So, is it possible to use Fiber's Future properly so that the client callback receives the same error parameter as the server callback? 因此,是否可以正确使用Fiber's Future,以便客户端回调收到与服务器回调相同的错误参数?

According to the Meteor.Error docs at http://docs.meteor.com/#/full/meteor_error 根据http://docs.meteor.com/#/full/meteor_error上的Meteor.Error文档

Methods can throw any kind of exception. 方法可以引发任何类型的异常。 But Meteor.Error is the only kind of error that a server will send to the client. 但是Meteor.Error是服务器将发送到客户端的唯一错误类型。 If a method function throws a different exception, then it will be mapped to a sanitized version on the wire. 如果一个方法函数抛出一个不同的异常,那么它将被映射到网络上的一个净化版本。 Specifically, if the sanitizedError field on the thrown error is set to a Meteor.Error, then that error will be sent to the client. 具体来说,如果抛出的错误上的sanitizedError字段设置为Meteor.Error,则该错误将发送到客户端。 Otherwise, if no sanitized version is available, the client gets Meteor.Error(500, 'Internal server error'). 否则,如果没有可用的经过清理的版本,则客户端将获取Meteor.Error(500,'内部服务器错误')。

Which is why you are receiving the 500 Server Error on the client. 这就是为什么您在客户端上收到500 Server Error的原因。 If you want to preserve the error message and have it be sent to the client, you can do something like this: 如果要保留错误消息并将其发送给客户端,则可以执行以下操作:

Future = Npm.require('fibers/future');

Meteor.methods({
    'myServerMethod': function(){
        var future = new Future();

        // URL to some remote API
        var url = UrlOfTheApiIWantToCall;

        HTTP.get(url, {//other params as a hash},
            function (error, result) {
                if (!error) {
                    future.return(result);
                } else {
                    future.throw(error);
                }
            }
        );

        try {
            return future.wait();
        }
        catch(err) {
            // Replace this with whatever you want sent to the client.
            throw new Meteor.Error("http-error", err);
        }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM