1e41f4b71Sopenharmony_ci# Analyzing JS Crash 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciWhen an application crashes dues to a JS exception, it generates a JS crash log file. You can view the log to locate the error code and analyze the cause of the crash. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ciThis topic describes the JS Crash exception capture scenario, JS Crash fault analysis, and typical cases. 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci## JS Crash Detection 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci### JS Crash Log Specifications 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ciThe following describes the fields in a process crash log. 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci``` 14e41f4b71Sopenharmony_ciBuild info:XXX-XXXX X.X.X.XX(XXXXXXXX) <- Build information 15e41f4b71Sopenharmony_ciModule name:com.example.myapplication <- Module name 16e41f4b71Sopenharmony_ciVersion:1.0.0 <- Version number 17e41f4b71Sopenharmony_ciPid:579 <- Process ID 18e41f4b71Sopenharmony_ciUid:0 <- User ID 19e41f4b71Sopenharmony_ciReason:TypeError <- Cause 20e41f4b71Sopenharmony_ciError message:Cannot read property c of undefined <- Error message 21e41f4b71Sopenharmony_ciCannot get SourceMap info, dump raw stack: <- The release package does not contain the **SourceMap** file, and the JavaScript stack fails to parse it. 22e41f4b71Sopenharmony_ciSourceCode: 23e41f4b71Sopenharmony_ci var a = b.c; <- Location of the problematic code 24e41f4b71Sopenharmony_ci ^ 25e41f4b71Sopenharmony_ciStacktrace: 26e41f4b71Sopenharmony_ci at onPageShow (entry/src/main/ets/pages/Index.ets:7:13) <- Call stack of the error code 27e41f4b71Sopenharmony_ci ^ ^ 28e41f4b71Sopenharmony_ci The error occurs at line 7, column 13 in the entry/src/main/ets/pages/Index.ets file. 29e41f4b71Sopenharmony_ci``` 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ciYou can identify the cause of the crash, mostly application issues, based on Error message and Stacktrace in the logs. 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ci### JS Crash Exception Types 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ciJS crash exceptions are classified into the following types in the **Reason** field based on exception scenarios: 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci - **Error**: **Error** is the most basic type. Other error types are inherited from this type. The **Error** object consists of **message** and **name**, which indicate the error message and error name, respectively. Generally, exceptions of the **Error** type are thrown by developers. 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci - **TypeError**: As the most common error type at run-time, **TypeError** indicates a variable or parameter that is not of the expected type. 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci - **SyntaxError**: **SyntaxError** is also called parsing error. As the most common error type in all programming languages, **SyntaxError** indicates that the syntax does not comply with the syntax specifications of the programming language. 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ci - **RangeError**: **RangeError** is thrown when a value exceeds the valid range. Common range errors include the following: 44e41f4b71Sopenharmony_ci - The array length is negative or too long. 45e41f4b71Sopenharmony_ci - The numeric parameter exceeds the predefined range. 46e41f4b71Sopenharmony_ci - The number of function stack calls exceeds the maximum. 47e41f4b71Sopenharmony_ci 48e41f4b71Sopenharmony_ci - **ReferenceError**: **ReferenceError** is thrown when a variable that does not exist is referenced. Each time a variable is created, the variable name and its value are stored in the key-value format. When a variable is referenced, the value will be located based on the key and returned. If the variable referenced cannot be to be found, **ReferenceError** is thrown. 49e41f4b71Sopenharmony_ci 50e41f4b71Sopenharmony_ci - **URI Error**: **URI Error** is thrown when an invalid URI is found in **encodeURI()**, **decodeURI()**, **encodeURIComponent()**, **decodeURIComponent()**, **escape()**, or **unescape()**. 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ci## JS Crash Fault Analysis 53e41f4b71Sopenharmony_ci 54e41f4b71Sopenharmony_ci### Obtaining the Log 55e41f4b71Sopenharmony_ci 56e41f4b71Sopenharmony_ciThe process crash log is a type of fault log managed together with the app freeze and JS application crash logs by the FaultLogger module. You can obtain process crash logs using any of the following methods: 57e41f4b71Sopenharmony_ci 58e41f4b71Sopenharmony_ci- Method 1: DevEco Studio 59e41f4b71Sopenharmony_ci 60e41f4b71Sopenharmony_ci DevEco Studio collects process crash logs in **/data/log/faultlog/faultlogger/** and archives the logs in FaultLog. For details, see <!--RP1-->[DevEco Studio User Guide-FaultLog](https://developer.huawei.com/consumer/en/doc/harmonyos-guides-V5/ide-fault-log-V5)<!--RP1End-->. 61e41f4b71Sopenharmony_ci 62e41f4b71Sopenharmony_ci- Method 2: hiAppEvent APIs 63e41f4b71Sopenharmony_ci 64e41f4b71Sopenharmony_ci hiAppEvent provides APIs to subscribe to various fault logs. For details, see [Introduction to HiAppEvent](hiappevent-intro.md). 65e41f4b71Sopenharmony_ci 66e41f4b71Sopenharmony_ci<!--Del--> 67e41f4b71Sopenharmony_ci- Method 3: Shell 68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_ci When a process crashes, you can find fault logs in **/data/log/faultlog/faultlogger/** on the device. The log files are named in the format of **jscrash-process name-process UID-time (seconds)**. They contain information such as the device name, system version, and process crash call stack. 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_ci  72e41f4b71Sopenharmony_ci<!--DelEnd--> 73e41f4b71Sopenharmony_ci 74e41f4b71Sopenharmony_ci### Analyzing Faults 75e41f4b71Sopenharmony_ci 76e41f4b71Sopenharmony_ciGenerally, the cause of the fault can be found by locating the problematic code based on the exception scenario, error message, and call stack. The call stack is analyzed in the following cases: 77e41f4b71Sopenharmony_ci 78e41f4b71Sopenharmony_ci#### 1. StackTrace Scenarios 79e41f4b71Sopenharmony_ci 80e41f4b71Sopenharmony_ciIn JS Crash fault logs, the **StackTrace** field provides the call stack information about the JS Crash exception. Common **StackTrace** information includes the following: 81e41f4b71Sopenharmony_ci 82e41f4b71Sopenharmony_ci1. The stack top indicates the problematic code, as shown in the following example. You can click the link to locate the problematic code. 83e41f4b71Sopenharmony_ci ``` 84e41f4b71Sopenharmony_ci Device info:xxx 85e41f4b71Sopenharmony_ci Build info:xxx-xxx x.x.x.xxx(xxxx) 86e41f4b71Sopenharmony_ci Fingerprint:ed1811f3f5ae13c7262b51aab73ddd01df95b2c64466a204e0d70e6461cf1697 87e41f4b71Sopenharmony_ci Module name:com.xxx.xxx 88e41f4b71Sopenharmony_ci Version:1.0.0 89e41f4b71Sopenharmony_ci VersionCode:1000000 90e41f4b71Sopenharmony_ci PreInstalled:No 91e41f4b71Sopenharmony_ci Foreground:Yes 92e41f4b71Sopenharmony_ci Pid:31255 93e41f4b71Sopenharmony_ci Uid:20020145 94e41f4b71Sopenharmony_ci Reason:Error 95e41f4b71Sopenharmony_ci Error name:Error 96e41f4b71Sopenharmony_ci Error message:JSERROR 97e41f4b71Sopenharmony_ci Sourcecode: 98e41f4b71Sopenharmony_ci throw new ErrOr("JSERROR"); 99e41f4b71Sopenharmony_ci ^ 100e41f4b71Sopenharmony_ci Stacktrace: 101e41f4b71Sopenharmony_ci at anonymous (entry/src/main/ets/pages/Index.ets:13:19) 102e41f4b71Sopenharmony_ci ``` 103e41f4b71Sopenharmony_ci 104e41f4b71Sopenharmony_ci2. If "Stack Cannot get SourceMap info, dump raw stack" is displayed in the call stack, as shown in the following example, the system fails to retrieve information from SourceMap and only displays the row number of the problematic code in the compiled code in an eTS stack. You can click the link to identify where the error occurred in the compiled code. 105e41f4b71Sopenharmony_ci ``` 106e41f4b71Sopenharmony_ci Device info:xxx 107e41f4b71Sopenharmony_ci Build info:xxx-xxx x.x.x.xxx(xxxx) 108e41f4b71Sopenharmony_ci Fingerprint:a370fceb59011d96e41e97bda139b1851c911012ab8c386d1a2d63986d6d226d 109e41f4b71Sopenharmony_ci Module name:com.xxx.xxx 110e41f4b71Sopenharmony_ci Version:1.0.0 111e41f4b71Sopenharmony_ci Versioncode:1000000 112e41f4b71Sopenharmony_ci PreInstalled:No 113e41f4b71Sopenharmony_ci Foreground:Yes 114e41f4b71Sopenharmony_ci Pid:39185 115e41f4b71Sopenharmony_ci Uid:20020145 116e41f4b71Sopenharmony_ci Reason:Error 117e41f4b71Sopenharmony_ci Error name:Error 118e41f4b71Sopenharmony_ci Error message:JSERROR 119e41f4b71Sopenharmony_ci Stacktrace: 120e41f4b71Sopenharmony_ci Cannot get SourceMap info, dump raw stack: 121e41f4b71Sopenharmony_ci at anonymous (entry/src/main/ets/paqes/Index.ts:49:49) 122e41f4b71Sopenharmony_ci ``` 123e41f4b71Sopenharmony_ci 124e41f4b71Sopenharmony_ci3. If "SourceMap is not initialized yet" is displayed in the call stack, as shown in the following example, SourceMap has not been initialized and the row number of the problematic code in the compiled code in an eTS stack is displayed. In this case, this log is added to notify developers. You can click the link to identify where the error occurred in the compiled code. 125e41f4b71Sopenharmony_ci ``` 126e41f4b71Sopenharmony_ci Device info:xxx 127e41f4b71Sopenharmony_ci Build info:xxx-xxx x.x.x.xxx(xxxx) 128e41f4b71Sopenharmony_ci Fingerprint:377ef8529301363f373ce837d0bf83aacfc46112502143237e2f4026e86a0510 129e41f4b71Sopenharmony_ci Module name:com.xxx.xxx 130e41f4b71Sopenharmony_ci Version:1.0.0 131e41f4b71Sopenharmony_ci Versioncode:1000000 132e41f4b71Sopenharmony_ci PreInstalled:No 133e41f4b71Sopenharmony_ci Foreground:Yes 134e41f4b71Sopenharmony_ci Pid:6042 135e41f4b71Sopenharmony_ci Uid:20020145 136e41f4b71Sopenharmony_ci Reason:Error 137e41f4b71Sopenharmony_ci Error name:Error 138e41f4b71Sopenharmony_ci Error message:JSERROR 139e41f4b71Sopenharmony_ci Sourcecode: 140e41f4b71Sopenharmony_ci throw new Error("JSERROR"); 141e41f4b71Sopenharmony_ci ^ 142e41f4b71Sopenharmony_ci Stacktrace: 143e41f4b71Sopenharmony_ci SourceMap is not initialized yet 144e41f4b71Sopenharmony_ci at anonymous (entry/src/main/ets/pages/Index.ts:49:49) 145e41f4b71Sopenharmony_ci ``` 146e41f4b71Sopenharmony_ci 147e41f4b71Sopenharmony_ci4. The native stack is printed in the call stack, as shown in the following example. Generally, the **libark_jsruntime.so** dynamic library is at the top of the stack. This is because JS exceptions are thrown by the VM. Search for the error from the top down. Generally, the next frame of **libace_napi.z.so** is the location where an exception is thrown. 148e41f4b71Sopenharmony_ci ``` 149e41f4b71Sopenharmony_ci Device info:xxx 150e41f4b71Sopenharmony_ci Build info:xxx-xxx x.x.x.xxx(xxxx) 151e41f4b71Sopenharmony_ci Fingerprint:89f2b64b24d642b0fc64e3a7cf68ca39fecaa580ff5736bb9d6706ea4cdf2c93 152e41f4b71Sopenharmony_ci Module name:com.xxx.xxx 153e41f4b71Sopenharmony_ci Version:1.0.0 154e41f4b71Sopenharmony_ci VersionCode:1000000 155e41f4b71Sopenharmony_ci PreInstalled:No 156e41f4b71Sopenharmony_ci Foreground:No 157e41f4b71Sopenharmony_ci Pid:14325 158e41f4b71Sopenharmony_ci Uid:20020145 159e41f4b71Sopenharmony_ci Reason:ReferenceError 160e41f4b71Sopenharmony_ci Error name:ReferenceError 161e41f4b71Sopenharmony_ci Error message:Cannot find module 'com.xxx.xxx/entry/EntryAbility' , which is application Entry Point 162e41f4b71Sopenharmony_ci Stacktrace: 163e41f4b71Sopenharmony_ci SourceMap is not initialized yet 164e41f4b71Sopenharmony_ci #01 pc 000000000028ba3b /system/libó4/platformsdk/libark_jsruntime.so(bf6ea8e474ac3e417991f101e062fa90) 165e41f4b71Sopenharmony_ci #02 pc 00000000001452ff /system/libó4/platformsdk/libark_jsruntime.so(bf6ea8e474ac3e417991f101e062fa90) 166e41f4b71Sopenharmony_ci #03 pC 0000000000144c9f /system/libó4/platformsdk/libark_jsruntime.so(bf6ea8e474ac3e417991f101e062fa90) 167e41f4b71Sopenharmony_ci #04 pc 00000000001c617b /system/libó4/platformsdk/libark_jsruntime.so(bf6ea8e474ac3e417991f101e062fa90) 168e41f4b71Sopenharmony_ci #05 pc 00000000004c3cb7 /system/libó4/platformsdk/libark_jsruntime.so(bf6ea8e474ac3e417991f101e062fa90) 169e41f4b71Sopenharmony_ci #06 pc 00000000004c045f /system/libó4/platformsdk/libark_jsruntime.so(bf6ea8e474ac3e417991f101e062fa90) 170e41f4b71Sopenharmony_ci #07 pc 000000000038034f /system/libó4/platformsdk/libark_jsruntime.so(bf6ea8e474ac3e417991f101e062fa90) 171e41f4b71Sopenharmony_ci #08 pc 00000000004b2d9b /system/libó4/platformsdk/libark_jsruntime.so(bf6ea8e474ac3e417991f101e062fa90) 172e41f4b71Sopenharmony_ci #09 pc 0000000000037e7f /system/libó4/platformsdk/libace_napi.z.so(10ceafd39b5354314d2fe3059b8f9e4f) 173e41f4b71Sopenharmony_ci #10 pc 00000000000484cf /system/lib64/platformsdk/libruntime.z.so(3f6305a3843fae1de148a06eec4bd014) <- Location where an exception is thrown 174e41f4b71Sopenharmony_ci #11 pc 000000000004fce7 /system/libó4/platformsdk/libruntime.z.so(3f6305a3843fae1de148a06eec4bd014) 175e41f4b71Sopenharmony_ci #12 pc 000000000004e9fb /system/libó4/platformsdk/libruntime.z.so(3f6305a3843fae1de148a06eec4bd014) 176e41f4b71Sopenharmony_ci #13 pc 000000000004eb7b /system/libó4/platformsdk/libruntime.z.so(3f6305a3843fae1de148a06eec4bd014) 177e41f4b71Sopenharmony_ci #14 pc 000000000004f5c7 /system/libó4/platformsdk/libruntime.z.so(3f6305a3843fae1de148a06eec4bd014) 178e41f4b71Sopenharmony_ci #15 pc 00000000000303cf /system/lib64/platformsdk/libuiabilitykit_native.z.so(3203F4CCe84a43b519d0a731dfOdb1a3) 179e41f4b71Sopenharmony_ci ``` 180e41f4b71Sopenharmony_ci 181e41f4b71Sopenharmony_ci#### 2. Call Stack Analysis 182e41f4b71Sopenharmony_ci 183e41f4b71Sopenharmony_ciPerform call stack analysis as follows: 184e41f4b71Sopenharmony_ci 185e41f4b71Sopenharmony_ci- Case 1: A hyperlink is provided to go to the problematic code. 186e41f4b71Sopenharmony_ci 187e41f4b71Sopenharmony_ci If the path or offset address in the stack trace information in the FaultLog points to a line of code of the current project, a hyperlink is provided. You can click the link in DevEco Studio to locate the code line. 188e41f4b71Sopenharmony_ci 189e41f4b71Sopenharmony_ci- Case 2: The hyperlink provided to go to the problematic code does not work. 190e41f4b71Sopenharmony_ci 191e41f4b71Sopenharmony_ci If "Cannot get Source Map info, dump raw stack" is displayed, the JS stack fails to obtain the row and column numbers for the problematic code. In this case, clicking the provided hyperlink in DevEco Studio navigates you to an incorrect position or displays an error that indicates the position does not exist. 192e41f4b71Sopenharmony_ci 193e41f4b71Sopenharmony_ci When an error occurs during the running of application code, the error stack information is printed. If the TS stack fails to obtain the row and column numbers for ArkTS code, the filename extension of the error stack is still "ets". You need to compile the intermediate product in the **build** directory to generate TS code and locate the problematic code in JS. For details, see [Application Stack Parsing](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/ide-release-app-stack-analysis-0000001814726289-V5). 194e41f4b71Sopenharmony_ci 195e41f4b71Sopenharmony_ci 196e41f4b71Sopenharmony_ci## Case Study 197e41f4b71Sopenharmony_ci 198e41f4b71Sopenharmony_ciThe following describes the most common error types, namely, **TypeError** and **Error**, that cause JS crashes. 199e41f4b71Sopenharmony_ci 200e41f4b71Sopenharmony_ci### TypeError Analysis 201e41f4b71Sopenharmony_ci 202e41f4b71Sopenharmony_ciAs one of the most common errors that cause JS crashes, TypeError is thrown when the variable type is not the expected one. In other words, the variable is not verified before use. The error message is as follows: 203e41f4b71Sopenharmony_ci 204e41f4b71Sopenharmony_ci``` 205e41f4b71Sopenharmony_ciError name:TypeError 206e41f4b71Sopenharmony_ciError message:Cannot read property xxx of undefined 207e41f4b71Sopenharmony_ci``` 208e41f4b71Sopenharmony_ci 209e41f4b71Sopenharmony_ci#### Case 1: JS crash occasionally occurs when a gesture value is updated. 210e41f4b71Sopenharmony_ci 211e41f4b71Sopenharmony_ci1. Obtain the JS crash log: 212e41f4b71Sopenharmony_ci 213e41f4b71Sopenharmony_ci ``` 214e41f4b71Sopenharmony_ci Generated by HiviewDFX@OpenHarmony 215e41f4b71Sopenharmony_ci ================================================================ 216e41f4b71Sopenharmony_ci Device info:xxxx 217e41f4b71Sopenharmony_ci Build info:xxxx 218e41f4b71Sopenharmony_ci Fingerprint:9851196f9fed7fd818170303296ae7a5767c9ab11f38fd8b0072f0e32c42ea39 219e41f4b71Sopenharmony_ci Module name:com.xxx.xxx 220e41f4b71Sopenharmony_ci Version:1.0.0.29 221e41f4b71Sopenharmony_ci VersionCode:10000029 222e41f4b71Sopenharmony_ci PreInstalled:Yes 223e41f4b71Sopenharmony_ci Foreground:No 224e41f4b71Sopenharmony_ci Pid:2780 225e41f4b71Sopenharmony_ci Uid:20020018 226e41f4b71Sopenharmony_ci Reason:TypeError 227e41f4b71Sopenharmony_ci Error name:TypeError 228e41f4b71Sopenharmony_ci Error message:Cannot read property needRenderTranslate of undefined 229e41f4b71Sopenharmony_ci Stacktrace: 230e41f4b71Sopenharmony_ci Cannot get SourceMap info, dump raw stack: 231e41f4b71Sopenharmony_ci at updateGestureValue (phone/src/main/ets/SceneBoard/recent/scenepanel/recentpanel/RecentGesture.ts:51:51) 232e41f4b71Sopenharmony_ci at onRecentGestureActionBegin (phone/src/main/ets/SceneBoard/scenemanager/SCBScenePanel.ts:5609:5609) 233e41f4b71Sopenharmony_ci at anonymous (phone/src/main/ets/SceneBoard/scenemanager/SCBScenePanel.ts:555:555) 234e41f4b71Sopenharmony_ci at anonymous (phone/src/main/ets/SceneBoard/recent/RecentEventView.ts:183:183) 235e41f4b71Sopenharmony_ci ``` 236e41f4b71Sopenharmony_ci 237e41f4b71Sopenharmony_ci2. Analyze log information. 238e41f4b71Sopenharmony_ci 239e41f4b71Sopenharmony_ci According to the log information, **TypeError** is reported because the **needRenderTranslate** object read is **undefined**. Then, obtain the error location based on the call stack. 240e41f4b71Sopenharmony_ciIf "Cannot get SourceMap info, dump raw stack" is displayed, the application is installed using a release package and the eTS row and column numbers cannot be covnerted from the JS stack. You can refer to [Application Stack Parsing](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/ide-release-app-stack-analysis-0000001814726289-V5) to parse the row number. 241e41f4b71Sopenharmony_ci 242e41f4b71Sopenharmony_ci3. Locate the error code. 243e41f4b71Sopenharmony_ci 244e41f4b71Sopenharmony_ci Based on the preceding JS stack and error variable analysis, the error code can be located as follows: 245e41f4b71Sopenharmony_ci 246e41f4b71Sopenharmony_ci ``` 247e41f4b71Sopenharmony_ci // Update the gesture value. 248e41f4b71Sopenharmony_ci public updateGestureValue(screenWidth: number, recentScale: number, sceneContainerSessionList: SCBSceneContainerSession[]) { 249e41f4b71Sopenharmony_ci // Calculate the moving distance of the hand. 250e41f4b71Sopenharmony_ci this.translationUpY = (this.multiCardsNum >= 1)? sceneContainerSessionList[this.multiCardsNum - 1].needRenderTranslate.translateY: 0; ---> Number of the error line 251e41f4b71Sopenharmony_ci this.translationDownY = (this.multiCardsNum >= 2) ? sceneContainerSessionList[this.multiCardsNum - 2].needRenderTranslate.translateY : 0; 252e41f4b71Sopenharmony_ci this.screenWidth = px2vp(screenWidth); 253e41f4b71Sopenharmony_ci this.recentScale = recentScale; 254e41f4b71Sopenharmony_ci } 255e41f4b71Sopenharmony_ci ``` 256e41f4b71Sopenharmony_ci 257e41f4b71Sopenharmony_ci4. Solution 258e41f4b71Sopenharmony_ci 259e41f4b71Sopenharmony_ci According to the preceding analysis, the member variable **needRenderTranslate** of **sceneContainerSessionList** may be undefined. A protection needs to be added to avoid this type of problem. For example, you can add a '?' operator before the access object for protection. 260e41f4b71Sopenharmony_ci 261e41f4b71Sopenharmony_ci ``` 262e41f4b71Sopenharmony_ci // Update the gesture value. 263e41f4b71Sopenharmony_ci public updateGestureValue(screenWidth: number, recentScale: number, sceneContainerSessionList: SCBSceneContainerSession[]) { 264e41f4b71Sopenharmony_ci // Calculate the moving distance of the hand. 265e41f4b71Sopenharmony_ci this.translationUpY = (this.multiCardsNum >= 1) ? 266e41f4b71Sopenharmony_ci sceneContainerSessionList[this.multiCardsNum - 1]?.needRenderTranslate.translateY : 0; 267e41f4b71Sopenharmony_ci this.translationDownY = (this.multiCardsNum >= 2) ? 268e41f4b71Sopenharmony_ci sceneContainerSessionList[this.multiCardsNum - 2]?.needRenderTranslate.translateY : 0; 269e41f4b71Sopenharmony_ci this.screenWidth = px2vp(screenWidth); 270e41f4b71Sopenharmony_ci this.recentScale = recentScale; 271e41f4b71Sopenharmony_ci } 272e41f4b71Sopenharmony_ci ``` 273e41f4b71Sopenharmony_ci 274e41f4b71Sopenharmony_ci5. Suggestions 275e41f4b71Sopenharmony_ci 276e41f4b71Sopenharmony_ci To solve this problem, we need to add necessary null checks in the coding phase to ensure security of object access. In many scenarios, the null check may only be a workaround. You need to check the object construction or value assignment logic based on service requirements. 277e41f4b71Sopenharmony_ci 278e41f4b71Sopenharmony_ci### Error Analysis 279e41f4b71Sopenharmony_ci 280e41f4b71Sopenharmony_ciError problems are JS exceptions thrown by developers or JS libraries. 281e41f4b71Sopenharmony_ci 282e41f4b71Sopenharmony_ciThere are two scenarios for this type of problem: 283e41f4b71Sopenharmony_ci1. If the application encounters a fault that cannot be rectified, a JS exception is thrown to terminate the service and generate a fault log. 284e41f4b71Sopenharmony_ci2. The service is terminated by an exception thrown by an API of the dependent JS library or module. In this case, you need to consider using try-catch to capture such exceptions. 285e41f4b71Sopenharmony_ci 286e41f4b71Sopenharmony_ci 287e41f4b71Sopenharmony_ci#### Case 1: Throw a custom JS exception to terminate an application. 288e41f4b71Sopenharmony_ci 289e41f4b71Sopenharmony_ciYou can use the following code to throw a JS exception: 290e41f4b71Sopenharmony_ci 291e41f4b71Sopenharmony_ci``` 292e41f4b71Sopenharmony_cithrow new Error("TEST JS ERROR") 293e41f4b71Sopenharmony_ci``` 294e41f4b71Sopenharmony_ci 295e41f4b71Sopenharmony_ciBased on the fault logs collected by DevEco Studio FaultLog, you can locate the exception based on the JS exception stack. 296e41f4b71Sopenharmony_ci 297e41f4b71Sopenharmony_ci 298e41f4b71Sopenharmony_ci 299e41f4b71Sopenharmony_ciTo solve this problem, locate the problematic code line based on the fault log and review the context. 300e41f4b71Sopenharmony_ci 301e41f4b71Sopenharmony_ci#### Case 2: Handle the JS crash caused by a JS exception thrown by a third-party API. 302e41f4b71Sopenharmony_ci 303e41f4b71Sopenharmony_ci1. Obtain the JS crash log. The key log information is as follows: 304e41f4b71Sopenharmony_ci ``` 305e41f4b71Sopenharmony_ci Error name:Error 306e41f4b71Sopenharmony_ci Error message:BussinessError 2501000: Operation failed. 307e41f4b71Sopenharmony_ci Error code:2501000 308e41f4b71Sopenharmony_ci Stacktrace: 309e41f4b71Sopenharmony_ci Cannot get SourceMap info, dump raw stack: 310e41f4b71Sopenharmony_ci at onStart (product/phone/build/default/cache/default/default@CompileArkTS/esmodule/release/feature/systemstatus/linkspeedcomponent/src/main/ets/default/controller/NetSpeedController.ts:50:1) 311e41f4b71Sopenharmony_ci at NetSpeedController (product/phone/build/default/cache/default/default@CompileArkTS/esmodule/release/feature/systemstatus/linkspeedcomponent/src/main/ets/default/controller/NetSpeedController.ts:43:43) 312e41f4b71Sopenharmony_ci at getInstance (product/phone/build/default/cache/default/default@CompileArkTS/esmodule/release/staticcommon/basiccommon/src/main/ets/component/utils/SingletonHelper.ts:17:17) 313e41f4b71Sopenharmony_ci at func_main_0 (product/phone/build/default/cache/default/default@CompileArkTS/esmodule/release/feature/systemstatus/linkspeedcomponent/src/main/ets/default/controller/NetSpeedController.ts:325:325) 314e41f4b71Sopenharmony_ci ``` 315e41f4b71Sopenharmony_ci 316e41f4b71Sopenharmony_ci2. Analyze log information. 317e41f4b71Sopenharmony_ci 318e41f4b71Sopenharmony_ci According to the log information, an **Error** exception is thrown by the code. Then, obtain the error location based on the stack trace. 319e41f4b71Sopenharmony_ciIf "Cannot get SourceMap info, dump raw stack" is displayed, the application is installed using a release package and the eTS row and column numbers cannot be covnerted from the JS stack. You can refer to [Application Stack Parsing](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/ide-release-app-stack-analysis-0000001814726289-V5) to parse the row number. 320e41f4b71Sopenharmony_ci 321e41f4b71Sopenharmony_ci3. Locate the error code. 322e41f4b71Sopenharmony_ci 323e41f4b71Sopenharmony_ci Based on the preceding JS stack, you can locate the code in the **NetSpeedController.ts** file. The exception is thrown when **wifiManager.on()** is called. 324e41f4b71Sopenharmony_ci 325e41f4b71Sopenharmony_ci ``` 326e41f4b71Sopenharmony_ci onStart(): void { 327e41f4b71Sopenharmony_ci super.onStart(); 328e41f4b71Sopenharmony_ci log.showInfo('onStart'); 329e41f4b71Sopenharmony_ci ... 330e41f4b71Sopenharmony_ci wifiManager.on('wifiConnectionChange', (data) => { 331e41f4b71Sopenharmony_ci this.isConnected = data === 1 ? true : false; 332e41f4b71Sopenharmony_ci this.handleUpdateState(); 333e41f4b71Sopenharmony_ci }); 334e41f4b71Sopenharmony_ci wifiManager.on('wifiStateChange', (data) => { 335e41f4b71Sopenharmony_ci this.isWifiActive = data === 1 ? true : false; 336e41f4b71Sopenharmony_ci this.handleUpdateState(); 337e41f4b71Sopenharmony_ci }); 338e41f4b71Sopenharmony_ci ... 339e41f4b71Sopenharmony_ci } 340e41f4b71Sopenharmony_ci ``` 341e41f4b71Sopenharmony_ci 342e41f4b71Sopenharmony_ci4. Solution 343e41f4b71Sopenharmony_ci 344e41f4b71Sopenharmony_ci According to the analysis of the source code, **wifiManager.on()** throws "BussinessError 2501000: Operation failed" occasionally. If this exception does not cause the application to crash, use try-catch to capture and process the exception. Modify the code as follows: 345e41f4b71Sopenharmony_ci 346e41f4b71Sopenharmony_ci ``` 347e41f4b71Sopenharmony_ci onStart(): void { 348e41f4b71Sopenharmony_ci super.onStart(); 349e41f4b71Sopenharmony_ci log.showInfo('onStart'); 350e41f4b71Sopenharmony_ci ... 351e41f4b71Sopenharmony_ci try { 352e41f4b71Sopenharmony_ci wifiManager.on('wifiConnectionChange', (data) => { 353e41f4b71Sopenharmony_ci this.isConnected = data === 1 ? true : false; 354e41f4b71Sopenharmony_ci this.handleUpdateState(); 355e41f4b71Sopenharmony_ci }); 356e41f4b71Sopenharmony_ci } catch (error) { 357e41f4b71Sopenharmony_ci log.showError('wifiConnectionChange error'); 358e41f4b71Sopenharmony_ci } 359e41f4b71Sopenharmony_ci try { 360e41f4b71Sopenharmony_ci wifiManager.on('wifiStateChange', (data) => { 361e41f4b71Sopenharmony_ci this.isWifiActive = data === 1 ? true : false; 362e41f4b71Sopenharmony_ci this.handleUpdateState(); 363e41f4b71Sopenharmony_ci }); 364e41f4b71Sopenharmony_ci } catch (error) { 365e41f4b71Sopenharmony_ci log.showError('wifiStateChange error'); 366e41f4b71Sopenharmony_ci } 367e41f4b71Sopenharmony_ci ... 368e41f4b71Sopenharmony_ci } 369e41f4b71Sopenharmony_ci ``` 370e41f4b71Sopenharmony_ci 371e41f4b71Sopenharmony_ci5. Suggestions 372e41f4b71Sopenharmony_ci 373e41f4b71Sopenharmony_ci For such problems, we can use the JS exception mechanism in the coding phase to identify various exception scenarios. In addition, consider capturing the exceptions thrown by APIs to prevent unnecessary interrupts of the main services of the application. 374