• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.app.ability.errorManager (ErrorManager)
2
3The **ErrorManager** module provides APIs for registering and deregistering error observers.
4
5> **NOTE**
6>
7> The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8
9## Modules to Import
10```
11import errorManager from '@ohos.app.ability.errorManager'
12```
13
14## ErrorManager.on
15
16on(type: 'error', observer: ErrorObserver): number;
17
18Registers an error observer.
19
20**System capability**: SystemCapability.Ability.AbilityRuntime.Core
21
22**Parameters**
23
24| Name| Type| Mandatory| Description|
25| -------- | -------- | -------- | -------- |
26| type | string | Yes| Type of the API to call.|
27| observer | [ErrorObserver](./js-apis-inner-application-errorObserver.md) | Yes| Digital code of the observer.|
28
29**Example**
30
31```js
32let observer = {
33    onUnhandledException(errorMsg) {
34        console.log('onUnhandledException, errorMsg: ', errorMsg)
35    }
36};
37try {
38    errorManager.on('error', observer);
39} catch (paramError) {
40    console.log('error: ' + paramError.code + ', ' + paramError.message);
41}
42```
43
44## ErrorManager.off
45
46off(type: 'error', observerId: number,  callback: AsyncCallback\<void>): void;
47
48Deregisters an error observer. This API uses an asynchronous callback to return the result.
49
50**System capability**: SystemCapability.Ability.AbilityRuntime.Core
51
52**Parameters**
53
54| Name| Type| Mandatory| Description|
55| -------- | -------- | -------- | -------- |
56| type | string | Yes| Type of the API to call.|
57| observerId | number | Yes| Digital code of the observer.|
58| callback | AsyncCallback\<void> | Yes| Callback used to return the result.|
59
60**Example**
61
62```js
63let observerId = 100;
64
65function unregisterErrorObserverCallback(err) {
66    if (err) {
67        console.log('------------ unregisterErrorObserverCallback ------------', err);
68    }
69}
70try {
71    errorManager.off('error', observerId, unregisterErrorObserverCallback);
72} catch (paramError) {
73    console.log('error: ' + paramError.code + ', ' + paramError.message);
74}
75```
76
77## ErrorManager.off
78
79off(type: 'error', observerId: number): Promise\<void>;
80
81Deregisters an error observer. This API uses a promise to return the result.
82
83**System capability**: SystemCapability.Ability.AbilityRuntime.Core
84
85**Parameters**
86
87| Name| Type| Mandatory| Description|
88| -------- | -------- | -------- | -------- |
89| type | string | Yes| Type of the API to call.|
90| observerId | number | Yes| Digital code of the observer.|
91
92**Return value**
93
94| Type| Description|
95| -------- | -------- |
96| Promise\<void> | Promise used to return the result.|
97
98**Example**
99
100```js
101let observerId = 100;
102try {
103    errorManager.off('error', observerId)
104        .then((data) => {
105            console.log('----------- unregisterErrorObserver success ----------', data);
106        })
107        .catch((err) => {
108            console.log('----------- unregisterErrorObserver fail ----------', err);
109    });
110} catch (paramError) {
111    console.log('error: ' + paramError.code + ', ' + paramError.message);
112}
113
114```
115