DEV Community

Cover image for HarmonyOS Development: Realize Global Exception Capture and Exception View
程序员一鸣
程序员一鸣

Posted on

HarmonyOS Development: Realize Global Exception Capture and Exception View

Foreword 

this article is based on Api12 

the abnormal information in development can be easily checked and can be viewed directly in the console. However, how can we obtain the abnormal information submitted to the test students or after going online? Here we can easily think of the three-party sdk, such as the common Tencent Bugly, by integrating it, we can collect abnormal information of applications, and directly in their background, we can find abnormal information, so that we can carry out targeted solutions. Using three parties is very convenient, and we don't need to consider the server problem. However, we need to pay a certain amount of money, even if Bugly, which has been free for a long time, has not yet charged, however, from the official website, it has already moved in the direction of charging. 

Charging is on the one hand, how to flexibly get the error information and execute the logic we want is also a demand of self-research, such as restarting the application after the global monitoring exception, or uploading it to your own server, or viewing it in the application, etc., to achieve a global exception capture, there are indeed many useful things. 

Hongmeng, how to achieve it? 

The implementation is very simple, direct use. errorManager object, register the listener, ErrorManager can provide the ability to register and unregister error observers, and it is recommended that the main in EntryAbility or AbilityStage. 

Introduction of abnormal monitoring function: 

interface Name description
onUnhandledException(errMsg: string): void The system callback interface, which is the callback when the application generates an uncaught exception after the application is registered. 
onException? (errObject: Error): void  system callback interface. After the application is registered, the callback when the application generates an exception and reports it to the JS layer. 

Simple examples are as follows:

 

errorManager.on('error', {
onUnhandledException: (errMsg) => {
Console.log (callback when uncaught exception occurs, onUnhandledException:', errMsg);
},
onException: (errorObj) => {
Console.log is a callback that reports exceptions to the JS layer, onException');
}
})
Enter fullscreen mode Exit fullscreen mode

when abnormal information occurs, the above monitoring will be carried out. Attention should be paid to it, after registration, you can capture the js crash generated by the application, the process does not exit when the application crashes.

In addition to registration, the system provides log out the error observer, which can be found in onDestroy is declared during the cycle. registerId is the id at the time of registration and can be directly assigned to errorManager.on().

 

errorManager.off('error', registerId, (result) => { 

    }); 
Enter fullscreen mode Exit fullscreen mode

In the above part, we only realized the monitoring of exceptions. We can expand the above methods, for example, store the exception information locally, upload it to the server, or customize the development-related exception UI view, etc. At present, I have made a layer of encapsulation for these functions, hoping to help friends in need. 

The main contents of this paper are as follows: 

1. catch Library effect after encapsulation

2. Quick introduction of catch Library

3. Main implementation of catch Library


first, the catch Library effect after encapsulation

Currently, in addition to the global exception capture method, the exception view UI is also provided, which is convenient for testers or other personnel to quickly share with developers when they encounter exceptions. 

Image description

Image description

Second, the rapid introduction of catch Library 

introduction 

method 1: in the Terminal window, run the following command to install the third-party package. DevEco Studio automatically adds the third-party package dependency to the project oh-package.json5. 

Suggestion: Execute the command under the module path used.


ohpm install @abner/catch
Enter fullscreen mode Exit fullscreen mode

Method 2: Set the three-party package dependency in the project oh-package.json5. The configuration example is as follows:

 

"dependencies": { "@abner/catch": "^1.0.0"}
Enter fullscreen mode Exit fullscreen mode

initialization 

it is recommended to initialize in the AbilityStage or the UIAbility of the main entry.

 

onHandledException({
  context: this.context,
  onExceptionBack: (exception) => {

  }
})
Enter fullscreen mode Exit fullscreen mode

Property Introduction


property type overview
context  Context  context, for database and culture store reads 
isExceptionSave  boolean  whether the exception information is saved locally, by default 
isFileSave  boolean  whether to save as a file, the default is database, true: file, fasle: Database
faultType  FaultLogger.FaultType  exception type, NO_SPECIFIC does not distinguish between fault types (default is both),CPP_CRASH C ++ program fault type, JS_CRASH JS program fault type, APP_FREEZE application stuck fault type 
isExceptionIntercept  boolean  whether the exception information is blocked, the default is true to block, false does not block, does not block, does not go through the callback, and does not save the exception information. 
onExceptionBack  callback function  the callback function returns exception information, which can be reported here. 

Close Global Exceptions


 

onExceptionDestroy()
Enter fullscreen mode Exit fullscreen mode

view Exception Information 

if you want to view global exception information locally, you can call it where it is used, and the exception list page will pop up. 

Click the entry: view the exception details, left slide the entry: you can delete this exception information, and click empty in the upper right corner: you can delete all exception information.

 

openExceptionDialog()
Enter fullscreen mode Exit fullscreen mode

Abnormal reporting 

if you want to get the exception information yourself, report it to your own server or three parties, or handle it yourself, you can implement it in initialization. The onExceptionBack callback. 

Three, catch Library main implementation

In fact, the catch library only encapsulates the global exceptions of the system, realizing the storage of local exception information, file storage and local database storage, and other temporary expansion. 

File storage, everyone can pay attention file management module fs, the database can focus on relational database relational store. 

Central Warehouse Address: https://ohpm.openharmony.cn/#/cn/detail/@abner%2Fcatch

Top comments (0)