harmony 鸿蒙Development of Error Manager

  • 2022-08-09
  • 浏览 (761)

Development of Error Manager

Overview

If coding specification issues or errors exist in the code of an application, the application may encounter unexpected errors, for example, uncaught exceptions or application lifecycle timeouts, while it is running. In such a case, the application may exit unexpectedly. Error logs, however, are usually stored on users’ local storage, making it inconvenient to locate faults. With the APIs provided by the errorManager module, your application will be able to report related errors and logs to your service platform for fault locating before it exits.

Available APIs

Application error management APIs are provided by the errorManager module. For details about how to import the module to use related APIs, see Development Example.

Table 1 Description of application error management APIs

API Description
on(type: “error”, observer: ErrorObserver): number Registers an observer for application errors. A callback will be invoked when an application error is detected. This API works in a synchronous manner. The return value is the SN of the registered observer.
off(type: “error”, observerId: number, callback: AsyncCallback<void>): void Unregisters an observer in callback mode. The number passed to this API is the SN of the registered observer.
off(type: “error”, observerId: number): Promise<void> Unregisters an observer in promise mode. The number passed to this API is the SN of the registered observer.

When an asynchronous callback is used, the return value can be processed directly in the callback. If a promise is used, the return value can also be processed in the promise in a similar way. For details about the result codes, see Result Codes for Unregistering an Observer.

Table 2 Description of the ErrorObserver API

API Description
onUnhandledException(errMsg: string): void Called when an uncaught exception is reported after the application is registered.
onException?(errObject: Error): void Called when an application exception is reported to the JavaScript layer after the application is registered.

Result Codes for Unregistering an Observer

Result Code Description
0 Normal.
-1 Input number not exist.
-2 Invalid parameter.

Development Example

import UIAbility from '@ohos.app.ability.UIAbility';
import AbilityConstant from '@ohos.app.ability.AbilityConstant';
import errorManager from '@ohos.app.ability.errorManager';
import Want from '@ohos.app.ability.Want';
import window from '@ohos.window';

let registerId = -1;
let callback: errorManager.ErrorObserver = {
    onUnhandledException: (errMsg) => {
        console.log(errMsg);
    },
    onException: (errorObj) => {
        console.log('onException, name: ', errorObj.name);
        console.log('onException, message: ', errorObj.message);
        if (typeof(errorObj.stack) === 'string') {
            console.log('onException, stack: ', errorObj.stack);
        }
    }
}
let abilityWant: Want;

export default class EntryAbility extends UIAbility {
    onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
        console.log("[Demo] EntryAbility onCreate")
        registerId = errorManager.on("error", callback);
        abilityWant = want;
    }

    onDestroy() {
        console.log("[Demo] EntryAbility onDestroy")
        errorManager.off("error", registerId, (result) => {
            console.log("[Demo] result " + result.code + ";" + result.message)
        });
    }

    onWindowStageCreate(windowStage: window.WindowStage) {
        // Main window is created for this ability.
        console.log("[Demo] EntryAbility onWindowStageCreate")

        windowStage.loadContent("pages/index", (err, data) => {
            if (err.code) {
                console.error('Failed to load the content. Cause:' + JSON.stringify(err));
                return;
            }
            console.info('Succeeded in loading the content. Data: ' + JSON.stringify(data))
        });
    }

    onWindowStageDestroy() {
        // Main window is destroyed to release UI resources.
        console.log("[Demo] EntryAbility onWindowStageDestroy")
    }

    onForeground() {
        // Ability is brought to the foreground.
        console.log("[Demo] EntryAbility onForeground")
    }

    onBackground() {
        // Ability is brought back to the background.
        console.log("[Demo] EntryAbility onBackground")
    }
};

你可能感兴趣的鸿蒙文章

harmony 鸿蒙DFX

harmony 鸿蒙Application Freeze (appfreeze) Log Analysis

harmony 鸿蒙Development of Application Recovery

harmony 鸿蒙Process Crash (cppcrash) Log Analysis

harmony 鸿蒙Development of Application Event Logging

harmony 鸿蒙HiLog Development (Native)

harmony 鸿蒙Development of Distributed Call Chain Tracing

harmony 鸿蒙Development of Performance Tracing (ArkTS)

harmony 鸿蒙Development of Performance Tracing (Native)

0  赞