Search code examples
javascriptwindows-phone-8.1promisewinjswinjs-promise

Failing promise gets catched by the Application.onerror function


There's this weird behavior which I don't know if it is supposed to be happening or it's a bug.

I'm using the WinJS.UI.Pages.render to render the content of a page under a specific element.

The problem I'm facing is that no matter if I handle the failing of the render function, the 'error' keeps propagating and it gets "catched" by the onerror function.

Here's a quick view of my code:

function goFunction() {
    WinJS.UI.Pages.render('otherpage.html', root).then(null, failedNavigating);
}

function noGoFunction() {
    WinJS.UI.Pages.render('missingpage.html', root).then(null, failedNavigating);
}

function failedNavigating() {
    console.log('failed to navigate');
}

WinJS.Application.onerror = function (args) {
    debugger;
};

The otherpage.html page exists so the rendering gets executed and everything is fine. But the missingpage.html does not exists. In that case the failedNavigating function executes, so far so good, but then the WinJS.Application.onerror handler gets 'called'.

Is that how that is supposed to work? What am I missing?

Here's a full project with a repro in case anybody wants to take a look at it.


Solution

  • I finally got around to debugging this in your GitHub issue. It's because WinJS.UI.Pages.render implicitly creates a Page, which by default has an error handler that creates an Error promise, which Application was detecting as an "unhandled error promise" since nothing was continuing off of it.

    You can work around it for now with something like this:

    function noGoFunction() {
    
        WinJS.UI.Pages.define('missingpage.html', {
            error: function (e) {
                console.log("Page failed to load");
            }
    
        });
    
        WinJS.UI.Pages.render('missingpage.html', root).then(null, failedNavigating);
    }
    
    function failedNavigating() {
        console.log('failed to navigate');
    }