|
| 1 | +--- |
| 2 | +title: Error handling |
| 3 | +--- |
| 4 | + |
| 5 | +Errors in NativeScript are handled differently to how they operate in a web application. By default when an unhandled exception is thrown in NativeScript, the app may crash, and an error with the corresponding stack trace will be shown. When the app is in **development** mode this may be the desired behaviour. However, when the app is in **production** similar application crashes can seriously hurt an app's credibility and drive away customers. In many cases, a different behavior is desired (e.g. app freeze, blank screen, failed navigation) to an actual crash with an error log. |
| 6 | + |
| 7 | +NativeScript allows error handling to be set dependent on whether the app is in **development** or **production** mode in the following three ways: |
| 8 | + |
| 9 | +- **development mode** |
| 10 | +**Allow app crash**: Throw exceptions as soon as an error occurs and crash the app. |
| 11 | + |
| 12 | +```ts |
| 13 | +const errorHandler: TraceErrorHandler = { |
| 14 | + handlerError(err) { |
| 15 | + |
| 16 | + throw err |
| 17 | + |
| 18 | + } |
| 19 | +} |
| 20 | +``` |
| 21 | +- **development mode** |
| 22 | + |
| 23 | +**Prevent app crash**: Write the error message to the console and continue the execution of the app. |
| 24 | + |
| 25 | +```ts |
| 26 | +const errorHandler: TraceErrorHandler = { |
| 27 | + handlerError(err) { |
| 28 | + |
| 29 | + Trace.write(err, 'unhandled-error', type.error) |
| 30 | + |
| 31 | + } |
| 32 | +} |
| 33 | +``` |
| 34 | +- **production mode** |
| 35 | + |
| 36 | +**Prevent app crash**: For example, send an error report to an analytics server but continue app execution. |
| 37 | + |
| 38 | +```ts |
| 39 | +const errorHandler: TraceErrorHandler = { |
| 40 | + handlerError(err) { |
| 41 | + |
| 42 | + reportToAnalytics(err) |
| 43 | + } |
| 44 | +} |
| 45 | +``` |
| 46 | +For more details about the `TraceErrorHandler`, see the [Tracing in NativeScript](/guide/nativescript-core/tracing) page. |
| 47 | + |
| 48 | +### Disabling rethrowing of uncaught JS exceptions to native |
| 49 | + |
| 50 | +Nativescript also allows the prevention of an app crash by disabling rethrowing of uncaught JS exceptions to native. This can be done by setting the `discardUncaughtJsExceptions` property to `true` inside the [nativescript.config.ts](/project-structure/nativescript-config-ts)file. |
| 51 | + |
| 52 | +<!--tab: app/nativescript.config.ts --> |
| 53 | + |
| 54 | +```ts |
| 55 | +ios: { |
| 56 | +... |
| 57 | + "discardUncaughtJsExceptions": true, |
| 58 | + |
| 59 | +}, |
| 60 | +android: { |
| 61 | +... |
| 62 | + "discardUncaughtJsExceptions": true, |
| 63 | + |
| 64 | +}, |
| 65 | +``` |
| 66 | +To handle discarded exceptions, two options are available: |
| 67 | +- Listening to the `Application.discardedErrorEvent` and using the received `DiscardedErrorEventData` instance |
| 68 | + |
| 69 | +```ts |
| 70 | +import { Application, DiscardedErrorEventData } from '@nativescript/core' |
| 71 | + |
| 72 | +Application.on(Application.discardedErrorEvent, function (args: DiscardedErrorEventData) { |
| 73 | + const error = args.error |
| 74 | + |
| 75 | + console.log('Received discarded exception: ') |
| 76 | + console.log(error.message) |
| 77 | + console.log(error.name) |
| 78 | + console.log(error.stack) |
| 79 | + console.log(error.nativeError) |
| 80 | + // for example, report the exception to an analytics solution here |
| 81 | +}) |
| 82 | +``` |
| 83 | + |
| 84 | +- Assigning a one-argument function to `global.__onDiscardedError` which will receive the exception as a `NativeScriptError` instance. |
| 85 | + |
| 86 | + |
0 commit comments