js-apis-app-ability-errorManager.md 4.2 KB
Newer Older
1
# @ohos.app.ability.errorManager (ErrorManager)
2

3
The **ErrorManager** module provides APIs for registering and deregistering error observers. For example, you can use the APIs to register an observer when your application wants to capture JS crashes.
4 5 6 7 8 9

> **NOTE**
> 
> 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.

## Modules to Import
10
```ts
11
import errorManager from '@ohos.app.ability.errorManager';
12 13 14 15
```

## ErrorManager.on

16
on(type: 'error', observer: ErrorObserver): number;
17 18 19 20 21 22 23 24 25

Registers an error observer.

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Parameters**
 
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
26 27 28 29 30 31 32 33
| type | string | Yes| Type of the API to call. It is fixed at **"error"**.|
| observer | [ErrorObserver](./js-apis-inner-application-errorObserver.md) | Yes| Digital code of the observer.|

**Return value**

  | Type| Description|
  | -------- | -------- |
  | number | Index of the observer.|
34

35 36 37 38 39 40 41 42
**Error codes**

| ID| Error Message|
| ------- | -------- |
| 16000003 | Id does not exist. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).

43 44
**Example**
    
45
```ts
46
let observer = {
47
    onUnhandledException(errorMsg) {
48
        console.log('onUnhandledException, errorMsg: ', errorMsg);
49 50 51 52 53 54 55
    },
    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);
        }
56
    }
57 58
};
let observerId = -1;
59
try {
60
    observerId = errorManager.on('error', observer);
61
} catch (paramError) {
62
    console.error('error: ${paramError.code}, ${paramError.message}');
63 64 65 66 67
}
```

## ErrorManager.off

68
off(type: 'error', observerId: number,  callback: AsyncCallback\<void>): void;
69 70 71 72 73 74 75 76 77

Deregisters an error observer. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Parameters**
 
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
78 79
| type | string | Yes| Type of the API to call. It is fixed at **"error"**.|
| observerId | number | Yes| Index of the observer returned by **on()**.|
80 81
| callback | AsyncCallback\<void> | Yes| Callback used to return the result.|

82 83 84 85 86 87 88 89
**Error codes**

| ID| Error Message|
| ------- | -------- |
| 16000003 | Id does not exist. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).

90 91
**Example**
    
92
```ts
93
let observerId = 100;
94 95 96

function unregisterErrorObserverCallback(err) {
    if (err) {
97
        console.error('------------ unregisterErrorObserverCallback ------------', err);
98 99 100
    }
}
try {
101
    errorManager.off('error', observerId, unregisterErrorObserverCallback);
102
} catch (paramError) {
103
    console.error('error: ${paramError.code}, ${paramError.message}');
104 105 106 107 108
}
```

## ErrorManager.off

109
off(type: 'error', observerId: number): Promise\<void>;
110 111 112 113 114 115 116 117 118

Deregisters an error observer. This API uses a promise to return the result.

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Parameters**
 
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
119 120
| type | string | Yes| Type of the API to call. It is fixed at **"error"**.|
| observerId | number | Yes| Index of the observer returned by **on()**.|
121 122 123 124 125 126 127

**Return value**

| Type| Description|
| -------- | -------- |
| Promise\<void> | Promise used to return the result.|

128 129 130 131 132 133 134 135
**Error codes**

| ID| Error Message|
| ------- | -------- |
| 16000003 | Id does not exist. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).

136 137
**Example**
    
138
```ts
139
let observerId = 100;
140
try {
141
    errorManager.off('error', observerId)
142 143 144 145
        .then((data) => {
            console.log('----------- unregisterErrorObserver success ----------', data);
        })
        .catch((err) => {
146
            console.error('----------- unregisterErrorObserver fail ----------', err);
147
    });
148
} catch (paramError) {
149
    console.error('error: ${paramError.code}, ${paramError.message}');
150 151 152
}

```