【问题标题】:Object has no method 'includes'对象没有方法“包含”
【发布时间】:2020-03-09 19:41:55
【问题描述】:

我正在使用Sentry 在我的平台 (CakePHP 2.*) 上捕获错误,我收到以下错误:

TypeError ?(clients/view/1998667)
· Object http://192.168.1.130/clients/view/1998667 has no method 'includes'

我发现这个版本的浏览器中的StringArray 对象都没有定义Object.prototype.includes 函数,所以我在任何可能使用此对象方法的脚本之前添加了它(在库和自定义代码之前)。

(不是完美的功能,但应该适合我的用例)。

if (String.prototype.includes === undefined)
{
    console.log('String.prototype.inclues is UNDEFINED');

    String.prototype.includes = function(word){
        return this.toLowerCase().indexOf(word.toLowerCase()) > -1;
    }
}

if (Array.prototype.includes === undefined)
{
    console.log('Array.prototype.inclues is UNDEFINED');

    Array.prototype.includes = function(search){
        var includes = false;

        this.forEach(function(item){
            if (item === search)
            {
                includes = true;

                return includes;
            }
        });

        return includes;
    }
}

奇怪的是;即使我在模拟器上运行Android with API 16,Sentry 仍会不断捕获错误,并且视图无法正常工作,并且我无法在我的 Android 代码中捕获错误。

我什至在 Safari 中使用来自 Android 的 WebView Mozilla/5.0 (Linux; U; Android 4.1.2; en-us; Android SDK built for x86 Build/MASTER) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30 的相同用户代理进行了测试

控制台消息的Android代码:

webView.setWebChromeClient(new WebChromeClient() {
    @Override
    public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
         android.util.Log.d("WebView", consoleMessage.message());

         return true;
    }
});

我的问题是:如何在 Android Java 中捕获 JavaScript 错误,以及我在哪些方面做得不对以使定义的函数正常工作?

【问题讨论】:

    标签: javascript android webview sentry


    【解决方案1】:

    Sentry Android SDK 不会检测到任何 JS 错误,但您可以执行类似于 Detecting Webview Error and Show Message 的操作

    为您的 webview 添加一个回调(例如 onReceivedError),从 errorCode、描述中创建一个 Exception 或 SentryEvent 并手动调用 Sentry.captureException(e)。

    【讨论】:

      猜你喜欢
      • 2013-01-19
      • 2014-10-17
      • 2011-12-08
      • 2015-01-09
      • 2015-02-02
      • 2023-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多