1

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);

1 Answer 1

3

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");
});
Sign up to request clarification or add additional context in comments.

6 Comments

I appreciate your answer; however, I've tried doing that already and I get: 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)
And you are sure you are binding in the same place I said? You don't put the error inside the response callback, like you had your uncaughtException handler
Yeah, I've checked, its in the same spot. Here is the full code w/your suggestion. pastebin.com/PTu7cA8s
Whoops in that code, line 28 the .on('error') was something that I was playing around with trying to test it in dif spots. Either way, same error still happens. I really do appreciate your help :)
Thank you for your help. Is there any way to clear the old error handlers out, so they don't just accumulate? I'm worried about there eventually being a numerous amount of handlers and what affect that may have?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.