Skip to content

Commit 807c9e8

Browse files
committed
Added read
1 parent ec40bac commit 807c9e8

File tree

2 files changed

+131
-15
lines changed

2 files changed

+131
-15
lines changed

README.md

Lines changed: 123 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,124 @@
11
# react-native-exception-handler
2-
A react native module to help tackle the exception problem in react native.
2+
3+
A react native module that lets you to register a global error handler that can capture fatal/non fatal uncaught exceptions.
4+
The module helps prevent abrupt crashing of RN Apps without a graceful message to the user.
5+
6+
In the current scenario:
7+
- `In DEV mode , you get a RED Screen error pointing your JS errors.`
8+
- `In Bundled mode , the app just quits without any prompt !` 🙄
9+
10+
To tackle this we register a global error handler that could be used to for example:
11+
1. Send bug reports to dev team when the app crashes
12+
2. Show a creative dialog saying the user should restart the application
13+
14+
### Installation:
15+
16+
```sh
17+
yarn add react-native-exception-handler
18+
```
19+
20+
or
21+
22+
```sh
23+
npm i react-native-exception-handler --save
24+
```
25+
26+
27+
### Usage
28+
29+
```js
30+
import {setJSExceptionHandler} from 'react-native-exception-handler';
31+
32+
.
33+
.
34+
.
35+
36+
const errorHandler = (error, isFatal) => {
37+
// This is your custom global error handler
38+
}
39+
40+
.
41+
.
42+
.
43+
44+
setJSExceptionHandler(errorHandler); // registering the error handler (maybe u can do this in the index.android.js or index.ios.js)
45+
46+
```
47+
48+
49+
### Examples
50+
51+
##### Restart on error example
52+
53+
This example shows how to use this module show a graceful bug dialog to the user on crash and restart the app when the user presses ok !
54+
55+
```js
56+
import {Alert} from 'react-native';
57+
import RNRestart from 'react-native-restart';
58+
import {setJSExceptionHandler} from 'react-native-exception-handler';
59+
60+
const errorHandler = (e, isFatal) => {
61+
if (isFatal) {
62+
Alert.alert(
63+
'Unexpected error occurred',
64+
`
65+
Error: ${(isFatal) ? 'Fatal:' : ''} ${e.name} ${e.message}
66+
67+
We will need to restart the app.
68+
`,
69+
[{
70+
text: 'Restart',
71+
onPress: () => {
72+
RNRestart.Restart();
73+
}
74+
}]
75+
);
76+
} else {
77+
console.log(e); // So that we can see it in the ADB logs in case of Android if needed
78+
}
79+
};
80+
81+
setJSExceptionHandler(errorHandler);
82+
```
83+
84+
#### Bug Capture to dev team example
85+
86+
This example shows how to use this module to send global errors to the dev team and show a graceful bug dialog to the user on crash !
87+
88+
```js
89+
import {Alert} from 'react-native';
90+
import {BackAndroid} from 'react-native';
91+
import {setJSExceptionHandler} from 'react-native-exception-handler';
92+
93+
const reporter = (error) => {
94+
// Logic for reporting to devs
95+
// Example : Log issues to github issues using github apis.
96+
console.log(error); // sample
97+
};
98+
99+
const errorHandler = (e, isFatal) => {
100+
if (isFatal) {
101+
reporter(e);
102+
Alert.alert(
103+
'Unexpected error occurred',
104+
`
105+
Error: ${(isFatal) ? 'Fatal:' : ''} ${e.name} ${e.message}
106+
107+
We have reported this to our team ! Please close the app and start again!
108+
`,
109+
[{
110+
text: 'Close',
111+
onPress: () => {
112+
BackAndroid.exitApp();
113+
}
114+
}]
115+
);
116+
} else {
117+
console.log(e); // So that we can see it in the ADB logs in case of Android if needed
118+
}
119+
};
120+
121+
setJSExceptionHandler(errorHandler);
122+
```
123+
124+
*More Examples can be found in the examples folder*

package.json

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"name": "react-native-exception-handler",
33
"version": "1.0.0",
4-
"description": "A react native module to help tackle the exception problem in react native.",
4+
"description": "A react native module that lets you to register a global error handler that can capture fatal/non fatal uncaught exceptions.
5+
",
56
"main": "index.js",
67
"scripts": {
78
"test": "echo no tests && exit 1",
@@ -18,9 +19,13 @@
1819
"native",
1920
"exception",
2021
"handler",
22+
"red",
23+
"screen",
2124
"production",
2225
"ios",
23-
"android"
26+
"android",
27+
"bug",
28+
"capture"
2429
],
2530
"peerDependencies": {
2631
"react": "*",
@@ -31,16 +36,5 @@
3136
"bugs": {
3237
"url": "https://github.com/master-atul/react-native-exception-handler/issues"
3338
},
34-
"homepage": "https://atulr.com/react-native-exception-handler",
35-
"bundledDependencies": [
36-
"react-native-restart"
37-
],
38-
"devDependencies": {
39-
"babel-eslint": "^7.1.1",
40-
"babel-polyfill": "^6.22.0",
41-
"babel-preset-react-native": "^1.9.1",
42-
"eslint": "^3.14.1",
43-
"eslint-plugin-react": "^6.9.0",
44-
"eslint-plugin-react-native": "^2.2.1"
45-
}
39+
"homepage": "https://atulr.com/react-native-exception-handler"
4640
}

0 commit comments

Comments
 (0)