简体   繁体   中英

Meteor HTTP.get error handling

I am trying to perform HTTP.get on a set of URLs asynchronously using futures.

Meteor version: 0.8.3 Platform: 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?

Update:

  1. I am using futures because the final operation can only be performed after getting all the URLs.

  2. Interesting thing, I am able to open the URL via browser, and even POSTMAN and getting 200 status. But meteor get is receiving 404.

Per your error message, you're getting a 404 error code; some of your URLs are invalid. And you say yourself that it only happens on certain URLs.

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?

_.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;
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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