From f96c778cb8cadbdb944112110d640a7d0946cd88 Mon Sep 17 00:00:00 2001 From: lanhaoyu Date: Tue, 19 Aug 2025 20:18:15 +0800 Subject: [PATCH] 1.2ets add throw error Signed-off-by: lanhaoyu --- .../@ohos.bundle.distributedBundleManager.ets | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/interfaces/kits/ani/distributed_bundle_manager/ets/@ohos.bundle.distributedBundleManager.ets b/interfaces/kits/ani/distributed_bundle_manager/ets/@ohos.bundle.distributedBundleManager.ets index 19515e8..35bb7e7 100644 --- a/interfaces/kits/ani/distributed_bundle_manager/ets/@ohos.bundle.distributedBundleManager.ets +++ b/interfaces/kits/ani/distributed_bundle_manager/ets/@ohos.bundle.distributedBundleManager.ets @@ -25,6 +25,39 @@ import { RemoteAbilityInfo as _RemoteAbilityInfo } from 'bundleManager.RemoteAbi export default namespace distributedBundleManager { loadLibrary("ani_distributed_bundle_manager.z"); + const ERROR_PARAM_CHECK_ERROR: int = 401; + const GET_REMOTE_ABILITY_INFO_MAX_SIZE: int = 10; + const ELEMENT_NAMES_SIZE_ERROR: string = "BusinessError 401: The number of ElementNames is greater than 10"; + const ELEMENT_NAME_ERROR: string = "BusinessError 401: Parameter error. The type of elementName must be object."; + + function createBusinessError(code: int, message: string) { + let err = new BusinessError(); + err.code = code; + err.name = 'Error'; + err.message = message; + return err; + } + + function checkElementName(elementName: ElementName) { + if (elementName.deviceId === undefined) { + throw createBusinessError(ERROR_PARAM_CHECK_ERROR, ELEMENT_NAME_ERROR); + } + } + + function checkElementNames(elementNames: Array) { + if (elementNames.length === 0) { + throw createBusinessError(ERROR_PARAM_CHECK_ERROR, ELEMENT_NAME_ERROR); + } + for (const elementName of elementNames) { + if (elementName.deviceId === undefined) { + throw createBusinessError(ERROR_PARAM_CHECK_ERROR, ELEMENT_NAME_ERROR); + } + } + if (elementNames.length > GET_REMOTE_ABILITY_INFO_MAX_SIZE) { + throw createBusinessError(ERROR_PARAM_CHECK_ERROR, ELEMENT_NAMES_SIZE_ERROR); + } + } + export native function getRemoteAbilityInfoNative(elementNames: Array, locale: string): RemoteAbilityInfo; export native function getRemoteAbilityInfosNative(elementNames: Array, locale: string): Array; @@ -35,6 +68,7 @@ export default namespace distributedBundleManager { } function getRemoteAbilityInfo(elementName: ElementName, callback: AsyncCallback): void { + checkElementName(elementName); let cb = (): RemoteAbilityInfo => { return distributedBundleManager.getRemoteAbilityInfoNative(elementName2Array(elementName), ''); }; @@ -48,6 +82,7 @@ export default namespace distributedBundleManager { } function getRemoteAbilityInfo(elementName: ElementName): Promise { + checkElementName(elementName); let p = new Promise((resolve: (remoteAbilityInfo: RemoteAbilityInfo) => void, reject: (error: BusinessError) => void) => { let cb = (): RemoteAbilityInfo => { return distributedBundleManager.getRemoteAbilityInfoNative(elementName2Array(elementName), ''); @@ -64,6 +99,7 @@ export default namespace distributedBundleManager { } function getRemoteAbilityInfo(elementNames: Array, callback: AsyncCallback>): void { + checkElementNames(elementNames); let cb = (): (Array) => { return distributedBundleManager.getRemoteAbilityInfosNative(elementNames, ''); }; @@ -77,6 +113,7 @@ export default namespace distributedBundleManager { } function getRemoteAbilityInfo(elementNames: Array): Promise> { + checkElementNames(elementNames); let p = new Promise>((resolve: (arrRemoteAbilityInfo: Array) => void, reject: (error: BusinessError) => void) => { let cb = (): (Array) => { return distributedBundleManager.getRemoteAbilityInfosNative(elementNames, ''); @@ -93,6 +130,7 @@ export default namespace distributedBundleManager { } function getRemoteAbilityInfo(elementName: ElementName, locale: string, callback: AsyncCallback): void { + checkElementName(elementName); let cb = (): RemoteAbilityInfo => { return distributedBundleManager.getRemoteAbilityInfoNative(elementName2Array(elementName), locale); }; @@ -106,6 +144,7 @@ export default namespace distributedBundleManager { } function getRemoteAbilityInfo(elementName: ElementName, locale: string): Promise { + checkElementName(elementName); let p = new Promise((resolve: (remoteAbilityInfo: RemoteAbilityInfo) => void, reject: (error: BusinessError) => void) => { let cb = (): RemoteAbilityInfo => { return distributedBundleManager.getRemoteAbilityInfoNative(elementName2Array(elementName), locale); @@ -122,6 +161,7 @@ export default namespace distributedBundleManager { } function getRemoteAbilityInfo(elementNames: Array, locale: string, callback: AsyncCallback>): void { + checkElementNames(elementNames); let cb = (): (Array) => { return distributedBundleManager.getRemoteAbilityInfosNative(elementNames, locale); }; @@ -135,6 +175,7 @@ export default namespace distributedBundleManager { } function getRemoteAbilityInfo(elementNames: Array, locale: string): Promise> { + checkElementNames(elementNames); let p = new Promise>((resolve: (arrRemoteAbilityInfo: Array) => void, reject: (error: BusinessError) => void) => { let cb = (): (Array) => { return distributedBundleManager.getRemoteAbilityInfosNative(elementNames, locale); -- Gitee