• 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**Error codes**
30
31| ID| Error Message|
32| ------- | -------- |
33| 16000003 | Id does not exist. |
34
35For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
36
37**Example**
38
39```js
40let observer = {
41    onUnhandledException(errorMsg) {
42        console.log('onUnhandledException, errorMsg: ', errorMsg)
43    }
44};
45try {
46    errorManager.on('error', observer);
47} catch (paramError) {
48    console.log('error: ' + paramError.code + ', ' + paramError.message);
49}
50```
51
52## ErrorManager.off
53
54off(type: 'error', observerId: number,  callback: AsyncCallback\<void>): void;
55
56Deregisters an error observer. This API uses an asynchronous callback to return the result.
57
58**System capability**: SystemCapability.Ability.AbilityRuntime.Core
59
60**Parameters**
61
62| Name| Type| Mandatory| Description|
63| -------- | -------- | -------- | -------- |
64| type | string | Yes| Type of the API to call.|
65| observerId | number | Yes| Digital code of the observer.|
66| callback | AsyncCallback\<void> | Yes| Callback used to return the result.|
67
68**Error codes**
69
70| ID| Error Message|
71| ------- | -------- |
72| 16000003 | Id does not exist. |
73
74For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
75
76**Example**
77
78```js
79let observerId = 100;
80
81function unregisterErrorObserverCallback(err) {
82    if (err) {
83        console.log('------------ unregisterErrorObserverCallback ------------', err);
84    }
85}
86try {
87    errorManager.off('error', observerId, unregisterErrorObserverCallback);
88} catch (paramError) {
89    console.log('error: ' + paramError.code + ', ' + paramError.message);
90}
91```
92
93## ErrorManager.off
94
95off(type: 'error', observerId: number): Promise\<void>;
96
97Deregisters an error observer. This API uses a promise to return the result.
98
99**System capability**: SystemCapability.Ability.AbilityRuntime.Core
100
101**Parameters**
102
103| Name| Type| Mandatory| Description|
104| -------- | -------- | -------- | -------- |
105| type | string | Yes| Type of the API to call.|
106| observerId | number | Yes| Digital code of the observer.|
107
108**Return value**
109
110| Type| Description|
111| -------- | -------- |
112| Promise\<void> | Promise used to return the result.|
113
114**Error codes**
115
116| ID| Error Message|
117| ------- | -------- |
118| 16000003 | Id does not exist. |
119
120For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
121
122**Example**
123
124```js
125let observerId = 100;
126try {
127    errorManager.off('error', observerId)
128        .then((data) => {
129            console.log('----------- unregisterErrorObserver success ----------', data);
130        })
131        .catch((err) => {
132            console.log('----------- unregisterErrorObserver fail ----------', err);
133    });
134} catch (paramError) {
135    console.log('error: ' + paramError.code + ', ' + paramError.message);
136}
137
138```
139