Debugging WP8 in PhoneGap

Windows Phone 8

Developing or porting a Cordova / PhoneGap app to WP8 can be particularly tricky. There are lots of gotchas, which are usually difficult to identify without experience. Today I reported an issue that prevents the WP8 platform from even being used with the latest stable version of Cordova. It really does get less attention that iOS and Android.

One of the main issues making the platform difficult to develop for is the inability to easily see errors as they occur using the default setup. In contrast, on the Android platform, you’re able to get an extremely verbose output of everything using logcat.

Show me the errors

I discovered a neat trick over 8 months ago when I was developing a WP8 app in anger. Simply place this code in your app (so that it is executed before any potential errors):

1
2
3
4
window.onerror = function (message, file, line) {
window.external.Notify("Error in Application: " +
message + ". Source File: " + file + ", Line: " + line);
}

This code allows you to not only see any thrown errors in the Visual Studio output window, but also the error message itself and the file / line number of the offending code! A bit better than splurging console.logs everywhere :)

You can see the original gist I wrote here.