简体   繁体   English

流星HTTP.get错误处理

[英]Meteor HTTP.get error handling

I am trying to perform HTTP.get on a set of URLs asynchronously using futures. 我正在尝试使用Future在一组URL上执行HTTP.get。

Meteor version: 0.8.3 Platform: Windows server 2012 流星版本: 0.8.3 平台: Windows Server 2012

The code is somewhat like this: 代码有点像这样:

var futures = _.map(test, function(url) {

 var future = new Future();
 var onComplete = future.resolver();

 try{
    // Make async http call
    var httpGet = HTTP.get(url,{followRedirects: true},function(error, result) {
        if(error)
        {
            apiLogger.error("%s is error",error);
            onComplete(error, null);
        }
        else
        {
            if(result!=null){
                //operations done here  
                onComplete(error, JSON.stringify(object1));                 
            }
            else {
                apiLogger.error('%s - User encountered an error. URL not parsed: %s',user,url);
                onComplete(error, null);
            }
        }
    });
 }
 catch(e)
 {
    apiLogger.error('%s - URsarsed: %s - %s',user,url,result.statusCode);
    onComplete(error, null);
 } 
 return future;
});

The issue I am facing is improper error handling. 我面临的问题是错误的错误处理。

I am getting the following error on some URLs: 在某些网址上出现以下错误:

I20140904-17:57:38.609(-4)? Exception while invoking method 'parallelAsyncJob' E
rror: failed [404] <html><head><title>Apache Tomcat/7.0.12 - Error report</title
><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color
:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;ba
ckground-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;
color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,A
rial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial
,sans-serif;color:white;background-colo...
I20140904-17:57:38.617(-4)?     at packages/underscore/underscore.js:255
I20140904-17:57:38.619(-4)?     at Array.map (native)
I20140904-17:57:38.621(-4)?     at Function._.map._.collect (packages/underscore
/underscore.js:123)
I20140904-17:57:38.623(-4)?     at Function._.invoke (packages/underscore/unders
core.js:254)
I20140904-17:57:38.626(-4)?     at Meteor.methods.parallelAsyncJob (app/server/m
ethods.js:1:9355)
I20140904-17:57:38.628(-4)?     at maybeAuditArgumentChecks (packages/livedata/l
ivedata_server.js:1488)
I20140904-17:57:38.631(-4)?     at packages/livedata/livedata_server.js:650
I20140904-17:57:38.632(-4)?     at _.extend.withValue (packages/meteor/dynamics_
nodejs.js:37)
I20140904-17:57:38.635(-4)?     at packages/livedata/livedata_server.js:649
I20140904-17:57:38.644(-4)?     at _.extend.withValue (packages/meteor/dynamics_
nodejs.js:37)
I20140904-17:57:38.646(-4)?     - - - - -
I20140904-17:57:38.648(-4)?     at makeErrorByStatus (packages/http/httpcall_com
mon.js:12)
I20140904-17:57:38.650(-4)?     at Request._callback (packages/http/httpcall_ser
ver.js:99)
I20140904-17:57:38.652(-4)?     at Request.self.callback (C:\Users\Administrator
\AppData\Local\.meteor\tools\edf8981bb6\lib\node_modules\request\request.js:122:
22)
I20140904-17:57:38.655(-4)?     at Request.EventEmitter.emit (events.js:98:17)
I20140904-17:57:38.657(-4)?     at Request.<anonymous> (C:\Users\Administrator\A
ppData\Local\.meteor\tools\edf8981bb6\lib\node_modules\request\request.js:888:14
)
I20140904-17:57:38.660(-4)?     at Request.EventEmitter.emit (events.js:117:20)
I20140904-17:57:38.662(-4)?     at IncomingMessage.<anonymous> (C:\Users\Adminis
trator\AppData\Local\.meteor\tools\edf8981bb6\lib\node_modules\request\request.j
s:839:12)
I20140904-17:57:38.665(-4)?     at IncomingMessage.EventEmitter.emit (events.js:
117:20)
I20140904-17:57:38.668(-4)?     at _stream_readable.js:920:16
I20140904-17:57:38.669(-4)?     at process._tickCallback (node.js:415:13)

Am I doing something wrong? 难道我做错了什么? Or is it some GET issue? 还是一些GET问题?

Update: 更新:

  1. I am using futures because the final operation can only be performed after getting all the URLs. 我使用期货是因为最终操作只能在获取所有URL后才能执行。

  2. Interesting thing, I am able to open the URL via browser, and even POSTMAN and getting 200 status. 有趣的是,我能够通过浏览器甚至POSTMAN打开URL并获得200状态。 But meteor get is receiving 404. 但是流星正在接收404。

Per your error message, you're getting a 404 error code; 根据您的错误消息,您将得到404错误代码; some of your URLs are invalid. 您的某些网址无效。 And you say yourself that it only happens on certain URLs. 您自己说它仅在某些URL上发生。

Why is your code so complicated, with all these futures? 在所有这些未来中,为什么您的代码如此复杂? HTTP.get() itself offers an asyncCallback , and you're already using inline callbacks in your code, so why not just strip out all the futures stuff? HTTP.get()本身提供了asyncCallback ,并且您已经在代码中使用了内联回调,所以为什么不删除所有期货呢?

_.map(test, function(url) {
  try {
    // Make async http call
    HTTP.get(url, {followRedirects: true}, function(error, result) {
      if (error) {
        if (result.statusCode == 404)
          apiLogger.error('Error 404, URL not found: %s', url);
        else
          apiLogger.error('Error %s from %s for user %s',
            result.statusCode, url, user);
        return false;
      } else {
        if (result != null) {
          // operations done here  
        } else {
          apiLogger.error('Empty or invalid result returned from %s for user %s',
            url, user);
        }
        return false;
      }
    });
  } catch (error) {
    return false;
  }
});

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

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