native prompt react_react-native异常处理库 – react-native-exception-...
react-native-exception-handlerA react native module that lets you to register a global error handler that can capture fatal/non fatal uncaught exceptions. The module helps prevent abrupt crashing of R
react-native-exception-handler
A react native module that lets you to register a global error handler that can capture fatal/non fatal uncaught exceptions. The module helps prevent abrupt crashing of RN Apps without a graceful message to the user.
In the current scenario:
In DEV mode , you get a RED Screen error pointing your JS errors.
In Bundled mode , the app just quits without any prompt !
To tackle this we register a global error handler that could be used to for example:
Send bug reports to dev team when the app crashes
Show a creative dialog saying the user should restart the application
Installation:
yarn add react-native-exception-handler?
or
npm i react-native-exception-handler --save
Screens
Without any error handling
In DEV MODE
In BUNDLED MODE
With Error handling in BUNDLED MODE
Examples
Restart on error example
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 !
import {Alert} from 'react-native';
import RNRestart from 'react-native-restart';
import {setJSExceptionHandler} from 'react-native-exception-handler';
const errorHandler = (e, isFatal) => {
if (isFatal) {
Alert.alert(
'Unexpected error occurred',
`
Error: ${(isFatal) ? 'Fatal:' : ''} ${e.name} ${e.message}
We will need to restart the app.
`,
[{
text: 'Restart',
onPress: () => {
RNRestart.Restart();
}
}]
);
} else {
console.log(e); // So that we can see it in the ADB logs in case of Android if needed
}
};
setJSExceptionHandler(errorHandler);
Bug Capture to dev team example
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 !
import {Alert} from 'react-native';
import {BackAndroid} from 'react-native';
import {setJSExceptionHandler} from 'react-native-exception-handler';
const reporter = (error) => {
// Logic for reporting to devs
// Example : Log issues to github issues using github apis.
console.log(error); // sample
};
const errorHandler = (e, isFatal) => {
if (isFatal) {
reporter(e);
Alert.alert(
'Unexpected error occurred',
`
Error: ${(isFatal) ? 'Fatal:' : ''} ${e.name} ${e.message}
We have reported this to our team ! Please close the app and start again!
`,
[{
text: 'Close',
onPress: () => {
BackAndroid.exitApp();
}
}]
);
} else {
console.log(e); // So that we can see it in the ADB logs in case of Android if needed
}
};
setJSExceptionHandler(errorHandler);
更多推荐

所有评论(0)