Created
October 6, 2019 15:42
-
-
Save cdesch/70a37724bd702c279ba081dd0c79b5eb to your computer and use it in GitHub Desktop.
ReactNative ErrorHandling Example - From Expo.io
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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