1

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

pkanev
  • 1,486
  • 1
  • 12
  • 20
terreb
  • 1,417
  • 2
  • 23
  • 40

3 Answers3

2

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);
    }
});
pkanev
  • 1,486
  • 1
  • 12
  • 20
  • 1
    Sorry, this doesn't answer the question. I do know how to register uncaughtErrorEvent. I would like to know how to prevent the app from crashing if such an error occur. Considering this is a JS, not native error, so the crash in production is something that I want to avoid. – terreb Feb 07 '18 at 15:53
  • 1
    @terreb You would ideally wrap the calls that you think can cause problems in try-catch clauses. You can't exactly have an exception, and continue using the app, as the application will often be left in an unrecoverable state. – pkanev Feb 07 '18 at 16:03
  • Can I have a global try/catch somewhere? – terreb Feb 08 '18 at 08:26
  • 1
    Mobile apps run fundamentally different from those applications that have a main() method, where the application terminates afterward. Your uncaughtError Handler is your 'try/catch', you just can't recover from it. – pkanev Feb 08 '18 at 08:29
  • Ok, thank you for your time and reply. I'll wait for some time, hopefully someone else will give me more insights. If no I will mark your answer as accepted. – terreb Feb 09 '18 at 09:16
  • 1
    @terreb think of your android program as of a program with a main() method, where a `while (true) {}` loop runs. Your program executes inside that fictitious loop, also called `Looper` in android. Normally, should an unhandled exception occur in the loop, the program will break out of it, similarly, if a crash occurs on Android, you can't recover from it, but you have the chance to finalize any work you may have started, send reports, and release resources. – pkanev Feb 12 '18 at 10:31
  • Thank you for the explanations, very appreciated. Marked your answer as accepted. – terreb Feb 13 '18 at 14:36
1

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:

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

Kenny Evitt
  • 9,291
  • 5
  • 65
  • 93
0

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.

clay
  • 5,917
  • 2
  • 23
  • 21