Coder Social home page Coder Social logo

parser-error vs user-error about primus HOT 8 CLOSED

primus avatar primus commented on May 17, 2024
parser-error vs user-error

from primus.

Comments (8)

3rd-Eden avatar 3rd-Eden commented on May 17, 2024

I didn't even realise that it would capture user thrown errors. Not sure if can be seen as a bug or as a feature, as I can that this can be useful but also really annoying.

I'm just going to label this as a bug for now. The quickest fix would be to force parsing to be async using a process.nextTick/setimmediate/setTimeout

Note: We could use https://github.com/NobleJS/setImmediate for this.

from primus.

gjohnson avatar gjohnson commented on May 17, 2024

Yeah, emitting data on the next tick does the trick.

gjohnson@ace4052

from primus.

3rd-Eden avatar 3rd-Eden commented on May 17, 2024

Good to know that it's working, but not sure if that would be the optimal solution for this. I think it makes more sense to update the parsers https://github.com/3rd-Eden/primus/blob/master/parsers/json.js#L11 so they call the fn callback after the try / catch statement. This would also increase performance as V8 won't be de-optimising transformation handling.

from primus.

gjohnson avatar gjohnson commented on May 17, 2024

Ohhhhh I see now... Good call, I'll go ahead and fix them that way. I don't really see the need to make the fn call async in this case now though. This what your thinking?

exports.decoder = function decoder(data, fn) {
  try { data = JSON.parse(data); }
  catch (e) { return fn(e); }
  fn(undefined, data);
}

Though, I guess you could also drop fn from the catch statement too... Need to test it, unless you know off the top of your head.

exports.decoder = function decoder(data, fn) {
  var err = undefined;
  try { data = JSON.parse(data); }
  catch (e) { err = e; }
  fn(err || undefined, data);
};

from primus.

3rd-Eden avatar 3rd-Eden commented on May 17, 2024

Yes, your first example was exactly what I was thinking. The second example would also work and can be written cleaner as well:

exports.decoder = function decoder(data, fn) {
  var err; // err is always undef when not nothing is assigned to it.

  try { data = JSON.parse(data); }
  catch (e) { err = e; }

  fn(err, data);
};

from primus.

gjohnson avatar gjohnson commented on May 17, 2024

Ah with this fix, it shows an actual failed test when using binary-pack. When checking for \u2028 and \u2029 in https://github.com/3rd-Eden/primus/blob/master/spark.js#L206, currently it always assumes strings and uses packet.indexOf.

Want to just ignore anything that's not a string? Seems silly to cast the buffer right back to a string... Meh?

from primus.

3rd-Eden avatar 3rd-Eden commented on May 17, 2024

@gjohnson whoops, yes I think a simple typeof check would be sufficient to fix that.

from primus.

3rd-Eden avatar 3rd-Eden commented on May 17, 2024

Closing this. It was fixed in #43

from primus.

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.