Logging & catching JS errors on iPhone

2980 views
Skip to first unread message

pamela

unread,
Oct 20, 2011, 4:20:24 PM10/20/11
to phonegap
Hey folks -

I've spent the last few days getting a PhoneGap app working on an
iPhone (an app which previously worked on Android). The hardest part
has been learning to debug in the iOS browser, so I thought I'd post
on my findings:

- To view the output of console.log, you must open the XCode console.
The iOS browser "Debug console" that most iOS debugging apps mention
is only displayed in the actual browser, not in the WebView (where
PhoneGap HTML lives).

- There seem to be times when console.log does not log the output
(perhaps during loading?) - in that case, alert() always seems to
work.

- If you log a JS object using console.log, it will just print
"Object" by default. You must JSON stringify it to be useful.

- You can also use debug.phonegap.com (hosted weinre) to view the DOM
and JS console logs as well.

- The WebView browser silently fails on JS errors - it stops running
the JS code and does not report the error. To see the error, you must
wrap the offending code in a try/catch block.

Saying all that, here's my log() wrapper function that I use across my
webapp:

function log(something) {
if (window.console){
if (something instanceof Date) {
something = something.toDateString();
}
if (isIOS() || isAndroid()) {
if (typeof something == 'object') {
something = JSON.stringify(something);
}
console.log(something);
} else {
console.log(something);
}
}
}

And I wrap various code blocks in try/catch, like the callback
function for AJAX requests:
try {
onSuccess(processJSON(responseJSON));
} catch(e) {
log(e);
}

I'd love to hear other people's tips for debugging their PhoneGap
apps.

- pamela

Nanomo

unread,
Oct 20, 2011, 5:10:14 PM10/20/11
to phonegap
Hi pamela,

use ripple, a Google chrome plugin.

Good luck.

Patrick Mueller

unread,
Oct 24, 2011, 8:52:27 AM10/24/11
to phon...@googlegroups.com
On 10/20/11 4:20 PM, pamela wrote:
> I've spent the last few days getting a PhoneGap app working on an
> iPhone (an app which previously worked on Android). The hardest part
> has been learning to debug in the iOS browser, so I thought I'd post
> on my findings:
> ...

> - There seem to be times when console.log does not log the output
> (perhaps during loading?) - in that case, alert() always seems to
> work.

I suppose it can't be guaranteed that console.log will 'work' until
after you get a 'deviceready' event. I think you should write a bug up
though. I have a similar issue with weinre, it seems like we could at
least queue up the messages until the native hookup is ready ...

> - If you log a JS object using console.log, it will just print
> "Object" by default. You must JSON stringify it to be useful.

Open a bug. I also had this issue in weinre, which I fixed at some
point. Emulating current browser semantics for the output rendering
isn't a huge problem (I think).

> - The WebView browser silently fails on JS errors - it stops running
> the JS code and does not report the error. To see the error, you must
> wrap the offending code in a try/catch block.

I wrote this little utility a while back, which monkeypatches some of
the callback-based functions in the browser, so that the callback is run
with a try/catch, and some info will be dumped to the console when an
exception occurs. Something very similar will be included in the next
version of weinre.

https://github.com/pmuellr/log-callback-error

--
Patrick Mueller - http://muellerware.org

pamela fox

unread,
Nov 2, 2011, 2:27:13 AM11/2/11
to phon...@googlegroups.com
Would you consider all these issues to be PhoneGap issues? I don't
understand PhoneGap/iOS enough to know which qualify as PhoneGap
issues, so I've been hesitant to file them.

That utility looks great. Will the next version of weinre be on
debug.phonegap.com?

> --
> You received this message because you are subscribed to the Google
> Groups "phonegap" group.
> To post to this group, send email to phon...@googlegroups.com
> To unsubscribe from this group, send email to
> phonegap+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/phonegap?hl=en?hl=en
>
> For more info on PhoneGap or to download the code go to www.phonegap.com
>

Reply all
Reply to author
Forward
0 new messages