• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the 'License');
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an 'AS IS' BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import { errorManager } from '@kit.AbilityKit';
17import { hilog } from '@kit.PerformanceAnalysisKit';
18import { promptAction } from '@kit.ArkUI';
19
20const TAG: string = 'ErrorManager';
21const DOMAIN: number = 0xFF00;
22
23let observer: errorManager.LoopObserver = {
24  onLoopTimeOut(timeout: number) {
25    hilog.info(DOMAIN, TAG, `onLoopTimeOut, timeout: ${timeout}`)
26    errorManager.off('loopObserver');
27  }
28};
29
30@Entry
31@Component
32struct ErrorManager {
33  private promptDuration: number = 2000;
34  private loopTimeOut: number = 20;
35  @State unhandledRejectionRegistered: boolean = false;
36
37  build() {
38    Column() {
39      Row() {
40        Flex({ justifyContent: FlexAlign.Start, alignContent: FlexAlign.Center }) {
41          Text($r('app.string.ErrorManager'))
42            .fontSize(30)
43            .fontWeight(700)
44            .textAlign(TextAlign.Start)
45            .margin({
46              top: 8,
47              bottom: 8,
48              left: 12
49            })
50        }
51      }
52      .width('100%')
53      .height('14.36%')
54      .justifyContent(FlexAlign.Start)
55      .backgroundColor($r('app.color.backGrounding'))
56
57      List({ initialIndex: 0 }) {
58        ListItem() {
59          Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) {
60            Text($r('app.string.registerLoopObserver'))
61              .textAlign(TextAlign.Start)
62              .fontWeight(500)
63              .margin({
64                top: 17,
65                bottom: 17,
66                left: 12
67              })
68              .fontSize(16)
69              .width('77.87%')
70              .height('39.29%')
71              .fontColor($r('app.color.text_color'))
72          }
73          .id('registerLoopObserver')
74          .onClick(() => {
75            ((): void => {
76              try {
77                errorManager.on('loopObserver', this.loopTimeOut, observer);
78                let msg = `loopObserver registered successfully, loopTimeOut: ${this.loopTimeOut}`;
79                hilog.info(DOMAIN, TAG, msg);
80                promptAction.showToast({
81                  message: msg,
82                  duration: this.promptDuration
83                })
84              } catch (error) {
85                hilog.info(DOMAIN, TAG, `errorManager.on('loopObserver') failed, error: ${JSON.stringify(error)}`);
86              }
87            })()
88          })
89        }
90        .height('8.45%')
91        .backgroundColor($r('app.color.start_window_background'))
92        .borderRadius(24)
93        .margin({ top: 12, right: 12, left: 12 })
94
95        ListItem() {
96          Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) {
97            Text(this.unhandledRejectionRegistered ?
98            $r('app.string.unregisterUnhandledRejection') : $r('app.string.registerUnhandledRejection'))
99              .textAlign(TextAlign.Start)
100              .fontWeight(500)
101              .margin({
102                top: 17,
103                bottom: 17,
104                left: 12
105              })
106              .fontSize(16)
107              .width('77.87%')
108              .height('39.29%')
109              .fontColor($r('app.color.text_color'))
110          }
111          .id(this.unhandledRejectionRegistered ? ('unregisterUnhandledRejection') : ('registerUnhandledRejection'))
112          .onClick(() => {
113            ((): void => {
114              if (this.unhandledRejectionRegistered == false) {
115                let observer: errorManager.UnhandledRejectionObserver = (reason: Error, promise: Promise<void>) => {
116                  let msg = `UnhandledRejectionObserver reason: ${JSON.stringify(reason)}`;
117                  hilog.info(DOMAIN, TAG, msg);
118                  promptAction.showToast({
119                    message: msg,
120                    duration: this.promptDuration
121                  })
122                };
123
124                try {
125                  errorManager.on('unhandledRejection', observer);
126                  this.unhandledRejectionRegistered = true;
127                  let msg = `unhandledRejection registered successfully`;
128                  hilog.info(DOMAIN, TAG, msg);
129                  promptAction.showToast({
130                    message: msg,
131                    duration: this.promptDuration
132                  })
133                } catch (error) {
134                  hilog.info(DOMAIN, TAG,
135                    `errorManager.on('unhandledRejection') failed, error: ${JSON.stringify(error)}`);
136                  this.unhandledRejectionRegistered = false;
137                }
138
139              } else {
140                try {
141                  errorManager.off('unhandledRejection');
142                  this.unhandledRejectionRegistered = false;
143                  let msg: string = `unhandledRejection unregistered successfully`;
144                  hilog.info(DOMAIN, TAG, msg);
145                  promptAction.showToast({
146                    message: msg,
147                    duration: this.promptDuration
148                  })
149                } catch (error) {
150                  this.unhandledRejectionRegistered = true;
151                  hilog.info(DOMAIN, TAG, `unhandledRejection unregistered failed, error: ${JSON.stringify(error)}`);
152                }
153              }
154            })()
155          })
156        }
157        .height('8.45%')
158        .backgroundColor($r('app.color.start_window_background'))
159        .borderRadius(24)
160        .margin({ top: 12, right: 12, left: 12 })
161
162        if (this.unhandledRejectionRegistered) {
163          ListItem() {
164            Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) {
165              Text($r('app.string.simulateUnhandledRejection'))
166                .textAlign(TextAlign.Start)
167                .fontWeight(500)
168                .margin({
169                  top: 17,
170                  bottom: 17,
171                  left: 12
172                })
173                .fontSize(16)
174                .width('77.87%')
175                .height('39.29%')
176                .fontColor($r('app.color.text_color'))
177            }
178            .id('simulateUnhandledRejection')
179            .onClick(() => {
180              Promise.reject({ name: 'test', message: 'simulateUnhandledRejection' });
181            })
182          }
183          .height('8.45%')
184          .backgroundColor($r('app.color.start_window_background'))
185          .borderRadius(24)
186          .margin({ top: 12, right: 12, left: 12 })
187        }
188      }
189      .height('86%')
190      .backgroundColor($r('app.color.backGrounding'))
191    }
192    .height('100%')
193    .width('100%')
194  }
195}