1# 应用质量提升案例-稳定性测试常见JS_ERROR问题分析与定位
2
3## 问题描述
4
5本文案例分析OpenHarmony开发的应用进行稳定性测试遇到的JS_ERROR,对其产生的原因进行分析与定位,并给出解决方案。
6
7几种常见的错误信息如下:
8
91)Obj is not a valid object :访问对象无效或不存在;
10
112)is not callable :调用方法不存在;
12
133)Internal error. UI execution context not found:未找到UI执行的上下文。
14
15
16
17## 案例分析
18
19### softwareUpdate.js出现JS_ERROR
20
21问题描述:
22
23系统稳定性测试10000分钟,com.ohos.settings 出现 17次JS_ERROR(softwareUpdate.js)。
24
25堆栈异常信息:
26
27```
28Module name:com.ohos.settings
29Pid:28150
30Uid:20010012
31Lifetime: 0.000000s
32Js-Engine: ark
33page: pages/softwareUpdate.js
34Error message: Obj is not a valid object
35Stacktrace:
36    at get (\\MainAbility\\pages\\softwareUpdate.ets:512:25)
37    at loading (./pages/softwareUpdate.js:2127:16)
38    at anonymous (./pages/softwareUpdate.js:2141:13)
39```
40
41问题定位:
42
43该问题提示错误类型:Obj is not a valid object。
44
45发生在调用 loading时get 操作,相关代码行数在commons.js中2141、2127、512等行数。
46
47![img](figures/jserror_1.png)
48
49问题原因:
50
51经过代码比对、流程分析、hilog校对等方式,确认该问题是由于偶发退出页面时未销毁setInterval定时器,导致后台程序仍访问前台page页面中的全局变量this.angle52
53通过模拟该场景可必现相同jserror日志。
54
55解决措施:
56
57对业务代码流程进行梳理,确保开启/关闭定时器操作成对出现,避免再次发生后台程序访问前台page页面中的全局变量。
58
59
60
61### notificationManage.js出现JS_ERROR
62
63问题描述:
64
65应用稳定性测试48小时,com.ohos.settings出现14次JS_ERROR(notificationManage.js)。
66
67堆栈异常信息:
68
69```
70Module name:com.ohos.settings
71Pid:17337
72Uid:20010012
73Lifetime: 0.000000s
74Js-Engine: ark
75page: pages/noticeApp/notificationManage.js
76Error message: Obj is not a valid object
77Stacktrace:
78    at initNotificationEnableInit (/pages/noticeApp/notificationManage.js:8479:16)
79    at initNotificationEnableInit (\pages\noticeApp\notificationManage.ets:31:5)
80    at anonymous (\pages\noticeApp\notificationManage.ets:20:7)
81```
82
83 问题定位:
84
85该问题提示错误类型:Obj is not a valid object。
86
87发生在调用initNotificationEnableInit 时,相关代码行数在notificationManage.ets中20、31行,commons.js中8479行。
88
89![img](figures/jserror_2.png)
90
91问题原因:
92
93经过代码比对、流程分析、hilog校对确认问题根因是notificationManage中调用initNotificationEnableInit 方法时参数非法,导致最终调用接口Notification.isNotificationEnabled时产生jserror。
94
95解决措施:
96
97对非法参数进行异常处理。
98
99
100
101### softwareUDiskUpdate.js出现JS_ERROR
102
103问题描述:
104
105系统稳定性测试10000分钟,com.ohos.settings 出现 1次JS_ERROR(softwareUDiskUpdate.js)。
106
107堆栈信息:
108
109```
110Module name:com.ohos.settings
111Pid:22221
112Uid:20010012
113Lifetime: 0.000000s
114Js-Engine: ark
115page: pages/softwareUDiskUpdate.js
116Error message: is not callable
117Stacktrace:
118    at anonymous (\\MainAbility\\pages\\softwareUDiskUpdate.ets:69:15)
119```
120
121 问题定位:该问题提示错误类型:is not callable。
122
123发生softwareUDiskUpdate.ets中,相关代码行数69行。
124
125![img](figures/jserror_3.png)
126
127问题原因:
128
129经过代码比对、流程分析、hilog校对确认问题根因是调用系统不存在的接口:UpdateSystemFromUDisk()。
130
131解决措施:
132
133此部分代码为冗余未删除代码,需要进行代码清理。
134
135
136
137### wifiPsd.js出现JS_ERROR
138
139问题描述:
140
141系统稳定性测试72小时,com.ohos.settings出现1次JS_ERROR:ohos_router_1.back142
143堆栈信息:
144
145```
146Module name:com.ohos.settings
147Version:2.0.0.002
148Pid:3995
149Uid:20010028
150Reason:Error
151Error message:Internal error. UI execution context not found.
152SourceCode:
153                _ohos_router_1.back({
154                ^
155Stacktrace:
156    at anonymous (D:/WorkSpace/Code/Common_Master_3.2/product_min_system/kh_applications_system/applications_settings/product/phone/build/default/intermediates/loader_out/default/ets/pages/wlan/wifiPsd_.js:1197:17)
157    at anonymous (D:/WorkSpace/Code/Common_Master_3.2/product_min_system/kh_applications_system/applications_settings/product/phone/build/default/intermediates/loader_out/default/ets/MainAbility/MainAbility_.js:2009:17)
158    at anonymous (D:/WorkSpace/Code/Common_Master_3.2/product_min_system/kh_applications_system/applications_settings/product/phone/build/default/intermediates/loader_out/default/ets/MainAbility/MainAbility_.js:2008:13)
159```
160
161 问题定位:
162
163该问题错误提示:UI execution context not found。
164
165发生在wifiPsd.js中,调用router.back()函数时出现异常。
166
167![img](figures/jserror_4.png)
168
169问题原因:
170
171当在异步回调中执行router.back()时,会产生已经不在当前页面的现象,导致router操作失败,例如:
172
1731)在A页面设置定时器setTimeOut 5秒后router到B页面;
174
1752)在5S内手动点击切换到其他非A页面上,等待A页面设置的定时器setTimeOut执行时,必现jserror。
176
177解决措施:
178
179对调用router.back()处代码添加try-catch保护。
180
181
182
183## 总结
184
185随着OpenHarmony生态的推广,对应的应用开发IDE、测试工具等也会不断完善,应用层面的错误信息也会披露的越来越清晰。大家在应用开发的过程中如果遇到JS_ERROR,可以通过阅读给出的堆栈或者log信息,结合代码来定位问题。
186
187希望本文能够为应用开发者在此方面提供一些参考。