Skip to content

Commit 49081da

Browse files
coder83469pangyinqiang
andauthored
feat: 修改底层异常处理逻辑,将跳转错误页面改为弹出自定义弹窗 (#14)
Co-authored-by: pangyinqiang <pangyinqiang1@h-partners.com>
1 parent 4327089 commit 49081da

File tree

3 files changed

+150
-101
lines changed

3 files changed

+150
-101
lines changed

harmony/exception_handler/src/main/ets/ExceptionComponent.ets

Lines changed: 0 additions & 101 deletions
This file was deleted.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (C) 2023 Huawei Device Co., Ltd.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
import {RNPackage, TurboModulesFactory} from '@rnoh/react-native-openharmony/ts';
26+
import type {TurboModule, TurboModuleContext} from '@rnoh/react-native-openharmony/ts';
27+
import {ExceptionHandlerTurboModule} from './ExceptionHandlerTurboModule';
28+
29+
class ExceptionHandlerTurboModulesFactory extends TurboModulesFactory {
30+
createTurboModule(name: string): TurboModule | null {
31+
if (name === 'ExceptionHandlerTurboModule') {
32+
globalThis.uiAbilityContext = this.ctx.uiAbilityContext;
33+
return new ExceptionHandlerTurboModule(this.ctx);
34+
}
35+
return null;
36+
}
37+
38+
hasTurboModule(name: string): boolean {
39+
return name === 'ExceptionHandlerTurboModule';
40+
}
41+
}
42+
43+
export class ExceptionHandlerPackage extends RNPackage {
44+
createTurboModulesFactory(ctx: TurboModuleContext): TurboModulesFactory {
45+
return new ExceptionHandlerTurboModulesFactory(ctx);
46+
}
47+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/**
2+
* MIT License
3+
*
4+
* Copyright (C) 2024 Huawei Device Co., Ltd.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
import { TurboModule, TurboModuleContext } from "@rnoh/react-native-openharmony/ts";
25+
import { ComponentContent, PromptAction, promptAction, window } from '@kit.ArkUI';
26+
import { common, errorManager } from '@kit.AbilityKit';
27+
28+
let observerIds: number[] = [];
29+
interface RealViewPointType {
30+
'error': string;
31+
};
32+
33+
@Builder
34+
function buildErrorDialog(errorMsg: ESObject) {
35+
Column() {
36+
Text('Unexpected error occured')
37+
.fontSize(24)
38+
.fontStyle(FontStyle.Normal)
39+
.baselineOffset(-40)
40+
.textAlign(TextAlign.Center)
41+
.width("80%")
42+
Text('Appologies..The app will close now Please restart the app')
43+
.baselineOffset(-5)
44+
.textAlign(TextAlign.Center)
45+
.width("80%")
46+
Text(errorMsg.error)
47+
.textAlign(TextAlign.Center)
48+
.baselineOffset(-5)
49+
.width("80%")
50+
}.backgroundColor('#ffffff').width("80%").height("80%").borderRadius(10)
51+
}
52+
53+
export class ExceptionHandlerTurboModule extends TurboModule {
54+
constructor(ctx: TurboModuleContext) {
55+
super(ctx)
56+
}
57+
58+
setHandlerForNativeException(handler: (errMsg: string) => void, executeDefaultHandler?: boolean): void {
59+
if (!executeDefaultHandler) {
60+
if (observerIds.length > 0) {
61+
observerIds.forEach(id => {
62+
errorManager.off('error', id);
63+
})
64+
}
65+
}
66+
67+
const uiAbilityContext: common.UIAbilityContext = this.ctx.uiAbilityContext
68+
let observer: errorManager.ErrorObserver = {
69+
onUnhandledException(errorMsg) {
70+
handler(errorMsg)
71+
console.error('onUnhandledException, errorMsg: ', errorMsg);
72+
const realViewPoint: RealViewPointType = {
73+
error:errorMsg
74+
}
75+
window.getLastWindow(uiAbilityContext)
76+
.then((value) => {
77+
const uiContext: UIContext = value.getUIContext();
78+
const promptAction: PromptAction = uiContext?.getPromptAction();
79+
const contentNode: ComponentContent<RealViewPointType> =
80+
new ComponentContent(uiContext!, wrapBuilder(buildErrorDialog), realViewPoint);
81+
const options: promptAction.BaseDialogOptions = {
82+
autoCancel: false
83+
}
84+
try {
85+
promptAction?.openCustomDialog(contentNode, options);
86+
} catch (error) {
87+
console.error(`eroor`);
88+
}
89+
})
90+
}
91+
};
92+
try {
93+
const observerId: number = errorManager.on('error', observer);
94+
observerIds.push(observerId);
95+
} catch (error) {
96+
console.error(`registerErrorObserver failed, error.code: ${error.code}, error.message: ${error.message}`);
97+
}
98+
}
99+
}
100+
101+
102+
103+

0 commit comments

Comments
 (0)