Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

window.onerror doesn't work in mobile -- How should I deal with unhandled exceptions?

I'm working on a complex web application written in Sencha Touch and PhoneGap. The way Sencha Touch applications are written makes it hard to surround every potential failure point with a try/catch, especially with asynchronous AJAX request handlers.

It's also critically important to detect when an unhandled exception occurs because it looks and feels like a native app with PhoneGap (i.e. we can't expect them to refresh the page and try again like in a web app you view in a regular browser).

Can you suggest a recommendation for how to deal with this scenario?

like image 828
Yoh Suzuki Avatar asked Aug 04 '11 21:08

Yoh Suzuki


1 Answers

It's a great question. Of course the window error event should never replace strategically placed try/catch blocks or good use of error responses from HTTP requests, but until the mobile browsers catch up with the desktop versions, reporting back truly unexpected errors is near impossible.

I don't have time to test this out; so all I can suggest is a thought experiment at the moment for what is in all honesty probably a bad idea...

You could potentially write replacement functions for addEventListener (and maybe dispatchMethod) that could wrap the native functions / handler arguments with try/catch and call a custom global error logger. Since there is no easy way to 'hook' these functions all at once, it would no doubt mean iterating over many of the native objects' prototypes on window to perform the 'wrapping'. This could cover a large majority of exceptions that would currently go silent.

like image 180
Chris Farmiloe Avatar answered Oct 11 '22 22:10

Chris Farmiloe