Code Monkey home page Code Monkey logo

Comments (7)

fabdrol avatar fabdrol commented on August 19, 2024

I am getting the same error, it seems to have to do with the parser, at line 43. There's a try..catch structure there, designed to emit an error when the parse failes...

Apparently, sometimes data isnt't formatted properly, which causes the parse to fail. Does anybody know if there's a more solid solution to this? It's something I've come across a lot in Javascript..

Parser.prototype.receive = function receive(buffer) {
  this.lastTime = new Date().getTime();
  this.buffer += buffer.toString('utf8');
  var index, json;

  // We have END?
  while ((index = this.buffer.indexOf(Parser.END)) > -1) {
    json = this.buffer.slice(0, index);
    this.buffer = this.buffer.slice(index + Parser.END_LENGTH);
    if (json.length > 0) {
      try {
        json = JSON.parse(json);
        this.emit('_data', json);
      } catch (error) {
        this.emit('error', json);
      }
    }
  }
};

from ntwitter.

nicola avatar nicola commented on August 19, 2024

Same for me but I get

Error: Uncaught, unspecified 'error' event.
at EventEmitter.emit (events.js:50:15)
at ClientRequest. (/Users/io/Proj/feetshot/node_modules/ntwitter/lib/twitter.js:251:14)
at ClientRequest.emit (events.js:67:17)
at HTTPParser.onIncoming (http.js:1261:11)
at HTTPParser.onHeadersComplete (http.js:102:31)
at CleartextStream.ondata (http.js:1150:24)
at CleartextStream._push (tls.js:375:27)
at SecurePair.cycle (tls.js:734:20)
at EncryptedStream.write (tls.js:130:13)
at Socket.ondata (stream.js:38:26)

from ntwitter.

timsavery avatar timsavery commented on August 19, 2024

+1...have been seeing this as well.

from ntwitter.

SujayKrishna avatar SujayKrishna commented on August 19, 2024

@nicolagreco I got the same error message.
I tried

stream.on('error', function(error){
    console.log(arguments);
  });

and i got { '0': 'http', '1': 406 }
406 is for Not Acceptable.
Wondering why i'm getting this error.
My Setup : Node 0.6.11 , ntwitter 0.2.8 on Windows 7.

from ntwitter.

CrabBot avatar CrabBot commented on August 19, 2024

This is caused whenever you have an uncaught exception in your stream.on('data') callback. Humorously, that's why it appears so random. lol.

At any rate, here is the associated code:

      try {
        json = JSON.parse(json);
        this.emit('_data', json);
      } catch (error) {
        this.emit('error', json);
      }

The trycatch is no doubt meant to catch only the JSON.parse error, but do to the sychronous nature of EventEmitter.emit, it actually catching any error's in the user's stream.on('data') callback as well. You could handle this one of two ways,

by not emitting synchronously, which leaves most of the code the same:

      try {
        json = JSON.parse(json);
        process.nextTick(this.emit.bind(this, '_data', json));
      } catch (error) {
        this.emit('error', json);
      }

or by only catching the parse error and additionally, returning the actual error, or a new type of error:

      try { json = JSON.parse(json); } catch (error) {
        this.emit('error', new Error('Invalid JSON returned - '+error.message);
        // You should always throw error objects, not strings,
        // but alternatively as it was before...
        // this.emit('error', json);
      }
      this.emit('_data', json);     

from ntwitter.

fent avatar fent commented on August 19, 2024

Nice find @CrabBot. I had actually found this a while ago but I did not realize it was related to this issue.

The solution I went with was the first one, process.nextTick().

from ntwitter.

CrabBot avatar CrabBot commented on August 19, 2024

I submitted a pull request to resolve this. Since the try/catch is only there to catch the JSON.parse(), I prefer to limit it to the line in general to avoid situations like this. We don't really need process.nextTick() since we've already asynced since the initial call.

Additionally, since it causes problems for users/libraries/stack traces/situations like this when you don't, I throw a real error.

#65

Cheers!

from ntwitter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.