react-native-instantpay-exception-handler
v1.0.1
Published
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.
Downloads
14
Maintainers
Readme
react-native-instantpay-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.
Installation
npm install react-native-instantpay-exception-handler
PLEASE READ BEFORE GOING TO USAGE SECTION
In a React Native app, errors can be categorized into two types:
- JS_Exceptions: These are errors generated by your JavaScript code, including all your React components.
- Native_Exceptions: These are errors produced by native modules.
Unhandled exceptions can leave the app in a critical state.
For JS_Exceptions, you can catch unhandled errors and perform tasks such as displaying alerts or popups, cleaning up resources, or even hitting an API to notify the development team before closing the app.
For Native_Exceptions, the situation is more severe. Although you can catch these unhandled errors and perform tasks like cleanup, logging out, or notifying the development team via an API, you cannot interact with the JS UI layer (e.g., showing a JS alert box). Instead, you must handle any UI-related actions using native methods provided by the module in the respective native codebases for iOS and Android. The module does come with default handlers that provide basic error popups, which you can customize. See the CUSTOMIZATION section for more details.
Usage
To catch JS_Exceptions
import RNException from 'react-native-instantpay-exception-handler';
// ...
.
.
// For most use cases:
// registering the error handler (maybe u can do this in the index.android.js or index.ios.js)
RNException.setJSExceptionHandler((error, isFatal) => {
// This is your custom global error handler
// You do stuff like show an error dialog
// or hit google analytics to track crashes
// or hit a custom api to inform the dev team.
});
//=================================================
// ADVANCED use case:
const exceptionhandler = (error, isFatal) => {
// your error handler function
};
RNException.setJSExceptionHandler(exceptionhandler, allowInDevMode);
// - exceptionhandler is the exception handler function
// - allowInDevMode is an optional parameter is a boolean.
// If set to true the handler to be called in place of RED screen
// in development mode also.
// getJSExceptionHandler gives the currently set JS exception handler
const currentHandler = RNException.getJSExceptionHandler();
To catch Native_Exceptions
import RNException from 'react-native-instantpay-exception-handler';
//For most use cases:
RNException.setNativeExceptionHandler((exceptionString) => {
// This is your custom global error handler
// You do stuff likehit google analytics to track crashes.
// or hit a custom api to inform the dev team.
//NOTE: alert or showing any UI change via JS
//WILL NOT WORK in case of NATIVE ERRORS.
});
//====================================================
// ADVANCED use case:
const exceptionhandler = (exceptionString) => {
// your exception handler code here
};
RNException.setNativeExceptionHandler(
exceptionhandler,
forceAppQuit,
executeDefaultHandler
);
// - exceptionhandler is the exception handler function
// - forceAppQuit is an optional ANDROID specific parameter that defines
// if the app should be force quit on error. default value is true.
// To see usecase check the common issues section.
// - executeDefaultHandler is an optional boolean (both IOS, ANDROID)
// It executes previous exception handlers if set by some other module.
// It will come handy when you use any other crash analytics module along with this one
// Default value is set to false. Set to true if you are using other analytics modules.
It is recommended you set both the handlers.
NOTE: setNativeExceptionHandler
only works in bundled mode - it will show the red screen when applied to dev mode.
Use Cases In Android
Open up android/app/src/main/java/[...]/MainApplication.java
- Add
import com.instantpayexceptionhandler.InstantpayExceptionHandlerModule
to the imports at the top of the file - Add below code inside
onCreate()
method
InstantpayExceptionHandlerModule.setAplicationContext = this
InstantpayExceptionHandlerModule.catchNativeException()
License
MIT
Created By Instantpay