How to prevent app from crashing on uncaughtErrorEvent

荒凉一梦 提交于 2019-12-12 22:39:29

问题


Is there a way to prevent the app from crashing when JS uncaught errors occur?

Already tried to wrap app.start(...) inside try/catch, but it sure doesn't work:)


回答1:


There is indeed, you can register an uncaughtErrorEvent listener.

Refer to the official documentation - https://docs.nativescript.org/core-concepts/application-lifecycle#use-application-events

You can drop the following in your app.js before bootstrapping the application

var application = require("application");

application.on(application.uncaughtErrorEvent, function (args) {
    if (args.android) {
        // For Android applications, args.android is an NativeScriptError.
        console.log("NativeScriptError: " + args.android);
    } else if (args.ios) {
        // For iOS applications, args.ios is NativeScriptError.
        console.log("NativeScriptError: " + args.ios);
    }
});



回答2:


There's no way to prevent the app from crashing. You can catch the error in the uncaughtError application event but the app will crash afterwards regardless.

Attempting to navigate to an error page won't work.

According to the comments on this GitHub issue:

  • Ability to specify custom error pages on uncaughtErrors · Issue #1718 · NativeScript/NativeScript

there is a way to customize the error page shown, but only on Android. Apparently there isn't anything similar available for iOS.




回答3:


This is an older question but there is a documented way to prevent this using Nativescript older than 4.2 (you should be on 6+ now)

Disable rethrowing of uncaught js exceptions to native

Basically, catch the JS errors but do not crash the native app.

Note the disclaimer there as well. The script will be unstable at this point. Yes, it did not crash, but wherever it dies did not continue, so you might be worse off for stability. Putting the app in a bad state or just quitting/recrashing might be safer.



来源:https://stackoverflow.com/questions/48662256/how-to-prevent-app-from-crashing-on-uncaughterrorevent

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!