DEV Community

桃花镇童长老
桃花镇童长老

Posted on

CrashUtil, exception-related tool class

Introduction and description of harmony-utils


harmony-utils A HarmonyOS tool library with rich features and extremely easy to use, with the help of many practical tools, is committed to helping developers quickly build Hongmeng applications. Its encapsulated tools cover APP, device, screen, authorization, notification, inter-thread communication, pop-up frames, toast, biometric authentication, user preferences, taking photos, albums, scanning codes, files, logs, exception capture, characters, strings, numbers, collections, dates, random, base64, encryption, decryption, JSON and other functions, which can meet various development needs.

picker_utils It is a sub-store split by harmony-utils, including PickerUtil, PhotoHelper, and ScanUtil.

Download and install

ohpm i @pura/harmony-utils

ohpm i @pura/picker_utils

  //Global initialization method, initialized in the onCreate method of UIAbility AppUtil.init()
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
    AppUtil.init(this.context);
  }
Enter fullscreen mode Exit fullscreen mode

API methods and usage


onHandled registers an error observer (this method is recommended to be called in Ability). After registration, you can capture the js crash generated by the application, and the process will not exit when the application crashes. Write exception information to local file
CrashUtil.onHandled((exceptionInfo)=>{
  LogUtil.error(JSON.stringify(exceptionInfo, null, 2));
});
Enter fullscreen mode Exit fullscreen mode
onDestroy Log out of error observer
CrashUtil.onDestroy(); //注销错误观测器
ToastUtil.showToast("注销误观测器,成功!");
Enter fullscreen mode Exit fullscreen mode
isHandled determines whether the error observer exists
let isHandled = CrashUtil.isHandled();
ToastUtil.showToast(`误观测器是否存在:${isHandled}`);
Enter fullscreen mode Exit fullscreen mode
getFilePath Gets the log file path (used to read exception files and export exception files)
let path = CrashUtil.getFilePath();
LogUtil.error(`异常日志文件路径:${path}`);
Enter fullscreen mode Exit fullscreen mode
access determines whether the log file exists
let access = CrashUtil.access();
ToastUtil.showToast(`日志文件是否存在:${access}`);
Enter fullscreen mode Exit fullscreen mode
delete delete log file
CrashUtil.delete();
ToastUtil.showToast(`日志文件删除成功!`);
Enter fullscreen mode Exit fullscreen mode
getExceptionJson gets the JSON string of the exception log
if (CrashUtil.access()) {
  let jsonStr = await CrashUtil.getExceptionJson(); //读取JSON
  Utils.showSheetText(jsonStr);
} else {
  ToastUtil.showToast("暂无日志文件");
}
Enter fullscreen mode Exit fullscreen mode
getExceptionList gets the collection of exception logs
if (CrashUtil.access()) {
  let list = await CrashUtil.getExceptionList();
  DialogHelper.showToast(`异常个数:${list.length}`);
} else {
  ToastUtil.showToast("暂无日志文件");
}
Enter fullscreen mode Exit fullscreen mode
enableAppRecovery Enable the application recovery function and fill in the parameters in order. After this interface is called, the first Ability supports recovery when the application starts from the initiator
CrashUtil.enableAppRecovery();
Enter fullscreen mode Exit fullscreen mode
restartApp Restart the APP and pull up the first Ability when the application starts. It can be used with the errorManager-related interface
CrashUtil.restartApp();
Enter fullscreen mode Exit fullscreen mode
saveAppState Save the current App status or actively save the state of Ability. This state will be used the next time you resume startup. Can be used with errorManager related interface
CrashUtil.saveAppState(this.context);
Enter fullscreen mode Exit fullscreen mode
setRestartWant Set the next time you restore the Ability in the scene that actively pulls up. The Ability must be the UIAbility under the current package
let want: Want = {
  bundleName: 'com.harmony.utils',
  abilityName: 'EntryAbility'
};
CrashUtil.setRestartWant(want);
Enter fullscreen mode Exit fullscreen mode

Top comments (0)