Skip to content

Commit

Permalink
Bumped version and added readme.
Browse files Browse the repository at this point in the history
  • Loading branch information
a7ul committed Jul 27, 2018
1 parent dca5aa6 commit e03a7c4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 63 deletions.
55 changes: 34 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ To tackle this we register a global error handler that could be used to for exam
**V2 of this module now supports catching Unhandled Native Exceptions also along with the JS Exceptions ✌🏻🍻**
There are **NO** breaking changes. So its safe to upgrade from v1 to v2. So there is no reason not to 😉.

**V2.9**
- Adds support for executing previously set error handlers (now this module can work with other analytics modules)
- Adds an improved approach for overwriting native error handlers.
- Thanks @ [Damien Solimando](https://github.com/dsolimando)

**Example** repo can be found here:
*[https://github.com/master-atul/react-native-exception-handler-example](https://github.com/master-atul/react-native-exception-handler-example) *

Expand Down Expand Up @@ -128,7 +133,7 @@ setJSExceptionHandler((error, isFatal) => {
// 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
Expand Down Expand Up @@ -156,17 +161,21 @@ setNativeExceptionHandler((exceptionString) => {
//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
}
setNativeExceptionHandler(exceptionhandler,forceAppQuit);
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.

// 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.

Expand Down Expand Up @@ -201,15 +210,15 @@ In Android and iOS you will see something like
<img src="https://github.com/master-atul/react-native-exception-handler/raw/master/screens/ios_native_exception.png" width="300"/>
</p>

**Modifying Android Native Exception handler UI** (NATIVE CODE HAS TO BE WRITTEN) *recommended that you do this in android studio*
**Modifying Android Native Exception handler (RECOMMENDED APPROACH)**

- Create an Empty Activity in the `android/app/src/main/java/[...]/`. For example lets say CustomErrorDialog.java
- Customize your activity to look and behave however you need it to be.
- In the `android/app/src/main/java/[...]/MainApplication.java`
(NATIVE CODE HAS TO BE WRITTEN) *recommended that you do this in android studio*

- In the `android/app/src/main/java/[...]/MainActivity.java`

```java
import com.masteratul.exceptionhandler.ReactNativeExceptionHandlerModule;
import <yourpackage>.YourCustomActivity; //This is your CustomErrorDialog.java
import com.masteratul.exceptionhandler.NativeExceptionHandlerIfc
...
...
...
Expand All @@ -221,18 +230,27 @@ public class MainApplication extends Application implements ReactApplication {
....
....
....
ReactNativeExceptionHandlerModule.replaceErrorScreenActivityClass(YourCustomActivity.class); //This will replace the native error handler popup with your own custom activity.
ReactNativeExceptionHandlerModule.setNativeExceptionHandler(new NativeExceptionHandlerIfc() {
@Override
public void handleNativeException(Thread thread, Throwable throwable, Thread.UncaughtExceptionHandler originalHandler) {
// Put your error handling code here
}
}//This will override the default behaviour of displaying the recover activity.
}

```

**Modifying Android Native Exception handler** (NATIVE CODE HAS TO BE WRITTEN) *recommended that you do this in android studio*
**Modifying Android Native Exception handler UI (CUSTOM ACTIVITY APPROACH (OLD APPROACH).. LEAVING FOR BACKWARD COMPATIBILITY)**

- In the `android/app/src/main/java/[...]/MainActivity.java`
(NATIVE CODE HAS TO BE WRITTEN) *recommended that you do this in android studio*

- Create an Empty Activity in the `android/app/src/main/java/[...]/`. For example lets say CustomErrorDialog.java
- Customize your activity to look and behave however you need it to be.
- In the `android/app/src/main/java/[...]/MainApplication.java`

```java
import com.masteratul.exceptionhandler.ReactNativeExceptionHandlerModule;
import com.masteratul.exceptionhandler.NativeExceptionHandlerIfc
import <yourpackage>.YourCustomActivity; //This is your CustomErrorDialog.java
...
...
...
Expand All @@ -244,12 +262,7 @@ public class MainApplication extends Application implements ReactApplication {
....
....
....
ReactNativeExceptionHandlerModule.setNativeExceptionHandler(new NativeExceptionHandlerIfc() {
@Override
public void handleNativeException(Thread thread, Throwable throwable, Thread.UncaughtExceptionHandler originalHandler) {
// Put your error handling code here
}
}//This will override the default behaviour of displaying the recover activity.
ReactNativeExceptionHandlerModule.replaceErrorScreenActivityClass(YourCustomActivity.class); //This will replace the native error handler popup with your own custom activity.
}

```
Expand Down
52 changes: 11 additions & 41 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,63 +4,33 @@ const { ReactNativeExceptionHandler } = NativeModules;

const noop = () => { };

export const setJSExceptionHandler = (
customHandler = noop,
allowedInDevMode = false
) => {
if (
typeof allowedInDevMode !== "boolean" ||
typeof customHandler !== "function"
) {
console.log(
"setJSExceptionHandler is called with wrong argument types.. first argument should be callback function and second argument is optional should be a boolean"
);
console.log(
"Not setting the JS handler .. please fix setJSExceptionHandler call"
);
export const setJSExceptionHandler = (customHandler = noop, allowedInDevMode = false) => {
if (typeof allowedInDevMode !== "boolean" || typeof customHandler !== "function"){
console.log("setJSExceptionHandler is called with wrong argument types.. first argument should be callback function and second argument is optional should be a boolean");
console.log("Not setting the JS handler .. please fix setJSExceptionHandler call");
return;
}
const allowed = allowedInDevMode ? true : !__DEV__;
if (allowed) {
global.ErrorUtils.setGlobalHandler(customHandler);
console.error = (message, error) => global.ErrorUtils.reportError(error); // sending console.error so that it can be caught
} else {
console.log(
"Skipping setJSExceptionHandler: Reason: In DEV mode and allowedInDevMode = false"
);
console.log("Skipping setJSExceptionHandler: Reason: In DEV mode and allowedInDevMode = false");
}
};

export const getJSExceptionHandler = () => global.ErrorUtils.getGlobalHandler();

export const setNativeExceptionHandler = (
customErrorHandler = noop,
forceApplicationToQuit = true,
executeDefaultHandler = false
) => {
if (
typeof customErrorHandler !== "function" ||
typeof forceApplicationToQuit !== "boolean"
) {
console.log(
"setNativeExceptionHandler is called with wrong argument types.. first argument should be callback function and second argument is optional should be a boolean"
);
console.log(
"Not setting the native handler .. please fix setNativeExceptionHandler call"
);
export const setNativeExceptionHandler = (customErrorHandler = noop, forceApplicationToQuit = true, executeDefaultHandler = false) => {
if (typeof customErrorHandler !== "function" || typeof forceApplicationToQuit !== "boolean") {
console.log("setNativeExceptionHandler is called with wrong argument types.. first argument should be callback function and second argument is optional should be a boolean");
console.log("Not setting the native handler .. please fix setNativeExceptionHandler call");
return;
}
if (Platform.OS === "ios") {
ReactNativeExceptionHandler.setHandlerforNativeException(
executeDefaultHandler,
customErrorHandler
);
ReactNativeExceptionHandler.setHandlerforNativeException(executeDefaultHandler, customErrorHandler);
} else {
ReactNativeExceptionHandler.setHandlerforNativeException(
executeDefaultHandler,
forceApplicationToQuit,
customErrorHandler
);
ReactNativeExceptionHandler.setHandlerforNativeException(executeDefaultHandler, forceApplicationToQuit, customErrorHandler);
}
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-exception-handler",
"version": "2.8.9",
"version": "2.9.0",
"description": "A react native module that lets you to register a global error handler that can capture fatal/non fatal uncaught exceptions.",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit e03a7c4

Please sign in to comment.