News for hackers. Stuff that matters.
Re:Error Handling in Nodejs by bsimpson on Friday July 17, 2015 @05:50AM (#9901945)
The biggest mistake in JS Harmony is silently swallowing async errors by default unless you add a .catch to a Promise chain.
Re:Error Handling in Nodejs by qudat on Friday July 17, 2015 @10:11AM (#9902774)
Agreed! I literally just dealt with this eariler in the work week, spent hours trying to figure out why an error wasn't properly being thrown in my unit tests ... forgot to catch the promise chain -.-

This applies to functions that are async as well in babel:

    async function get(url) {
        throw new Error('hi there');
    }

    // wont throw the error
    get('http://google.com');

    // catch the error
    get('http://google.com').catch(function(err) {
        throw err;
    });
Re:Error Handling in Nodejs by inglor on Friday July 17, 2015 @08:55AM (#9902464)
Not if I can help it :) In io.js this is no longer an issue, you have a process event you can catch on "silent errors" to make them loud. This lands in NodeJS soon. Bluebird promises don't behave this way and have never done so.
Re:Error Handling in Nodejs by elliotf on Friday July 17, 2015 @12:12PM (#9903420)
Maybe there's a nuance between "possibly unhandled" and "unhandled" ?

I was bitten by bluebird quietly eating exceptions on at least one codebase. I fixed it by re-throwing them via: https://github.com/petkaantonov/bluebird/blob/master/API.md#...

edit: bluebird also exposes a process-level event, now, too!

Re:Error Handling in Nodejs by grandalf on Friday July 17, 2015 @11:03AM (#9903058)
great! BTW would you use bluebird or es6 promises if starting a new project today?
Re:Error Handling in Nodejs by phpnode on Friday July 17, 2015 @01:58PM (#9904130)
inglor is a contributor to Bluebird, so I'd guess he'd say Bluebird. Bluebird is the much faster option and has a lot more features, if you're using babel (and a recent node/io.js) you can transpile async/await to bluebird coroutines for maximum performance and syntactic beauty.
Re:Error Handling in Nodejs by grandalf on Friday July 17, 2015 @02:11PM (#9904236)
Is there any chance Bluebird will become the standard in the future? Not sure if it makes sense to use a nonstandard approach, but would love to be convinced otherwise.
Re:Error Handling in Nodejs by phs2501 on Friday July 17, 2015 @02:49PM (#9904508)
IMHO, very doubtful; Bluebird has a lot of (very useful) features that would be pretty burdensome to put into a specification.

On the other hand, there's really no reason not to use Bluebird. It's easy to ensure that a promise you receive is a Bluebird promise:

    import Bluebird from 'bluebird';
    const bluebirdPromise = Bluebird.resolve(promise);
If `promise` is a Bluebird promise that will be a no-op, and if it's a foreign promise or thennable you'll get a new Bluebird promise that will resolve/reject when the foreign one does. (This is standard behavior; see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...)

Since Bluebird promises are thennables, the reverse will be true if they're consumed by a different promise library.

Given Bluebird's speed, superior debuggability, and features, there's really no reason not to use it a this point.

Re:Error Handling in Nodejs by grandalf on Friday July 17, 2015 @06:43PM (#9905784)
thanks much for the explanation. I will try bluebird.
Re:Error Handling in Nodejs by esamatti on Friday July 17, 2015 @06:53AM (#9902095)
Chrome at least logs the error to console

http://jsfiddle.net/sb1zqjdm/ (using native promises)

And it seems that the "Pause on exceptions" debugger feature works on unhandled promise errors. Doesn't seem too bad to me.

Re:Error Handling in Nodejs by bsimpson on Friday July 17, 2015 @07:05AM (#9902131)
This is a recent addition due to complaints like mine. Hopefully it becomes standard, because it's the way it should have always been.
Re:Error Handling in Nodejs by inglor on Friday July 17, 2015 @08:59AM (#9902484)
Well - the moral is to complain more, when I wrote and advocated for detection in io.js I needed people on my side - Domenic needs people to complain and help now for the browser spec - we're always a hand short :)
Re:Error Handling in Nodejs by esamatti on Friday July 17, 2015 @07:26AM (#9902200)
Wonder whether there is anything to add to the standard. Isn't this just a devtools issue?
Re:Error Handling in Nodejs by BonsaiDen on Friday July 17, 2015 @08:34AM (#9902387)
The standard should simply be changed to throw by default, but I guess the train has already left the station and so we're left with "bugs by design" before the spec has even been fully implemented by all vendors :(

But then again, in it's current "minimal" state of design, the Harmony Promises are inferior to any of the dedicated Promise A Plus implementations out there (e.g. Bluebird) and at least under Node there is little to no reason to use the built-in Promise object at this point in time.

Re:Error Handling in Nodejs by inglor on Friday July 17, 2015 @09:18AM (#9902554)
Re:Error Handling in Nodejs by prawnsalad on Friday July 17, 2015 @06:49AM (#9902087)
This has been my biggest pain point with Promises, I have spent hours trying to debug an issue only to find out I hadn't used an error handler in a Promise somewhere and an error was being thrown. It is so frustrating.

I know some Promise libraries have an option to report unhandled errors which makes life a lot easier but there really should be something natively too.

Re:Error Handling in Nodejs by inglor on Friday July 17, 2015 @08:56AM (#9902471)
process.on('unhandledRejection', function(promise,reason) {` since io.js 1.3
Re:Error Handling in Nodejs by bsimpson on Friday July 17, 2015 @09:42AM (#9902646)
I haven't looked at io.js in a short while. Does it log to console.error by default (like Chrome now does)?

This mistake is going to be most frustrating for those who don't realize that "unhandledRejection" is a case they even need to consider. People learning JS and having nothing happen when they run (erroneous) code are going to be pulling their hair out.

Re:Error Handling in Nodejs by inglor on Friday July 17, 2015 @10:09AM (#9902769)
This is currently being discussed in io.js (and remember it's being backported to the main NodeJS branch). Feel free to come participate in the discussion.
Re:Error Handling in Nodejs by linkmotif on Friday July 17, 2015 @01:07PM (#9903728)
FWIW, I think JS swallows all async errors regardless of whether you use .catch(). If the error is thrown async, you can't catch it. Right?
Re:Error Handling in Nodejs by cwp on Friday July 17, 2015 @01:29PM (#9903855)
No. This is the whole point of promises.
Re:Error Handling in Nodejs by dmethvin on Friday July 17, 2015 @02:26PM (#9904345)
Perhaps the gp meant this case?

   something().then(function(){  
      setTimeout(function(){  
        whoops_not_a_function();  
      }, 1000);  
   }).catch(function(){  
      // nope, nothing to catch  
   });
Re:Error Handling in Nodejs by phs2501 on Friday July 17, 2015 @03:33PM (#9904778)
No built-in help with native promises, but with something like Bluebird you'd prefer:

   something().then(() => {  
      return Promise.delay(1000).then(() => {  
        whoops_not_a_function();  
      });  
   }).catch(() => {  
      // whoops_not_a_function is not defined  
   });
...or write Promise_delay yourself on top of native promises (but really just use Bluebird).
Re:Error Handling in Nodejs by linkmotif on Saturday July 18, 2015 @02:02PM (#9908300)
Yup this is what I meant. Thanks. Yeah arrow functions help.