harmony 鸿蒙@ohos.app.ability.errorManager (ErrorManager)

  • 2022-12-22
  • 浏览 (667)

@ohos.app.ability.errorManager (ErrorManager)

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.

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(type: ‘error’, observer: ErrorObserver)(deprecated)

on(type: ‘error’, observer: ErrorObserver): number;

Registers an error observer.

NOTE

This API is supported since API version 9 and deprecated since API version 10. You are advised to use ErrorManager.on(type: ‘errorEvent’, observer: ErrorObserver).

System capability: SystemCapability.Ability.AbilityRuntime.Core

Parameters

Name Type Mandatory Description
type string Yes Type of the API to call. 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(type: ‘error’, observerId: number, callback: AsyncCallback<void>)(deprecated)

off(type: ‘error’, observerId: number, callback: AsyncCallback<void>): void;

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

NOTE

This API is supported since API version 9 and deprecated since API version 10. You are advised to use ErrorManager.off(type: ‘errorEvent’, observerId: number).

System capability: SystemCapability.Ability.AbilityRuntime.Core

Parameters

Name Type Mandatory Description
type string Yes Type of the API to call. 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(type: ‘error’, observerId: number)(deprecated)

off(type: ‘error’, observerId: number): Promise<void>;

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

NOTE

This API is supported since API version 9 and deprecated since API version 10. You are advised to use ErrorManager.off(type: ‘errorEvent’, observerId: number).

System capability: SystemCapability.Ability.AbilityRuntime.Core

Parameters

Name Type Mandatory Description
type string Yes Type of the API to call. 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}`);
}

ErrorManager.on(type: ‘errorEvent’, observer: ErrorObserver)10+

on(type: ‘errorEvent’, observer: ErrorObserver): number;

Registers an error observer.

System capability: SystemCapability.Ability.AbilityRuntime.Core

Parameters

Name Type Mandatory Description
type string Yes Type of the API to call. It is fixed at ‘errorEvent’.
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('errorEvent', observer);
} catch (paramError) {
    let code = (paramError as BusinessError).code;
    let message = (paramError as BusinessError).message;
    console.error(`error: ${code}, ${message}`);
}

ErrorManager.off(type: ‘errorEvent’, observerId: number)10+

off(type: ‘errorEvent’, observerId: number): void;

Deregisters an error observer.

System capability: SystemCapability.Ability.AbilityRuntime.Core

Parameters

Name Type Mandatory Description
type string Yes Type of the API to call. It is fixed at ‘errorEvent’.
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;

try {
    errorManager.off('errorEvent', observerId);
} catch (paramError) {
    let code = (paramError as BusinessError).code;
    let message = (paramError as BusinessError).message;
    console.error(`error: ${code}, ${message}`);
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙APIs

harmony 鸿蒙System Common Events (To Be Deprecated Soon)

harmony 鸿蒙System Common Events

harmony 鸿蒙API Reference Document Description

harmony 鸿蒙Enterprise Device Management Overview (for System Applications Only)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager)

harmony 鸿蒙@ohos.distributedBundle (Distributed Bundle Management)

harmony 鸿蒙@ohos.bundle (Bundle)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (EnterpriseAdminExtensionAbility)

0  赞