errormanager-guidelines.md 4.3 KB
Newer Older
S
shawn_he 已提交
1 2
# Development of Error Manager

S
shawn_he 已提交
3
## Overview
S
shawn_he 已提交
4 5 6 7 8 9 10 11 12 13 14

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](#development-example).

**Table 1** Description of application error management APIs

| API                                                      | Description                                                |
| ------------------------------------------------------------ | ---------------------------------------------------- |
S
shawn_he 已提交
15 16 17
| 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. |
S
shawn_he 已提交
18

S
shawn_he 已提交
19
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](#result codes-for-unregistering-an-observer).
S
shawn_he 已提交
20 21 22 23 24 25


**Table 2** Description of the ErrorObserver API

| API                        | Description                                                        |
| ------------------------------ | ------------------------------------------------------------ |
S
shawn_he 已提交
26
| onUnhandledException(errMsg: string): void | Called when an application generates an uncaught exception after being registered.|
S
shawn_he 已提交
27 28 29 30 31 32 33 34 35 36 37 38


### Result Codes for Unregistering an Observer

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

## Development Example
```ts
S
shawn_he 已提交
39 40
import UIAbility from '@ohos.app.ability.UIAbility';
import errorManager from '@ohos.app.ability.errorManager';
S
shawn_he 已提交
41

S
shawn_he 已提交
42 43
let registerId = -1;
let callback = {
S
shawn_he 已提交
44 45 46 47
    onUnhandledException: function (errMsg) {
        console.log(errMsg);
    }
}
S
shawn_he 已提交
48 49

export default class EntryAbility extends UIAbility {
S
shawn_he 已提交
50
    onCreate(want, launchParam) {
S
shawn_he 已提交
51
        console.log("[Demo] EntryAbility onCreate")
S
shawn_he 已提交
52
        registerId = errorManager.on("error", callback);
S
shawn_he 已提交
53 54 55 56
        globalThis.abilityWant = want;
    }

    onDestroy() {
S
shawn_he 已提交
57
        console.log("[Demo] EntryAbility onDestroy")
S
shawn_he 已提交
58
        errorManager.off("error", registerId, (result) => {
S
shawn_he 已提交
59 60
            console.log("[Demo] result " + result.code + ";" + result.message)
        });
S
shawn_he 已提交
61 62 63 64
    }

    onWindowStageCreate(windowStage) {
        // Main window is created for this ability.
S
shawn_he 已提交
65
        console.log("[Demo] EntryAbility onWindowStageCreate")
S
shawn_he 已提交
66

S
shawn_he 已提交
67 68 69 70 71 72 73
        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))
        });
S
shawn_he 已提交
74 75 76 77
    }

    onWindowStageDestroy() {
        // Main window is destroyed to release UI resources.
S
shawn_he 已提交
78
        console.log("[Demo] EntryAbility onWindowStageDestroy")
S
shawn_he 已提交
79 80 81 82
    }

    onForeground() {
        // Ability is brought to the foreground.
S
shawn_he 已提交
83
        console.log("[Demo] EntryAbility onForeground")
S
shawn_he 已提交
84 85 86 87
    }

    onBackground() {
        // Ability is brought back to the background.
S
shawn_he 已提交
88
        console.log("[Demo] EntryAbility onBackground")
S
shawn_he 已提交
89 90 91
    }
};
```