You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@cordova.apache.org by "Jesse MacFadyen (JIRA)" <ji...@apache.org> on 2013/04/11 00:57:16 UTC
[jira] [Commented] (CB-2303) Add event for script errors
[ https://issues.apache.org/jira/browse/CB-2303?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13628396#comment-13628396 ]
Jesse MacFadyen commented on CB-2303:
-------------------------------------
Instead of mapping to a new cordova document level event, why not actually just call onerror of window?
instead of:
javascript:cordova.fireDocumentEvent(...)
this:
javascript:window.onerror(msg,fileName,lineNumber)
presumably the developer could already have written a handler ...
window.onerror = function(msg){ ... };
This would allow the same js code to run on multiple devices.
Does this descend into recursive hell?
> Add event for script errors
> ---------------------------
>
> Key: CB-2303
> URL: https://issues.apache.org/jira/browse/CB-2303
> Project: Apache Cordova
> Issue Type: New Feature
> Components: Android
> Reporter: Jonathan Prince
> Assignee: Joe Bowser
>
> Android webview does not support window.onerror for catching unexpected javascript errors. This makes cross platform error logging and analysis very difficult (especially on released software).
> A new event could easily be added to enable developers to track javascript errors.
> e.g. in CordovaChromeClient.java change the onConsoleMessage override from
> {code:title=CordovaChromeClient.java - Current method|borderStyle=solid}
> @TargetApi(8)
> @Override
> public boolean onConsoleMessage(ConsoleMessage consoleMessage)
> {
> if (consoleMessage.message() != null)
> LOG.d(TAG, consoleMessage.message());
> return super.onConsoleMessage(consoleMessage);
> }
> {code}
> All this currently does is re-log messages. Changing the method to the below also triggers a scripterror event for errors (this is just proof of concept code but works correctly). Developers can easily map this event to call a standard error handler.
> {code:title=CordovaChromeClient.java - Suggested method|borderStyle=solid}
> @TargetApi(8)
> @Override
> public boolean onConsoleMessage(ConsoleMessage consoleMessage)
> {
> if (consoleMessage.message() != null)
> {
> if (consoleMessage.messageLevel().name() == "ERROR")
> {
> String errorUrl = "javascript:cordova.fireDocumentEvent('scripterror', {msg: \"" + consoleMessage.message() + "\", line: " + consoleMessage.lineNumber() + ", url: \"" + consoleMessage.sourceId() + "\"});";
> this.appView.loadUrl(errorUrl);
> LOG.e(TAG, consoleMessage.message() + " line " + consoleMessage.lineNumber() + " in " + consoleMessage.sourceId());
> }
> else
> {
> LOG.d(TAG, consoleMessage.message());
> }
> }
> return super.onConsoleMessage(consoleMessage);
> }
> {code}
> The only quirk I have found with this approach is that javascript calls to console.error also trigger the scripterror event. This actually seems logical but developers should be aware of it.
> Otherwise; unexpected script errors and thrown errors trigger the event and handled errors (in try...catch blocks) don't.
--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira