From 28db2afbd49a6cdb52adb3adee8f14e882217f44 Mon Sep 17 00:00:00 2001 From: Vasil Trifonov Date: Wed, 9 Jan 2019 18:24:25 +0200 Subject: [PATCH] feat: OnDiscardedError typings and event (#6777) * feat: OnDiscardedError typings and event * remove ios and android from DiscardedErrorEventData --- apps/app/ui-tests-app/app.ts | 7 +++++ tests/app/app/app.ts | 6 ++++ .../application/application-common.ts | 10 +++++-- tns-core-modules/application/application.d.ts | 29 +++++++++++++++---- tns-core-modules/module.d.ts | 5 ++-- 5 files changed, 47 insertions(+), 10 deletions(-) diff --git a/apps/app/ui-tests-app/app.ts b/apps/app/ui-tests-app/app.ts index cdeed4d82..e12bb5d9d 100644 --- a/apps/app/ui-tests-app/app.ts +++ b/apps/app/ui-tests-app/app.ts @@ -81,6 +81,13 @@ application.on(application.uncaughtErrorEvent, function(args: application.Unhand console.log("### stack: " + args.error.stack); }); +application.on(application.discardedErrorEvent, function(args: application.DiscardedErrorEventData) { + console.log("### [Discarded] NativeScriptError: " + args.error); + console.log("### [Discarded] nativeException: " + (args.error).nativeException); + console.log("### [Discarded] stackTrace: " + (args.error).stackTrace); + console.log("### [Discarded] stack: " + args.error.stack); +}); + application.setCssFileName("ui-tests-app/app.css"); application.start({ moduleName: "ui-tests-app/main-page" }); diff --git a/tests/app/app/app.ts b/tests/app/app/app.ts index 6b35dfacf..a0e348269 100644 --- a/tests/app/app/app.ts +++ b/tests/app/app/app.ts @@ -90,6 +90,12 @@ application.on(application.uncaughtErrorEvent, function (args: application.Unhan console.log((args.error).stackTrace || (args.error).stack); }); +application.on(application.discardedErrorEvent, function (args: application.DiscardedErrorEventData) { + console.log("[Discarded] NativeScriptError: " + args.error); + console.log((args.error).nativeException || (args.error).nativeError); + console.log((args.error).stackTrace || (args.error).stack); +}); + // Android activity events if (application.android) { application.android.on(application.AndroidApplication.activityCreatedEvent, function (args: application.AndroidActivityBundleEventData) { diff --git a/tns-core-modules/application/application-common.ts b/tns-core-modules/application/application-common.ts index 71af5ddc3..166c4e0d1 100644 --- a/tns-core-modules/application/application-common.ts +++ b/tns-core-modules/application/application-common.ts @@ -38,10 +38,11 @@ import { getRootView, iOSApplication, LoadAppCSSEventData, - UnhandledErrorEventData + UnhandledErrorEventData, + DiscardedErrorEventData } from "./application"; -export { UnhandledErrorEventData, CssChangedEventData, LoadAppCSSEventData }; +export { UnhandledErrorEventData, DiscardedErrorEventData, CssChangedEventData, LoadAppCSSEventData }; export const launchEvent = "launch"; export const suspendEvent = "suspend"; @@ -50,6 +51,7 @@ export const resumeEvent = "resume"; export const exitEvent = "exit"; export const lowMemoryEvent = "lowMemory"; export const uncaughtErrorEvent = "uncaughtError"; +export const discardedErrorEvent = "discardedError"; export const orientationChangedEvent = "orientationChanged"; let cssFile: string = "./app.css"; @@ -122,3 +124,7 @@ export function addCss(cssText: string): void { global.__onUncaughtError = function (error: NativeScriptError) { events.notify({ eventName: uncaughtErrorEvent, object: app, android: error, ios: error, error: error }); } + +global.__onDiscardedError = function (error: NativeScriptError) { + events.notify({ eventName: discardedErrorEvent, object: app, error: error }); +} diff --git a/tns-core-modules/application/application.d.ts b/tns-core-modules/application/application.d.ts index 92ac02946..582a19786 100644 --- a/tns-core-modules/application/application.d.ts +++ b/tns-core-modules/application/application.d.ts @@ -21,6 +21,11 @@ export var displayedEvent: string; */ export var uncaughtErrorEvent: string; +/** + * String value used when hooking to discardedError event. + */ +export var discardedErrorEvent: string; + /** * String value used when hooking to suspend event. */ @@ -103,6 +108,13 @@ export interface UnhandledErrorEventData extends ApplicationEventData { error: NativeScriptError; } +/** + * Event data containing information about discarded application errors. + */ +export interface DiscardedErrorEventData extends ApplicationEventData { + error: NativeScriptError; +} + /** * Event data containing information about application css change. */ @@ -137,7 +149,7 @@ export function setResources(res: any): void; export function setResources(resources: any); /** - * Sets css file name for the application. + * Sets css file name for the application. */ export function setCssFileName(cssFile: string): void; @@ -199,7 +211,7 @@ export function shouldCreateRootFrame(): boolean; /** * A basic method signature to hook an event listener (shortcut alias to the addEventListener method). - * @param eventNames - String corresponding to events (e.g. "onLaunch"). Optionally could be used more events separated by `,` (e.g. "onLaunch", "onSuspend"). + * @param eventNames - String corresponding to events (e.g. "onLaunch"). Optionally could be used more events separated by `,` (e.g. "onLaunch", "onSuspend"). * @param callback - Callback function which will be executed when event is raised. * @param thisArg - An optional parameter which will be used as `this` context for callback execution. */ @@ -262,6 +274,11 @@ export function on(event: "lowMemory", callback: (args: ApplicationEventData) => */ export function on(event: "uncaughtError", callback: (args: UnhandledErrorEventData) => void, thisArg?: any); +/** + * This event is raised when an discarded error occurs while the application is running. + */ +export function on(event: "discardedError", callback: (args: DiscardedErrorEventData) => void, thisArg?: any); + /** * This event is raised the orientation of the current device has changed. */ @@ -403,13 +420,13 @@ export class AndroidApplication extends Observable { /** * Initialized the android-specific application object with the native android.app.Application instance. * This is useful when creating custom application types. - * @param nativeApp - the android.app.Application instance that started the app. + * @param nativeApp - the android.app.Application instance that started the app. */ init: (nativeApp) => void; /** * A basic method signature to hook an event listener (shortcut alias to the addEventListener method). - * @param eventNames - String corresponding to events (e.g. "propertyChange"). Optionally could be used more events separated by `,` (e.g. "propertyChange", "change"). + * @param eventNames - String corresponding to events (e.g. "propertyChange"). Optionally could be used more events separated by `,` (e.g. "propertyChange", "change"). * @param callback - Callback function which will be executed when event is raised. * @param thisArg - An optional parameter which will be used as `this` context for callback execution. */ @@ -516,7 +533,7 @@ export class AndroidApplication extends Observable { public static activityRequestPermissionsEvent: string; /** - * Register a BroadcastReceiver to be run in the main activity thread. The receiver will be called with any broadcast Intent that matches filter, in the main application thread. + * Register a BroadcastReceiver to be run in the main activity thread. The receiver will be called with any broadcast Intent that matches filter, in the main application thread. * For more information, please visit 'http://developer.android.com/reference/android/content/Context.html#registerReceiver%28android.content.BroadcastReceiver,%20android.content.IntentFilter%29' * @param intentFilter A string containing the intent filter. * @param onReceiveCallback A callback function that will be called each time the receiver receives a broadcast. @@ -524,7 +541,7 @@ export class AndroidApplication extends Observable { registerBroadcastReceiver(intentFilter: string, onReceiveCallback: (context: any /* android.content.Context */, intent: any /* android.content.Intent */) => void): void; /** - * Unregister a previously registered BroadcastReceiver. + * Unregister a previously registered BroadcastReceiver. * For more information, please visit 'http://developer.android.com/reference/android/content/Context.html#unregisterReceiver(android.content.BroadcastReceiver)' * @param intentFilter A string containing the intent filter with which the receiver was originally registered. */ diff --git a/tns-core-modules/module.d.ts b/tns-core-modules/module.d.ts index 0a0f6a033..f252e6295 100644 --- a/tns-core-modules/module.d.ts +++ b/tns-core-modules/module.d.ts @@ -3,7 +3,7 @@ declare var global: NodeJS.Global; interface ModuleResolver { /** * A function used to resolve the exports for a module. - * @param uri The name of the module to be resolved. + * @param uri The name of the module to be resolved. */ (uri: string): any; } @@ -18,7 +18,7 @@ declare namespace NodeJS { * Register all modules from a webpack context. * The context is one created using the following webpack utility: * https://webpack.github.io/docs/context.html - * + * * The extension map is optional, modules in the webpack context will have their original file extension (e.g. may be ".ts" or ".scss" etc.), * while the built-in module builders in {N} will look for ".js", ".css" or ".xml" files. Adding a map such as: * ``` @@ -54,6 +54,7 @@ declare namespace NodeJS { __onLiveSync: (context?: { type: string, module: string }) => void; __onLiveSyncCore: () => void; __onUncaughtError: (error: NativeScriptError) => void; + __onDiscardedError: (error: NativeScriptError) => void; TNS_WEBPACK?: boolean; __requireOverride?: (name: string, dir: string) => any; }