Skip to content

Instantly share code, notes, and snippets.

@cdesch
Created October 6, 2019 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cdesch/70a37724bd702c279ba081dd0c79b5eb to your computer and use it in GitHub Desktop.
Save cdesch/70a37724bd702c279ba081dd0c79b5eb to your computer and use it in GitHub Desktop.
ReactNative ErrorHandling Example - From Expo.io
const defaultHandler = (ErrorUtils.getGlobalHandler && ErrorUtils.getGlobalHandler()) || ErrorUtils._globalHandler;
const customErrorHandler = async (err, isFatal) => {
await AsyncStorage.setItem('lastError', JSON.stringify(err, Object.getOwnPropertyNames(err)));
return defaultHandler(err, isFatal);
};
ErrorUtils.setGlobalHandler(customErrorHandler);
//inside the App component:
async componentWillMount() {
const lastError = await AsyncStorage.getItem('lastError');
// dispatch error to logs
if (lastError) {
log({
sender: 'mobile-ops',
message: 'Fatal error !',
info: {
error: lastError
}
});
await AsyncStorage.removeItem('lastError');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment