How to catch an error from the logcat inside Android App?

217 Views Asked by At

I'm getting the next error on some phones that doesn't have an updated webview engine, or have weird browers that aren't supported by cordova webview.

07-27 13:50:47.233 14356-14356/com.locknot I/chromium: [INFO:CONSOLE(1)] "Uncaught TypeError: Object.assign is not a function", source: file:///android_asset/www/vendor.6356da77cc5903aae591.bundle.js (1)

I want to catch this error, in order to show to the user a dialog saying that his phone is not compatible.

How I can do that? Sorry for my english.

1

There are 1 best solutions below

0
Miloš Černilovský On BEST ANSWER

You can extend the WebChromeClient class and override its onConsoleMessage() method. This method receives all the console messages from within the website it is showing. Finally, set the instance of this WebChromeClient to the WebView via setWebChromeClient() method. So the Kotlin code would look something like this:

webView.webChromeClient = object : WebChromeClient() {
    override fun onConsoleMessage(consoleMessage: ConsoleMessage?): Boolean {
        if (consoleMessage != null && consoleMessage.message().contains("Uncaught TypeError", false)) {
            // show user a dialog
        }
        return super.onConsoleMessage(consoleMessage)
     }
}