cordova-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jonathan Prince (JIRA)" <j...@apache.org>
Subject [jira] [Created] (CB-2303) Add event for script errors
Date Tue, 29 Jan 2013 10:03:13 GMT
Jonathan Prince created CB-2303:
-----------------------------------

             Summary: 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 

    @TargetApi(8)
    @Override
    public boolean onConsoleMessage(ConsoleMessage consoleMessage)
    {
        if (consoleMessage.message() != null)
            LOG.d(TAG, consoleMessage.message());
        return super.onConsoleMessage(consoleMessage);
    }

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.

    @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);
    }

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

Mime
View raw message