|
1 | 1 | # react-native-exception-handler
|
2 |
| -A react native module to help tackle the exception problem in react native. |
| 2 | + |
| 3 | +A react native module that lets you to register a global error handler that can capture fatal/non fatal uncaught exceptions. |
| 4 | +The module helps prevent abrupt crashing of RN Apps without a graceful message to the user. |
| 5 | + |
| 6 | +In the current scenario: |
| 7 | + - `In DEV mode , you get a RED Screen error pointing your JS errors.` |
| 8 | + - `In Bundled mode , the app just quits without any prompt !` 🙄 |
| 9 | + |
| 10 | +To tackle this we register a global error handler that could be used to for example: |
| 11 | +1. Send bug reports to dev team when the app crashes |
| 12 | +2. Show a creative dialog saying the user should restart the application |
| 13 | + |
| 14 | +### Installation: |
| 15 | + |
| 16 | +```sh |
| 17 | +yarn add react-native-exception-handler |
| 18 | +``` |
| 19 | + |
| 20 | +or |
| 21 | + |
| 22 | +```sh |
| 23 | +npm i react-native-exception-handler --save |
| 24 | +``` |
| 25 | + |
| 26 | + |
| 27 | +### Usage |
| 28 | + |
| 29 | +```js |
| 30 | +import {setJSExceptionHandler} from 'react-native-exception-handler'; |
| 31 | + |
| 32 | +. |
| 33 | +. |
| 34 | +. |
| 35 | + |
| 36 | +const errorHandler = (error, isFatal) => { |
| 37 | + // This is your custom global error handler |
| 38 | +} |
| 39 | + |
| 40 | +. |
| 41 | +. |
| 42 | +. |
| 43 | + |
| 44 | +setJSExceptionHandler(errorHandler); // registering the error handler (maybe u can do this in the index.android.js or index.ios.js) |
| 45 | + |
| 46 | +``` |
| 47 | + |
| 48 | + |
| 49 | +### Examples |
| 50 | + |
| 51 | +##### Restart on error example |
| 52 | + |
| 53 | +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 ! |
| 54 | + |
| 55 | +```js |
| 56 | +import {Alert} from 'react-native'; |
| 57 | +import RNRestart from 'react-native-restart'; |
| 58 | +import {setJSExceptionHandler} from 'react-native-exception-handler'; |
| 59 | + |
| 60 | +const errorHandler = (e, isFatal) => { |
| 61 | + if (isFatal) { |
| 62 | + Alert.alert( |
| 63 | + 'Unexpected error occurred', |
| 64 | + ` |
| 65 | + Error: ${(isFatal) ? 'Fatal:' : ''} ${e.name} ${e.message} |
| 66 | +
|
| 67 | + We will need to restart the app. |
| 68 | + `, |
| 69 | + [{ |
| 70 | + text: 'Restart', |
| 71 | + onPress: () => { |
| 72 | + RNRestart.Restart(); |
| 73 | + } |
| 74 | + }] |
| 75 | + ); |
| 76 | + } else { |
| 77 | + console.log(e); // So that we can see it in the ADB logs in case of Android if needed |
| 78 | + } |
| 79 | +}; |
| 80 | + |
| 81 | +setJSExceptionHandler(errorHandler); |
| 82 | +``` |
| 83 | + |
| 84 | +#### Bug Capture to dev team example |
| 85 | + |
| 86 | +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 ! |
| 87 | + |
| 88 | +```js |
| 89 | +import {Alert} from 'react-native'; |
| 90 | +import {BackAndroid} from 'react-native'; |
| 91 | +import {setJSExceptionHandler} from 'react-native-exception-handler'; |
| 92 | + |
| 93 | +const reporter = (error) => { |
| 94 | + // Logic for reporting to devs |
| 95 | + // Example : Log issues to github issues using github apis. |
| 96 | + console.log(error); // sample |
| 97 | +}; |
| 98 | + |
| 99 | +const errorHandler = (e, isFatal) => { |
| 100 | + if (isFatal) { |
| 101 | + reporter(e); |
| 102 | + Alert.alert( |
| 103 | + 'Unexpected error occurred', |
| 104 | + ` |
| 105 | + Error: ${(isFatal) ? 'Fatal:' : ''} ${e.name} ${e.message} |
| 106 | +
|
| 107 | + We have reported this to our team ! Please close the app and start again! |
| 108 | + `, |
| 109 | + [{ |
| 110 | + text: 'Close', |
| 111 | + onPress: () => { |
| 112 | + BackAndroid.exitApp(); |
| 113 | + } |
| 114 | + }] |
| 115 | + ); |
| 116 | + } else { |
| 117 | + console.log(e); // So that we can see it in the ADB logs in case of Android if needed |
| 118 | + } |
| 119 | +}; |
| 120 | + |
| 121 | +setJSExceptionHandler(errorHandler); |
| 122 | +``` |
| 123 | + |
| 124 | +*More Examples can be found in the examples folder* |
0 commit comments