捕获http.call超时异常

时间:2016-08-02 06:04:38

标签: meteor

此Meteor服务器代码使用http@1.2.8,在慢速互联网连接期间,它将以下内容打印到终端:

response = HTTP.call(method, url, {
  timeout: 30000,
  headers: header,
  content: content,
  followRedirects: true
});
  

异步函数回调中的异常:错误:ETIMEDOUT    at Object.Future.wait(/abc/xyz/.meteor/packages/meteor-tool/.1.4.0.i9pn2o++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64 /dev_bundle/server-lib/node_modules/fibers/future.js:449:15)

如何优雅地捕获此错误以便我可以通知客户端?感谢

1 个答案:

答案 0 :(得分:1)

只需使用try catch

即可
try{
    response = HTTP.call(method, url, {
        timeout: 30000,
        headers: header,
        content: content,
        followRedirects: true
    });
}
catch(e){
    if(e.code==="ETIMDOUT"){
        //alert the client
    }
}