Skip to content

Commit d446d1b

Browse files
committed
[Issues: #I8LTB0] 添加react-native-exception-handler文档
1 parent 2601553 commit d446d1b

File tree

3 files changed

+307
-0
lines changed

3 files changed

+307
-0
lines changed

_sidebar.md

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
- [react-native-SmartRefreshLayout](zh-cn/react-native-SmartRefreshLayout.md)
1818
- [lottie-react-native](zh-cn/lottie-react-native.md)
1919
- [react-native-webview](zh-cn/react-native-webview.md)
20+
- [react-native-exception-handler](zh-cn/react-native-exception-handler.md)
2021

2122

2223

zh-cn/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
| 10 | react-native-SmartRefreshLayout | 0.6.7 || 70% | [@react-native-oh-library/react-native-SmartRefreshLayout](https://github.com/react-native-oh-library/react-native-SmartRefreshLayout/releases) | [链接](zh-cn/react-native-SmartRefreshLayout.md) |
2727
| 11 | lottie-react-native | 6.4.1 || 50% | [@react-native-oh-library/lottie-react-native](https://github.com/react-native-oh-library/lottie-react-native/releases) | [链接](zh-cn/lottie-react-native.md) |
2828
| 12 | react-native-webview | 13.6.2 || 15% | [@react-native-oh-library/react-native-webview](https://github.com/react-native-oh-library/react-native-webview) | [链接](zh-cn/react-native-webview.md) |
29+
| 13 | react-native-exception-handler | 2.10.10 || 100% | [@react-native-oh-library/react-native-exception-handler](https://github.com/react-native-oh-library/react-native-exception-handler) | [链接](zh-cn/react-native-exception-handler.md) |
2930

3031
## 社区
3132

+305
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,305 @@
1+
> 模板版本:v0.0.1
2+
3+
<p align="center">
4+
<h1 align="center"> <code>react-native-exception-handler</code> </h1>
5+
</p>
6+
<p align="center">
7+
<a href="https://github.com/a7ul/react-native-exception-handler">
8+
<img src="https://img.shields.io/badge/platforms-android%20|%20ios%20|%20harmony%20-lightgrey.svg" alt="Supported platforms" />
9+
</a>
10+
<a href="https://github.com/a7ul/react-native-exception-handler/blob/master/LICENSE">
11+
<img src="https://img.shields.io/badge/license-MIT-green.svg" alt="License" />
12+
</a>
13+
</p>
14+
15+
## 安装与使用
16+
17+
进入到工程目录并输入以下命令:
18+
19+
<!-- tabs:start -->
20+
21+
#### **yarn**
22+
23+
```bash
24+
yarn add react-native-exception-handler@npm:@react-native-oh-library/react-native-exception-handler
25+
```
26+
27+
#### **npm**
28+
29+
```bash
30+
npm install react-native-exception-handler@npm:@react-native-oh-library/react-native-exception-handler
31+
```
32+
33+
<!-- tabs:end -->
34+
35+
下面的代码展示了这个库的基本使用场景:
36+
37+
To catch <b>JS_Exceptions</b>
38+
```typescript
39+
import {setJSExceptionHandler, getJSExceptionHandler} from 'react-native-exception-handler';
40+
41+
// For most use cases:
42+
// registering the error handler (maybe u can do this in the index.android.js or index.ios.js)
43+
setJSExceptionHandler((error, isFatal) => {
44+
// This is your custom global error handler
45+
// You do stuff like show an error dialog
46+
// or hit google analytics to track crashes
47+
// or hit a custom api to inform the dev team.
48+
});
49+
//=================================================
50+
// ADVANCED use case:
51+
const exceptionhandler = (error, isFatal) => {
52+
// your error handler function
53+
};
54+
setJSExceptionHandler(exceptionhandler, allowInDevMode);
55+
// - exceptionhandler is the exception handler function
56+
// - allowInDevMode is an optional parameter is a boolean.
57+
// If set to true the handler to be called in place of RED screen
58+
// in development mode also.
59+
60+
// getJSExceptionHandler gives the currently set JS exception handler
61+
const currentHandler = getJSExceptionHandler();
62+
```
63+
To catch <b>Native_Exceptions</b>
64+
```typescript
65+
import { setNativeExceptionHandler } from "react-native-exception-handler";
66+
67+
//For most use cases:
68+
setNativeExceptionHandler((exceptionString) => {
69+
// This is your custom global error handler
70+
// You do stuff likehit google analytics to track crashes.
71+
// or hit a custom api to inform the dev team.
72+
//NOTE: alert or showing any UI change via JS
73+
//WILL NOT WORK in case of NATIVE ERRORS.
74+
});
75+
//====================================================
76+
// ADVANCED use case:
77+
const exceptionhandler = (exceptionString) => {
78+
// your exception handler code here
79+
};
80+
setNativeExceptionHandler(
81+
exceptionhandler,
82+
forceAppQuit,
83+
executeDefaultHandler
84+
);
85+
// - exceptionhandler is the exception handler function
86+
// - forceAppQuit is an optional ANDROID specific parameter that defines
87+
// if the app should be force quit on error. default value is true.
88+
// To see usecase check the common issues section.
89+
// - executeDefaultHandler is an optional boolean (both IOS, ANDROID)
90+
// It executes previous exception handlers if set by some other module.
91+
// It will come handy when you use any other crash analytics module along with this one
92+
// Default value is set to false. Set to true if you are using other analytics modules.
93+
```
94+
95+
## Link
96+
97+
目前鸿蒙暂不支持 AutoLink,所以 Link 步骤需要手动配置。
98+
99+
首先需要使用 DevEco Studio 打开项目里的鸿蒙工程 `harmony`
100+
101+
### 引入原生端代码
102+
103+
目前有两种方法:
104+
105+
1. 通过 har 包引入(在 IDE 完善相关功能后该方法会被遗弃,目前首选此方法);
106+
2. 直接链接源码。
107+
108+
方法一:通过 har 包引入
109+
打开 `entry/oh-package.json5`,添加以下依赖
110+
111+
```diff
112+
"dependencies": {
113+
"rnoh": "file:../rnoh",
114+
+ "rnoh-exception-handler": "file:../../node_modules/react-native-exception-handler/harmony/exception_handler.har",
115+
}
116+
```
117+
118+
点击右上角的 `sync` 按钮
119+
120+
或者在终端执行:
121+
122+
```bash
123+
cd entry
124+
ohpm install
125+
```
126+
127+
方法二:直接链接源码
128+
打开 `entry/oh-package.json5`,添加以下依赖
129+
130+
```diff
131+
"dependencies": {
132+
"rnoh": "file:../rnoh",
133+
+ "rnoh-exception-handler": "file:../../node_modules/react-native-exception-handler/harmony/exception_handler"
134+
}
135+
```
136+
137+
打开终端,执行:
138+
139+
```bash
140+
cd entry
141+
ohpm install --no-link
142+
```
143+
144+
### 配置 CMakeLists 和引入 ExceptionHandlerPackage
145+
146+
打开 `entry/src/main/cpp/CMakeLists.txt`,添加:
147+
148+
```diff
149+
project(rnapp)
150+
cmake_minimum_required(VERSION 3.4.1)
151+
set(RNOH_APP_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
152+
set(OH_MODULE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../oh_modules")
153+
set(RNOH_CPP_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../react-native-harmony/harmony/cpp")
154+
155+
add_subdirectory("${RNOH_CPP_DIR}" ./rn)
156+
157+
# RNOH_BEGIN: add_package_subdirectories
158+
add_subdirectory("../../../../sample_package/src/main/cpp" ./sample-package)
159+
+ add_subdirectory("${OH_MODULE_DIR}/rnoh-exception-handler/src/main/cpp" ./exception_handler)
160+
# RNOH_END: add_package_subdirectories
161+
162+
add_library(rnoh_app SHARED
163+
"./PackageProvider.cpp"
164+
"${RNOH_CPP_DIR}/RNOHAppNapiBridge.cpp"
165+
)
166+
167+
target_link_libraries(rnoh_app PUBLIC rnoh)
168+
169+
# RNOH_BEGIN: link_packages
170+
target_link_libraries(rnoh_app PUBLIC rnoh_sample_package)
171+
+ target_link_libraries(rnoh_app PUBLIC rnoh_exception_handler)
172+
# RNOH_END: link_packages
173+
```
174+
175+
打开 `entry/src/main/cpp/PackageProvider.cpp`,添加:
176+
177+
```diff
178+
#include "RNOH/PackageProvider.h"
179+
#include "SamplePackage.h"
180+
+ #include "ExceptionHandlerPackage.h"
181+
182+
using namespace rnoh;
183+
184+
std::vector<std::shared_ptr<Package>> PackageProvider::getPackages(Package::Context ctx) {
185+
return {
186+
std::make_shared<SamplePackage>(ctx),
187+
+ std::make_shared<ExceptionHandlerPackage>(ctx)
188+
};
189+
}
190+
```
191+
192+
### 在 ArkTs 侧引入 ExceptionHandlerPackage
193+
194+
打开 `entry/src/main/ets/RNPackagesFactory.ts`,添加:
195+
196+
```diff
197+
import type {RNPackageContext, RNPackage} from 'rnoh/ts';
198+
import {SamplePackage} from 'rnoh-sample-package/ts';
199+
+ import {ExceptionHandlerPackage} from 'rnoh-async-storage/ts';
200+
201+
export function createRNPackages(ctx: RNPackageContext): RNPackage[] {
202+
return [
203+
new SamplePackage(ctx),
204+
+ new ExceptionHandlerPackage(ctx)
205+
];
206+
}
207+
```
208+
209+
### 应用重启使能
210+
`YourProject/entry/src/main/ets/app` 目录下,新建文件`MyAbilityStage.ets`
211+
```typescript
212+
import AbilityStage from '@ohos.app.ability.AbilityStage';
213+
import appRecovery from '@ohos.app.ability.appRecovery';
214+
import Want from '@ohos.app.ability.Want';
215+
216+
export default class MyAbilityStage extends AbilityStage {
217+
onCreate() {
218+
appRecovery.enableAppRecovery();
219+
let want: Want = {
220+
bundleName: this.context.applicationInfo.name,
221+
abilityName: this.context.currentHapModuleInfo.mainElementName
222+
}
223+
appRecovery.setRestartWant(want);
224+
}
225+
}
226+
```
227+
`YourProject/entry/src/main/module.json5`补上配置
228+
```diff
229+
{
230+
"module": {
231+
"name": "entry",
232+
"type": "entry",
233+
+ "srcEntry": "./ets/app/MyAbilityStage.ets",
234+
"description": "$string:module_desc",
235+
"mainElement": "EntryAbility",
236+
237+
···
238+
239+
"abilities": [
240+
{
241+
"name": "EntryAbility",
242+
"srcEntry": "./ets/entryability/EntryAbility.ets",
243+
"description": "$string:EntryAbility_desc",
244+
"icon": "$media:icon",
245+
"label": "$string:EntryAbility_label",
246+
"startWindowIcon": "$media:icon",
247+
"startWindowBackground": "$color:start_window_background",
248+
+ "recoverable": true,
249+
"exported": true,
250+
"skills": [
251+
{
252+
"entities": [
253+
"entity.system.home"
254+
],
255+
"actions": [
256+
"action.system.home"
257+
]
258+
}
259+
]
260+
}
261+
]
262+
```
263+
264+
### 运行
265+
266+
点击右上角的 `sync` 按钮
267+
268+
或者在终端执行:
269+
270+
```bash
271+
cd entry
272+
ohpm install
273+
```
274+
275+
然后编译、运行即可。
276+
277+
## 兼容性
278+
279+
要使用此库,需要使用正确的 React-Native 和 RNOH 版本。另外,还需要使用配套的 DevEco Studio 和 手机 ROM。
280+
281+
请到三方库相应的 Releases 发布地址查看 Release 配套的版本信息:[@react-native-oh-library/reace-native-exception-handler Releases](https://github.com/react-native-oh-library/react-native-exception-handler/releases)
282+
283+
## 静态方法
284+
285+
| 名称 | 说明 | 类型 | 是否必填 | 原库平台 | 鸿蒙支持 |
286+
| ---------------------- | ------------ | ------------------ | -------- | -------- | -------- |
287+
| `setJSExceptionHandler` | 设置JS异常处理方法 | function | no | All | yes |
288+
| `getJSExceptionHandler` | 获取JS异常处理方法 | function | no | All | yes |
289+
290+
291+
## API
292+
293+
| 名称 | 说明 | 类型 | 是否必填 | 原库平台 | 鸿蒙支持 |
294+
| ---- | ---- | ---- | -------- | -------- | -------- |
295+
| `setNativeExceptionHandler` | 设置native异常处理方法 | function | no | android,ios | yes |
296+
297+
## 遗留问题
298+
299+
- [ ] Harmony没有异常默认处理机制
300+
301+
## 其他
302+
303+
## 开源协议
304+
305+
本项目基于 [The MIT License (MIT)](https://github.com/a7ul/react-native-exception-handler/blob/master/LICENSE) ,请自由地享受和参与开源。

0 commit comments

Comments
 (0)