js-apis-app-ability-errorManager.md 3.4 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

**Example**
    
37
```ts
38
let observer = {
39
    onUnhandledException(errorMsg) {
40
        console.log('onUnhandledException, errorMsg: ', errorMsg);
41
    }
42 43
};
let observerId = -1;
44
try {
45
    observerId = errorManager.on('error', observer);
46
} catch (paramError) {
47
    console.log('error: ${paramError.code}, ${paramError.message}');
48 49 50 51 52
}
```

## ErrorManager.off

53
off(type: 'error', observerId: number,  callback: AsyncCallback\<void>): void;
54 55 56 57 58 59 60 61 62

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|
| -------- | -------- | -------- | -------- |
63 64
| type | string | Yes| Type of the API to call. It is fixed at **"error"**.|
| observerId | number | Yes| Index of the observer returned by **on()**.|
65 66 67 68
| callback | AsyncCallback\<void> | Yes| Callback used to return the result.|

**Example**
    
69
```ts
70
let observerId = 100;
71 72 73 74 75 76 77

function unregisterErrorObserverCallback(err) {
    if (err) {
        console.log('------------ unregisterErrorObserverCallback ------------', err);
    }
}
try {
78
    errorManager.off('error', observerId, unregisterErrorObserverCallback);
79
} catch (paramError) {
80
    console.log('error: ${paramError.code}, ${paramError.message}');
81 82 83 84 85
}
```

## ErrorManager.off

86
off(type: 'error', observerId: number): Promise\<void>;
87 88 89 90 91 92 93 94 95

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

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

**Parameters**
 
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
96 97
| type | string | Yes| Type of the API to call. It is fixed at **"error"**.|
| observerId | number | Yes| Index of the observer returned by **on()**.|
98 99 100 101 102 103 104 105 106

**Return value**

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

**Example**
    
107
```ts
108
let observerId = 100;
109
try {
110
    errorManager.off('error', observerId)
111 112 113 114 115
        .then((data) => {
            console.log('----------- unregisterErrorObserver success ----------', data);
        })
        .catch((err) => {
            console.log('----------- unregisterErrorObserver fail ----------', err);
116
    });
117
} catch (paramError) {
118
    console.log('error: ${paramError.code}, ${paramError.message}');
119 120 121
}

```