1# @ohos.app.ability.errorManager (ErrorManager) 2 3ErrorManager模块提供对错误观察器的注册和注销的能力。使用场景:应用想要捕获js crash。 4 5> **说明:** 6> 7> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8 9## 导入模块 10```ts 11import errorManager from '@ohos.app.ability.errorManager'; 12``` 13 14## ErrorManager.on 15 16on(type: 'error', observer: ErrorObserver): number; 17 18注册错误观测器。 19 20**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 21 22**参数:** 23 24| 参数名 | 类型 | 必填 | 说明 | 25| -------- | -------- | -------- | -------- | 26| type | string | 是 | 填写'error',表示错误观察器。 | 27| observer | [ErrorObserver](./js-apis-inner-application-errorObserver.md) | 是 | 错误观察器。 | 28 29**返回值:** 30 31 | 类型 | 说明 | 32 | -------- | -------- | 33 | number | 观察器的index值,和观察器一一对应。 | 34 35**错误码**: 36 37| 错误码ID | 错误信息 | 38| ------- | -------- | 39| 16000003 | Id does not exist. | 40 41以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)。 42 43**示例:** 44 45```ts 46import errorManager from '@ohos.app.ability.errorManager'; 47import { BusinessError } from '@ohos.base'; 48 49let observer: errorManager.ErrorObserver = { 50 onUnhandledException(errorMsg) { 51 console.log('onUnhandledException, errorMsg: ', errorMsg); 52 }, 53 onException(errorObj) { 54 console.log('onException, name: ', errorObj.name); 55 console.log('onException, message: ', errorObj.message); 56 if (typeof(errorObj.stack) === 'string') { 57 console.log('onException, stack: ', errorObj.stack); 58 } 59 } 60}; 61let observerId = -1; 62try { 63 observerId = errorManager.on('error', observer); 64} catch (paramError) { 65 let code = (paramError as BusinessError).code; 66 let message = (paramError as BusinessError).message; 67 console.error(`error: ${code}, ${message}`); 68} 69``` 70 71## ErrorManager.off 72 73off(type: 'error', observerId: number, callback: AsyncCallback\<void>): void; 74 75注销错误观测器。 76 77**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 78 79**参数:** 80 81| 参数名 | 类型 | 必填 | 说明 | 82| -------- | -------- | -------- | -------- | 83| type | string | 是 | 填写'error',表示错误观察器。 | 84| observerId | number | 是 | 由on方法返回的观察器的index值。 | 85| callback | AsyncCallback\<void> | 是 | 表示指定的回调方法。 | 86 87**错误码**: 88 89| 错误码ID | 错误信息 | 90| ------- | -------- | 91| 16000003 | Id does not exist. | 92 93以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)。 94 95**示例:** 96 97```ts 98import errorManager from '@ohos.app.ability.errorManager'; 99import { BusinessError } from '@ohos.base'; 100 101let observerId = 100; 102 103function unregisterErrorObserverCallback(err: BusinessError) { 104 if (err) { 105 console.error('------------ unregisterErrorObserverCallback ------------', err); 106 } 107} 108try { 109 errorManager.off('error', observerId, unregisterErrorObserverCallback); 110} catch (paramError) { 111 let code = (paramError as BusinessError).code; 112 let message = (paramError as BusinessError).message; 113 console.error(`error: ${code}, ${message}`); 114} 115``` 116 117## ErrorManager.off 118 119off(type: 'error', observerId: number): Promise\<void>; 120 121注销错误观测器。 122 123**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 124 125**参数:** 126 127| 参数名 | 类型 | 必填 | 说明 | 128| -------- | -------- | -------- | -------- | 129| type | string | 是 | 填写'error',表示错误观察器。 | 130| observerId | number | 是 | 由on方法返回的观察器的index值。 | 131 132**返回值:** 133 134| 类型 | 说明 | 135| -------- | -------- | 136| Promise\<void> | 返回执行结果。 | 137 138**错误码**: 139 140| 错误码ID | 错误信息 | 141| ------- | -------- | 142| 16000003 | Id does not exist. | 143 144以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)。 145 146**示例:** 147 148```ts 149import errorManager from '@ohos.app.ability.errorManager'; 150import { BusinessError } from '@ohos.base'; 151 152let observerId = 100; 153try { 154 errorManager.off('error', observerId) 155 .then((data) => { 156 console.log('----------- unregisterErrorObserver success ----------', data); 157 }) 158 .catch((err: BusinessError) => { 159 console.error('----------- unregisterErrorObserver fail ----------', err); 160 }); 161} catch (paramError) { 162 let code = (paramError as BusinessError).code; 163 let message = (paramError as BusinessError).message; 164 console.error(`error: ${code}, ${message}`); 165} 166 167``` 168