react-router icon indicating copy to clipboard operation
react-router copied to clipboard

Transition hooks, stacktrace & promises

Open ganmor opened this issue 11 years ago • 4 comments

Currently the run transition hooks callback is delayed in a settimeout so exceptions are not caught in the promise.

// Use setTimeout to break the promise chain.
    promise.then(function () {
      setTimeout(callback);
    }, function (error) {
      setTimeout(function () {
        callback(error);
      });
    });

This solution prevent the error from being caught, but it also modify the call chain and looses the stack trace. I suggest using the dedicated catch method and use something like this :

promise.then(function () {
      callback(); 
   }, function (error) { 
      callback(error);  
}).catch(function (err){ 
    // Log the complete stack trace, for recent browsers anyway
    if (console && console.error) { console.error(err.stack||err); }
    // Make the stack trace reach the window so any custom exception logger can grab it
    setTimeout(function() { throw err; });
});

I can make a pull request if you want

ganmor avatar Dec 09 '14 16:12 ganmor

I'm confused about what you're trying to achieve.

These all seem to work fine for me, with no exceptions being swallowed:

        willTransitionFrom: function(transition){
           transition.wait(Promise.reject(new Error('message')));
           throw new Error('message');
           return Promise.reject(new Error('message'));
        },
        willTransitionTo: function(transition){
            transition.wait(Promise.reject(new Error('message')));
            throw new Error('message');
        }

Also, I don't see the purpose of your proposed solution. To me, it seems intentional that exceptions from callback() are not caught by that promise. Why would the transition handlers be interested in handling exceptions that occur at a higher level in the application?

agundermann avatar Dec 09 '14 18:12 agundermann

The exception is not swallowed but it does not hold the stacktrace anymore because the call chain is broken by using a setTimeout. If an error occurs in any of the render method of the view tree, the exception will not hold the original line number file etc.. which make it very hard to debug.

I will try to setup a minimal demo, but if you compare the two exceptions ( the one with the proposed code and the current version ) you will see the difference and it makes a very big deal when debugging your app.

Also, in my opinion, if an an exception occurs while transitionning to a new state the top level route handler should be aware of it. It could thus take appropriate measures like redirecting to an error page

ganmor avatar Dec 09 '14 22:12 ganmor

In the examples I posted, the stack trace is preserved by the error object. It wouldn't be if you used a string for rejecting the promise.

React Router already allows you to pass an onError function for handling errors, which is called if there's an exception in willTransitionTo/From.

As for exceptions within render(), those are reported to me by the browser as well, including stack traces. But I don't think it makes sense to catch/handle exceptions from render() . In my opinion, the render methods should not throw in the first place. If they do, it should be a programmer error, and the stack trace helps you find them as is.

agundermann avatar Dec 10 '14 08:12 agundermann

My mistake, after setting up a minimal example, it turns out that it's the exception logger tool that I am using that swallow stacktraces of exceptions caught in window.onerror. ( raven js).

I still think that an error occuring in a render method is a transition error and should be handled, but i can catch them at router level so that's ok.

ganmor avatar Dec 10 '14 10:12 ganmor