@ohos.app.ability.errorManager (ErrorManager)
The ErrorManager module provides APIs for registering and unregistering error observers.
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
import errorManager from '@ohos.app.ability.errorManager';
ErrorManager.on
on(type: 'error', observer: ErrorObserver): number
Registers 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.
System capability: SystemCapability.Ability.AbilityRuntime.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
type | string | Yes | Event type. It is fixed at "error". |
observer | ErrorObserver | Yes | Digital code of the observer. |
Return value
Type | Description |
---|---|
number | Index of the observer. |
Error codes
ID | Error Message |
---|---|
16000003 | Id does not exist. |
For details about the error codes, see Ability Error Codes.
Example
import errorManager from '@ohos.app.ability.errorManager';
import { BusinessError } from '@ohos.base';
let observer: errorManager.ErrorObserver = {
onUnhandledException(errorMsg) {
console.log('onUnhandledException, errorMsg: ', errorMsg);
},
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);
}
}
};
let observerId = -1;
try {
observerId = errorManager.on('error', observer);
} catch (paramError) {
let code = (paramError as BusinessError).code;
let message = (paramError as BusinessError).message;
console.error(`error: ${code}, ${message}`);
}
ErrorManager.off
off(type: 'error', observerId: number, callback: AsyncCallback<void>): void
Unregisters an error observer. This API uses an asynchronous callback to return the result.
System capability: SystemCapability.Ability.AbilityRuntime.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
type | string | Yes | Event type. It is fixed at "error". |
observerId | number | Yes | Index of the observer returned by on(). |
callback | AsyncCallback<void> | Yes | Callback used to return the result. |
Error codes
ID | Error Message |
---|---|
16000003 | Id does not exist. |
For details about the error codes, see Ability Error Codes.
Example
import errorManager from '@ohos.app.ability.errorManager';
import { BusinessError } from '@ohos.base';
let observerId = 100;
function unregisterErrorObserverCallback(err: BusinessError) {
if (err) {
console.error('------------ unregisterErrorObserverCallback ------------', err);
}
}
try {
errorManager.off('error', observerId, unregisterErrorObserverCallback);
} catch (paramError) {
let code = (paramError as BusinessError).code;
let message = (paramError as BusinessError).message;
console.error(`error: ${code}, ${message}`);
}
ErrorManager.off
off(type: 'error', observerId: number): Promise<void>
Unregisters an error observer. This API uses a promise to return the result.
System capability: SystemCapability.Ability.AbilityRuntime.Core
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
type | string | Yes | Event type. It is fixed at "error". |
observerId | number | Yes | Index of the observer returned by on(). |
Return value
Type | Description |
---|---|
Promise<void> | Promise used to return the result. |
Error codes
ID | Error Message |
---|---|
16000003 | Id does not exist. |
For details about the error codes, see Ability Error Codes.
Example
import errorManager from '@ohos.app.ability.errorManager';
import { BusinessError } from '@ohos.base';
let observerId = 100;
try {
errorManager.off('error', observerId)
.then((data) => {
console.log('----------- unregisterErrorObserver success ----------', data);
})
.catch((err: BusinessError) => {
console.error('----------- unregisterErrorObserver fail ----------', err);
});
} catch (paramError) {
let code = (paramError as BusinessError).code;
let message = (paramError as BusinessError).message;
console.error(`error: ${code}, ${message}`);
}