From 10fbe14f0b2e1c848533fc33d4d87a444532df25 Mon Sep 17 00:00:00 2001 From: rekirt Date: Tue, 8 Apr 2025 16:42:30 +0800 Subject: [PATCH] fix exception not catch Signed-off-by: rekirt --- README.en.md | 7 ++- README.md | 7 ++- .../main/ets/entryability/EntryAbility.ets | 54 +++++++++++-------- 3 files changed, 42 insertions(+), 26 deletions(-) diff --git a/README.en.md b/README.en.md index 099e850..6b129cf 100644 --- a/README.en.md +++ b/README.en.md @@ -19,8 +19,11 @@ Instructions: ``` ├──entry/src/main/ets/ -│ ├──common/constant -│ │ └──CommonConstants.ets // Common constant class +│ ├──common +│ │ ├──constants +│ │ │ └─CommonConstants.ets // Common constant class +│ │ └──utils +│ │ └──Utils.ets // Utils class │ ├──entryability │ │ └──EntryAbility.ets // Entry point class │ ├──page diff --git a/README.md b/README.md index 0165fd8..4e5a219 100644 --- a/README.md +++ b/README.md @@ -19,8 +19,11 @@ ``` ├──entry/src/main/ets/ -│ ├──common/constant -│ │ └──CommonConstants.ets // 公共常量类 +│ ├──common +│ │ ├──constants +│ │ │ └─CommonConstants.ets // 公共常量类 +│ │ └──utils +│ │ └──Utils.ets // 工具类 │ ├──entryability │ │ └──EntryAbility.ets // 程序入口类 │ ├──page diff --git a/entry/src/main/ets/entryability/EntryAbility.ets b/entry/src/main/ets/entryability/EntryAbility.ets index 9deb8c9..83ad01f 100644 --- a/entry/src/main/ets/entryability/EntryAbility.ets +++ b/entry/src/main/ets/entryability/EntryAbility.ets @@ -38,15 +38,19 @@ export default class EntryAbility extends UIAbility { AppStorage.setOrCreate('currentBreakpoint', this.curBp); } } catch (exception) { - hilog.error(0x0000, 'testTag', - `UpdateBreakpoint fail, error code: ${JSON.stringify((exception as BusinessError).code)}`); + hilog.error(0x0000, 'testTag', '%{public}s', + `UpdateBreakpoint fail, error code: ${(exception as BusinessError).code}`); } } onCreate(): void { hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); AppStorage.setOrCreate('systemColorMode', this.context.config.colorMode); - this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); + try { + this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); + } catch (err) { + hilog.error(0x0000, 'testTag', '%{public}s', `setColorMode fail. error code: ${err.code}`); + } } onDestroy(): void { @@ -56,17 +60,21 @@ export default class EntryAbility extends UIAbility { onWindowStageCreate(windowStage: window.WindowStage): void { windowStage.getMainWindow().then((windowObj: window.Window) => { let type = window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR; - let avoidArea = windowObj.getWindowAvoidArea(type); - let bottomRectHeight = avoidArea.bottomRect.height; - AppStorage.setOrCreate('bottomRectHeight', bottomRectHeight); - type = window.AvoidAreaType.TYPE_SYSTEM; - avoidArea = windowObj.getWindowAvoidArea(type); - let topRectHeight = avoidArea.topRect.height; - AppStorage.setOrCreate('topRectHeight', topRectHeight); - this.updateBreakpoint(windowObj.getWindowProperties().windowRect.width); - windowObj.on('windowSizeChange', (windowSize) => { - this.updateBreakpoint(windowSize.width); - }) + try { + let avoidArea = windowObj.getWindowAvoidArea(type); + let bottomRectHeight = avoidArea.bottomRect.height; + AppStorage.setOrCreate('bottomRectHeight', bottomRectHeight); + type = window.AvoidAreaType.TYPE_SYSTEM; + avoidArea = windowObj.getWindowAvoidArea(type); + let topRectHeight = avoidArea.topRect.height; + AppStorage.setOrCreate('topRectHeight', topRectHeight); + this.updateBreakpoint(windowObj.getWindowProperties().windowRect.width); + windowObj.on('windowSizeChange', (windowSize) => { + this.updateBreakpoint(windowSize.width); + }); + } catch (err) { + hilog.error(0x0000, 'testTag', '%{public}s', `config window fail. Cause: ${err.code}`); + } }); // Main window is created, set main page for this ability hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); @@ -75,16 +83,18 @@ export default class EntryAbility extends UIAbility { AppStorage.setOrCreate('resourceManager', resourceManager); windowStage.loadContent('pages/Index', (err) => { if (err.code) { - hilog.error(0x0000, 'Failed to load the content. Cause: %{public}s', JSON.stringify(err.code) ?? ''); + hilog.error(0x0000, 'testTag', '%{public}s', `Failed to load the content. Cause: ${err.code}`); return; } - let windowClass: window.Window = windowStage.getMainWindowSync(); - let isLayoutFullScreen = true; - windowClass.setWindowLayoutFullScreen(isLayoutFullScreen).then(() => { - hilog.info(0x0000, 'testTag', 'Succeeded in setting the window layout to full-screen mode.'); - }).catch((err: BusinessError) => { - hilog.error(0x0000, 'Failed to set the window layout to full-screen mode.', JSON.stringify(err.code)); - }); + try { + let windowClass: window.Window = windowStage.getMainWindowSync(); + let isLayoutFullScreen = true; + windowClass.setWindowLayoutFullScreen(isLayoutFullScreen).then(() => { + hilog.info(0x0000, 'testTag', '%{public}s', 'Succeeded in setting the window layout to full-screen mode.'); + }); + } catch (err) { + hilog.error(0x0000, 'testTag', '%{public}s', `Failed to set the window layout to full-screen mode. ${err.code}`); + } }); } -- Gitee