1# @ohos.app.ability.errorManager (ErrorManager) 2 3The ErrorManager module provides APIs for registering and unregistering 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```ts 11import { errorManager } from '@kit.AbilityKit'; 12``` 13 14## errorManager.on('error') 15 16on(type: 'error', observer: ErrorObserver): number 17 18Registers an error observer. After the registration, JS crashes generated by the application can be captured. When the application breaks down, the process does not exit. 19 20**Atomic service API**: This API can be used in atomic services since API version 11. 21 22**System capability**: SystemCapability.Ability.AbilityRuntime.Core 23 24**Parameters** 25 26| Name| Type| Mandatory| Description| 27| -------- | -------- | -------- | -------- | 28| type | string | Yes| Event type. It is fixed at **"error"**.| 29| observer | [ErrorObserver](js-apis-inner-application-errorObserver.md) | Yes| Digital code of the observer.| 30 31**Return value** 32 33 | Type| Description| 34 | -------- | -------- | 35 | number | Index of the observer.| 36 37**Error codes** 38 39For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 40 41| ID| Error Message| 42| ------- | -------- | 43| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3. Parameter verification failed. | 44| 16000003 | The specified ID does not exist. | 45 46**Example** 47 48```ts 49import { errorManager } from '@kit.AbilityKit'; 50import { BusinessError } from '@kit.BasicServicesKit'; 51 52let observer: errorManager.ErrorObserver = { 53 onUnhandledException(errorMsg) { 54 console.log('onUnhandledException, errorMsg: ', errorMsg); 55 }, 56 onException(errorObj) { 57 console.log('onException, name: ', errorObj.name); 58 console.log('onException, message: ', errorObj.message); 59 if (typeof(errorObj.stack) === 'string') { 60 console.log('onException, stack: ', errorObj.stack); 61 } 62 } 63}; 64let observerId = -1; 65 66try { 67 observerId = errorManager.on('error', observer); 68} catch (paramError) { 69 let code = (paramError as BusinessError).code; 70 let message = (paramError as BusinessError).message; 71 console.error(`error: ${code}, ${message}`); 72} 73``` 74 75## errorManager.off('error') 76 77off(type: 'error', observerId: number, callback: AsyncCallback\<void>): void 78 79Unregisters an error observer. This API uses an asynchronous callback to return the result. 80 81**Atomic service API**: This API can be used in atomic services since API version 11. 82 83**System capability**: SystemCapability.Ability.AbilityRuntime.Core 84 85**Parameters** 86 87| Name| Type| Mandatory| Description| 88| -------- | -------- | -------- | -------- | 89| type | string | Yes| Event type. It is fixed at **"error"**.| 90| observerId | number | Yes| Index of the observer returned by **on()**.| 91| callback | AsyncCallback\<void> | Yes| Callback used to return the result.| 92 93**Error codes** 94 95For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 96 97| ID| Error Message| 98| ------- | -------- | 99| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3. Parameter verification failed. | 100| 16000003 | The specified ID does not exist. | 101 102**Example** 103 104```ts 105import { errorManager } from '@kit.AbilityKit'; 106import { BusinessError } from '@kit.BasicServicesKit'; 107 108let observerId = 100; 109 110function unregisterErrorObserverCallback(err: BusinessError) { 111 if (err) { 112 console.error('------------ unregisterErrorObserverCallback ------------', err); 113 } 114} 115 116try { 117 errorManager.off('error', observerId, unregisterErrorObserverCallback); 118} catch (paramError) { 119 let code = (paramError as BusinessError).code; 120 let message = (paramError as BusinessError).message; 121 console.error(`error: ${code}, ${message}`); 122} 123``` 124 125## errorManager.off('error') 126 127off(type: 'error', observerId: number): Promise\<void> 128 129Unregisters an error observer. This API uses a promise to return the result. 130 131**Atomic service API**: This API can be used in atomic services since API version 11. 132 133**System capability**: SystemCapability.Ability.AbilityRuntime.Core 134 135**Parameters** 136 137| Name| Type| Mandatory| Description| 138| -------- | -------- | -------- | -------- | 139| type | string | Yes| Event type. It is fixed at **"error"**.| 140| observerId | number | Yes| Index of the observer returned by **on()**.| 141 142**Return value** 143 144| Type| Description| 145| -------- | -------- | 146| Promise\<void> | Promise that returns no value.| 147 148**Error codes** 149 150For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 151 152| ID| Error Message| 153| ------- | -------- | 154| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3. Parameter verification failed. | 155| 16000003 | The specified ID does not exist. | 156 157**Example** 158 159```ts 160import { errorManager } from '@kit.AbilityKit'; 161import { BusinessError } from '@kit.BasicServicesKit'; 162 163let observerId = 100; 164 165try { 166 errorManager.off('error', observerId) 167 .then((data) => { 168 console.log('----------- unregisterErrorObserver success ----------', data); 169 }) 170 .catch((err: BusinessError) => { 171 console.error('----------- unregisterErrorObserver fail ----------', err); 172 }); 173} catch (paramError) { 174 let code = (paramError as BusinessError).code; 175 let message = (paramError as BusinessError).message; 176 console.error(`error: ${code}, ${message}`); 177} 178``` 179 180## errorManager.on('loopObserver')<sup>12+</sup> 181 182on(type: 'loopObserver', timeout: number, observer: LoopObserver): void 183 184Registers an observer for the message processing duration of the main thread. After the registration, the execution time of a message processed by the main thread of the application can be captured. 185 186**Atomic service API**: This API can be used in atomic services since API version 12. 187 188**System capability**: SystemCapability.Ability.AbilityRuntime.Core 189 190**Parameters** 191 192| Name| Type| Mandatory| Description| 193| -------- | -------- | -------- | -------- | 194| type | string | Yes| Event type. It is fixed at **'loopObserver'**, indicating an observer for the message processing duration of the main thread.| 195| timeout | number | Yes| Event execution threshold, in milliseconds. The value must be greater than **0**.| 196| observer | [LoopObserver](js-apis-inner-application-loopObserver.md) | Yes| Observer to register.| 197 198**Error codes** 199 200For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 201 202| ID| Error Message| 203| ------- | -------- | 204| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3. Parameter verification failed. | 205 206**Example** 207 208```ts 209import { errorManager } from '@kit.AbilityKit'; 210 211let observer: errorManager.LoopObserver = { 212 onLoopTimeOut(timeout: number) { 213 console.log('Duration timeout: ' + timeout); 214 } 215}; 216 217errorManager.on("loopObserver", 1, observer); 218``` 219 220## errorManager.on('unhandledRejection')<sup>12+</sup> 221 222on(type: 'unhandledRejection', observer: UnhandledRejectionObserver): void 223 224Registers an observer for the promise rejection. After the registration, a rejected promise that is not captured in the current thread of the application can be captured. 225 226**Atomic service API**: This API can be used in atomic services since API version 12. 227 228**System capability**: SystemCapability.Ability.AbilityRuntime.Core 229 230**Parameters** 231 232| Name | Type | Mandatory| Description | 233|-----------------------|-------------------------------------------------------------| -------- |------------------------------------------| 234| type | string | Yes| Event type. It is fixed at **'unhandledRejection'**, indicating an observer for the promise rejection.| 235| observer | [UnhandledRejectionObserver](#unhandledrejectionobserver12) | Yes| Observer to register. | 236 237**Error codes** 238 239For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 240 241| ID| Error Message| 242| ------- | -------- | 243| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3. Parameter verification failed. | 244| 16200001 | If the caller is invalid. | 245 246**Example** 247 248```ts 249import { errorManager } from '@kit.AbilityKit'; 250 251let observer: errorManager.UnhandledRejectionObserver = (reason: Error, promise: Promise<void>) => { 252 if (promise === promise1) { 253 console.log("promise1 is rejected"); 254 } 255 console.log("reason.name: ", reason.name); 256 console.log("reason.message: ", reason.message); 257 if (reason.stack) { 258 console.log("reason.stack: ", reason.stack); 259 } 260}; 261 262errorManager.on("unhandledRejection", observer); 263 264let promise1 = new Promise<void>(() => {}).then(() => { 265 throw new Error("uncaught error"); 266}); 267``` 268 269## errorManager.off('loopObserver')<sup>12+</sup> 270 271off(type: 'loopObserver', observer?: LoopObserver): void 272 273Unregisters an observer for the message processing duration of the main thread. 274 275**Atomic service API**: This API can be used in atomic services since API version 12. 276 277**System capability**: SystemCapability.Ability.AbilityRuntime.Core 278 279**Parameters** 280 281| Name| Type| Mandatory| Description| 282| -------- | -------- | -------- | -------- | 283| type | string | Yes| Event type. It is fixed at **'loopObserver'**, indicating an observer for the message processing duration of the main thread.| 284| observer | [LoopObserver](js-apis-inner-application-loopObserver.md) | No| Observer to unregister.| 285 286**Error codes** 287 288For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 289 290| ID| Error Message| 291| ------- | -------- | 292| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3. Parameter verification failed. | 293 294**Example** 295 296```ts 297import { errorManager } from '@kit.AbilityKit'; 298 299errorManager.off("loopObserver"); 300``` 301 302## errorManager.off('unhandledRejection')<sup>12+</sup> 303 304off(type: 'unhandledRejection', observer?: UnhandledRejectionObserver): void 305 306Unregisters an observer for the promise rejection. 307 308**Atomic service API**: This API can be used in atomic services since API version 12. 309 310**System capability**: SystemCapability.Ability.AbilityRuntime.Core 311 312**Parameters** 313 314| Name | Type | Mandatory| Description | 315|-----------------------|---------------------------------|----|----------------------------------------------| 316| type | string | Yes | Event type. It is fixed at **'unhandledRejection'**, indicating an observer for the promise rejection.| 317| observer | [UnhandledRejectionObserver](#unhandledrejectionobserver12) | No | Observer to unregister. | 318 319**Error codes** 320 321For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 322 323| ID| Error Message| 324| ------- | -------- | 325| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3. Parameter verification failed. | 326| 16200001 | If the caller is invalid. | 327| 16300004 | If the observer does not exist. | 328 329For details about the error codes, see [Ability Error Codes](errorcode-ability.md). 330 331**Example** 332 333```ts 334import { errorManager } from '@kit.AbilityKit'; 335 336let observer: errorManager.UnhandledRejectionObserver = (reason: Error, promise: Promise<void>) => { 337 if (promise === promise1) { 338 console.log("promise1 is rejected"); 339 } 340 console.log("reason.name: ", reason.name); 341 console.log("reason.message: ", reason.message); 342 if (reason.stack) { 343 console.log("reason.stack: ", reason.stack); 344 } 345}; 346 347errorManager.on("unhandledRejection", observer); 348 349let promise1 = new Promise<void>(() => {}).then(() => { 350 throw new Error("uncaught error") 351}) 352 353errorManager.off("unhandledRejection"); 354``` 355Or: 356```ts 357import { errorManager } from '@kit.AbilityKit'; 358 359let observer: errorManager.UnhandledRejectionObserver = (reason: Error, promise: Promise<void>) => { 360 if (promise === promise1) { 361 console.log("promise1 is rejected"); 362 } 363 console.log("reason.name: ", reason.name); 364 console.log("reason.message: ", reason.message); 365 if (reason.stack) { 366 console.log("reason.stack: ", reason.stack); 367 } 368}; 369 370errorManager.on("unhandledRejection", observer); 371 372let promise1 = new Promise<void>(() => {}).then(() => { 373 throw new Error("uncaught error") 374}) 375 376errorManager.off("unhandledRejection", observer); 377``` 378 379## UnhandledRejectionObserver<sup>12+</sup> 380 381type UnhandledRejectionObserver = (reason: Error | any, promise: Promise\<any>) => void 382 383Defines an observer to capture the cause of a rejected promise. 384 385**Atomic service API**: This API can be used in atomic services since API version 12. 386 387**System capability**: SystemCapability.Ability.AbilityRuntime.Core 388 389**Parameters** 390 391| Name | Type | Mandatory| Description| 392|--------|---------------|---| -------- | 393| reason | Error \| any | Yes| Generally, the value is of the **Error** type, indicating the reason for rejection.| 394| promise | Promise\<any> | Yes| Rejected promise.| 395