1

The Kris Kowal's Q docs states that Q.onerror is invoked on unhandled exceptions.
I can't make it work:

var Q = require('q');
Q.longStackSupport = true;
var util = require('util');

Q.onerror=function(){
    console.log('Q.onerror::')
    console.log(util.inspect(arguments))
}

function get(){
    var def=Q.defer();
    def.resolve('resolved');    
    return def.promise;
}

get()
.then(function(val){
    console.log('ok:'+val)
    undefined._prop;  // i would expect this exception to be  
                      // forwarded to Q.onerror... but it doesn't
    console.log('not reachd')
});

outputs:

ok:resolved

I think I didn't understand well the use of Q.onerror I'd like to track unhandled exceptions (and possibly rejections too) with a nice stack trace

jfriend00
  • 683,504
  • 96
  • 985
  • 979
aleclofabbro
  • 1,635
  • 1
  • 18
  • 35

1 Answers1

1

Q does not track* unhandled rejections at the moment, so you have to explicitly tell it the chain has ended.

Q.onerror handles exceptions unhandled inside done clauses:

get()
.done(function(val){ // you can not chain this, this indicates the chain is done
    console.log('ok:'+val)
    undefined._prop;  // i would expect this exception to be  
                      // forwarded to Q.onerror... but it doesn't
    console.log('not reachd')
});

This is unlike libraries like Bluebird, that can figure out unhandled rejections on their own, or native promises in Firefox that use GC to detect unhandled rejections.

* (atm, an experimental feature was added to Q and then removed)

Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
  • ok, i see.. but what about the stack trace? the Q.error logs out ``TypeError: Cannot read property '_prop' of undefined`` but there's no stack trace, despite Q.longStackSupport = true; – aleclofabbro May 10 '14 at 19:42
  • Interesting, seems like Q's stack traces are not smart enough, everything else seems in order according to the docs and relevant code. In Bluebird, you get "From previous event: ... at get` – Benjamin Gruenbaum May 10 '14 at 19:49
  • wait, i found [this](http://stackoverflow.com/questions/15691366/using-promises-logging-stack-trace-in-fail-handler) and logging ``e.stack`` in ``Q.onerror`` works.. it didn't show up with ``util.inspect`` because it's a getter ! – aleclofabbro May 10 '14 at 19:51