how to handle uncaught exception in http.get response

I have this code to pull down a news feed from a third party website using an API. Its setup to run every 5 seconds, and pull any news transactions that may occur. The problem seems to be when there is no new transactions that occur.

By adding the process.on('uncaught exception', function(error){ console.log("hmph") }) the cron job is able to continue 5 seconds later, so I was tempted to leave it as is; however, I added console.log("hmph") to the and now I'm confused.

The first time, the console will write hmph. 5 seconds later it will write hmph hmph

and so on. I know I must be missing something, but I'm not quite sure what it is. I've tried in the else statement to do request.end() but the error still fires.

Without the process.on('uncaught...') the error thrown is:

events.js:71 throw arguments[1]; // Unhandled 'error' event ^ Error: Parse Error at Socket.socketOnData (http.js:1367:20) at TCP.onread (net.js:403:27)

with the proccess.on('uncaught...') the console.log(error) is:

{ [Error: Parse Error] bytesParsed: 161, code: 'HPE_INVALID_CONSTANT' }

How do I properly handle this error?

Abbreviated code:

var job = new cronJob('*/5 * * * * *', function(){
  var request = http.get({
                            host: 'www.example.com',
                            path: '/news_feed?apicode=myapicode',
                            port: 80,
                            headers: { 'accept-encoding': 'gzip' } 
                         })

  request.on('response', function(response){
    if (response.statusCode == 200){
      // gunzip response, save response to mongodb
    } 
    else
    {
      // here is where the error is occuring
      process.on('uncaughtException',function(error){
        console.log(error);
        console.log("hmph");
    }
  });
}, null, true);

Every time you make a request, you are binding a new uncaughtException handler, so when the first request is sent, you bind the first one, and when it fails it prints an error, and then at the next request, you add another handler, and when that fails both the first and second handlers will run.

Examining the error, and a conversation about this type of error here: https://github.com/joyent/node/issues/3354 It seems like the server you are connecting to is probably doing something weird. The easiest solution for you probably is to use the uncaughtException handler for now. That said, it is less than ideal and you should not do it as a general solution to future problems like this.

var job = new cronJob('*/5 * * * * *', function(){
  var request = http.get({
    host: 'www.example.com',
    path: '/news_feed?apicode=myapicode',
    port: 80,
    headers: { 'accept-encoding': 'gzip' } 
  });
  request.on('response', function(response){
    if (response.statusCode == 200){
      // gunzip response, save response to mongodb
    }
  });
}, null, true);

process.on('uncaughtException',function(error){
  console.log(error);
  console.log("hmph");
});
链接地址: http://www.djcxy.com/p/46456.html

上一篇: 向没有响应的服务器发出http.request时发生node.js错误

下一篇: 如何在http.get响应中处理未捕获的异常