From 20c727ca0502f8a711bc545d04b77645b0d32f8d Mon Sep 17 00:00:00 2001 From: chenyongjian Date: Tue, 1 Apr 2025 18:10:15 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90feature=E3=80=91helloworld=E6=96=B0?= =?UTF-8?q?=E5=A2=9Ebusiness=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chenyongjian --- .../main/ets/entryability/EntryAbility.ets | 20 +++++++++- .../src/main/ets/models/MyBusinessError.ets | 39 +++++++++++++++++++ 2 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 code/HelloWorld/entry/src/main/ets/models/MyBusinessError.ets diff --git a/code/HelloWorld/entry/src/main/ets/entryability/EntryAbility.ets b/code/HelloWorld/entry/src/main/ets/entryability/EntryAbility.ets index 7080b7f8ea..aff4999576 100644 --- a/code/HelloWorld/entry/src/main/ets/entryability/EntryAbility.ets +++ b/code/HelloWorld/entry/src/main/ets/entryability/EntryAbility.ets @@ -2,8 +2,10 @@ import UIAbility from '@ohos.app.ability.UIAbility'; import AbilityConstant from '@ohos.app.ability.AbilityConstant'; import Want from '@ohos.app.ability.Want'; import window from '@ohos.window'; -import { BusinessError } from '@ohos.base' -import hilog from '@ohos.hilog' +import { BusinessError } from '@ohos.base'; +import hilog from '@ohos.hilog'; +import { MyBusinessError, MyCallback } from '../models/MyBusinessError'; + class EntryAbility extends UIAbility { onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { @@ -15,6 +17,20 @@ class EntryAbility extends UIAbility { try { windowStage.loadContent('pages/Index', (err: BusinessError): void => { hilog.info(0x0000, 'testTag', 'loadContent entering'); + MyCallback.myCallback((data: string) => { + hilog.info(0x0000, 'testTag', 'MyCallback' + data); + }) + MyCallback.myAsyncCallback((error: BusinessError, result: string) => { + hilog.info(0x0000, 'testTag', `myAsyncCallback, error: ${error.message}, result:${result}`); + }) + MyCallback.myErrorCallback((error: BusinessError) => { + hilog.info(0x0000, 'testTag', `myAsyncCallback, error: ${error.message}`); + }) + let be1 = MyBusinessError.basicError() + let be2 = MyBusinessError.basicError2() + hilog.info(0x0000, 'testTag', `basicError: ${be1}`); + hilog.info(0x0000, 'testTag', `asicError2: ${be2}`); + if (err.code) { hilog.info(0x0000, 'testTag', 'loadContent error'); return; diff --git a/code/HelloWorld/entry/src/main/ets/models/MyBusinessError.ets b/code/HelloWorld/entry/src/main/ets/models/MyBusinessError.ets new file mode 100644 index 0000000000..406221a96a --- /dev/null +++ b/code/HelloWorld/entry/src/main/ets/models/MyBusinessError.ets @@ -0,0 +1,39 @@ +import { AsyncCallback, BusinessError, Callback, ErrorCallback } from '@ohos.base'; +import hilog from '@ohos.hilog'; + +const SYNTAX_ERROR_CODE: double = 1002; + +export class MyBusinessError { + static basicError(): double { + let be: BusinessError = { code: SYNTAX_ERROR_CODE }; + return be.code; + } + + static basicError2(): string { + let error = new Error('Business Error', 'basicError2 message', undefined); + let be2: BusinessError = new BusinessError(SYNTAX_ERROR_CODE, error); + return be2.message; + } +} + +export class MyCallback { + static myCallback(callback: Callback): void { + hilog.info(0x0000, 'testTag', 'myCallback'); + callback('myCallback') + } + + static myAsyncCallback(callback: AsyncCallback): void { + hilog.info(0x0000, 'testTag', 'myAsyncCallback'); + let error = new Error('Business Error', 'basicError2 message', undefined); + let be2: BusinessError = new BusinessError(SYNTAX_ERROR_CODE, error); + callback(be2, 'yAsyncCallback') + } + + static myErrorCallback(callback: ErrorCallback>): void { + hilog.info(0x0000, 'testTag', 'myErrorCallback'); + let error = new Error('Business Error', 'myErrorCallback message', undefined); + let result: string = 'test'; + let be: BusinessError = new BusinessError(SYNTAX_ERROR_CODE, result, error); + callback(be); + } +} \ No newline at end of file -- Gitee