3

When doing debugging with Chrome, the debugger has some niceties for navigating the Call Stack of unhandled exceptions. I've started using Q promises and now unhandled exceptions are essentially converted to rejected promises. This is fine, except when it comes to debugging. Consider the example:

promise.done(do_work)

Any exceptions thrown in the do_work function will be caught by Q and then thrown as unhandled exceptions in the next tick. The problem with this is that the callstack in the Chrome debugger is boring and just represents the Q flush. It is true that the stack property of the exception is correct, but that's just a tooltip in the Chrome debugger and annoying to use.

I've tried things like:

promise.done(do_work, function(e){throw e;})

But those exceptions are just caught by Q again; and, anyway, it doesn't matter because by the time you get to the error handler the stack is already from the next tick. I've also tried playing with Q.onerror and others, but they have the same problem.

Is there a way to cause Q to truly ignore exceptions for some calls during development so the Chrome debugger can get a nice original call stack to work with before Q goes to the next process tick?

5
  • This is precisely what I don't like about Q - confusion between errors and rejections. Q fans will of course demur and tell you this is the greatest feature since sliced bread. As far as I'm aware, you have to go along with Q's proclivities by putting in place .fail() (alias .catch()) handler(s), which perform console.log() statements (or similar) to display error messages of your choice. Dec 23, 2013 at 22:31
  • .fail()/.catch() also don't solve this because by the time they execute you're on the next process tick and have lost the original context. I've found a workaround, though; which I'll post as an answer.
    – drarmstr
    Dec 24, 2013 at 14:00
  • I don't get your point about "next process tick". Surely the error that was thrown is passed, as a parameter, to the .error() handler, where it can be handled and/or rethrown. Dec 24, 2013 at 15:18
  • Yes, it is. However, we are no longer in the execution context of that error. So, while we can look at the stack property of the error to see what the stack was when the error occured, we cannot interactively navigate that call stack in a debugger nor inspect the state of locals, closures, etc in the context of what they were when the error actually occurred. There is nothing "wrong" about how Q is handling the error based on the Promises/A specification. However, this handling makes it more difficult to debug errors. Hopefully my workaround answer below also helps clarify the difference.
    – drarmstr
    Dec 24, 2013 at 18:37
  • Drarmstr, ah right, thank you, I better understand now, but am unfortunately no nearer being able to offer sensible advice :( Dec 25, 2013 at 0:19

2 Answers 2

1

Looks like I've found a workable answer. I'm posting here so it may hopefully help others or else someone can point out a cleaner approach, as I consider this a hack..

The following works to allow unhandled exceptions in the do_work function to be caught in the Chrome debugger. Not only do you get the nice call stack navigation, but you can inspect values and state at the point of failure.

Instead of:

promise.done(do_work)

Use:

promise.done -> setTimeout do_work, 0

Or in JavaScript:

promise.done(function(){setTimeout(do_work,0);});

With this hack the call to do_work() is still dependent upon the promise fullfillment, but is then divorced from the Q mechanism for catching exceptions so errors can be more easily debugged with Chrome. I also can use this pattern to help debug functions in the middle of the promise chain:

Instead of:

new_promise = promise.then(do_intermediate_work)

Use:

new_promise = promise.then -> Q.promise (resolve)-> setTimeout(
    ->
        do_intermediate_work()
        resolve()
    0
)

Or in JavaScript:

new_promise = promise.then(function() {
    return Q.promise(function(resolve) {
        setTimeout((function() {
            do_intermediate_work();
            resolve();
        }), 0);
    });
});

I hope I'm missing a better solution here as this seems like a usability problem with the current toolchain and debugging with promises..

0
0

There is no useful original call stack in this sense because call stacks don't work through async boundaries. I am not sure what the problem is with the "fake" extended stack that you get with long stack traces enabled (except the fact that it only goes 1 level instead of all the way) - those are just as readable as normal stack traces....

2
  • Besides the nicety of the Call Stack display, you also need to be executing in the context of the error for the Chrome debugger to be able to traverse up and down the stack to view the state of locals, the this context, closures, etc. It is very helpful to be able to inspect this state when an error occurs. If you are only notified about and error at a later point outside this context then you can't inspect that state.
    – drarmstr
    Dec 24, 2013 at 14:03
  • @drarmstr yea I don't really care for that, that has just usually slowed me down or not been necessary.. good luck :)
    – Esailija
    Dec 24, 2013 at 21:01

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.