Merge pull request #19 from Instabug/feature/introduse-crash-reporting-for-js-crashes

Feature/introduse crash reporting for js crashes
This commit is contained in:
Hossam Hassan 2017-02-01 17:46:29 +02:00 committed by GitHub
commit fba29cb055
5 changed files with 64 additions and 2 deletions

1
.gitignore vendored
View File

@ -6,3 +6,4 @@ android/.DS_Store
ios/.DS_Store ios/.DS_Store
build/ build/
/android/src/main/java/com/instabug/reactlibrary/wrappedAPIs

View File

@ -6,6 +6,9 @@ import android.net.Uri;
import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule; import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod; import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.instabug.library.Instabug; import com.instabug.library.Instabug;
import com.instabug.library.internal.module.InstabugLocale; import com.instabug.library.internal.module.InstabugLocale;
import com.instabug.library.invocation.InstabugInvocationEvent; import com.instabug.library.invocation.InstabugInvocationEvent;
@ -430,6 +433,36 @@ public class RNInstabugReactnativeModule extends ReactContextBaseJavaModule {
} }
} }
/**
* Report a caught exception to Instabug dashboard
*
* @param throwable the exception to be reported
*/
@ReactMethod
public void reportJsException(ReadableArray stack, String message, String errorIdentifier) {
try {
int size = stack != null ? stack.size() : 0;
StackTraceElement[] stackTraceElements = new StackTraceElement[size];
for (int i = 0; i < size; i++) {
ReadableMap frame = stack.getMap(i);
String methodName = frame.getString("methodName");
String fileName = frame.getString("file");
int lineNumber = frame.getInt("lineNumber");
stackTraceElements[i] = new StackTraceElement(fileName, methodName, fileName, lineNumber);
}
Throwable throwable = new Throwable(message);
throwable.setStackTrace(stackTraceElements);
if (errorIdentifier != null)
mInstabug.reportException(throwable);
else
mInstabug.reportException(throwable, errorIdentifier);
} catch (Exception e) {
e.printStackTrace();
}
}
private Locale getLocaleByKey(String instabugLocale) { private Locale getLocaleByKey(String instabugLocale) {
String localeInLowerCase = instabugLocale.toLowerCase(); String localeInLowerCase = instabugLocale.toLowerCase();
switch (localeInLowerCase) { switch (localeInLowerCase) {

View File

@ -5,8 +5,8 @@
*/ */
import {NativeModules, NativeAppEventEmitter, Platform} from 'react-native'; import {NativeModules, NativeAppEventEmitter, Platform} from 'react-native';
let {Instabug} = NativeModules; let {Instabug} = NativeModules;
import instabugParser from './utils/instabugParser.js';
/** /**
* Instabug * Instabug
@ -464,6 +464,18 @@ module.exports = {
Instabug.isInstabugNotification(dict, isInstabugNotificationCallback); Instabug.isInstabugNotification(dict, isInstabugNotificationCallback);
}, },
reportJsException: function (error, errorIdentifier) {
if (!error || !error instanceof Error)
throw new Error("You should pass an error object");
let jsStackTrace = instabugParser(error);
if (!errorIdentifier)
Instabug.reportJsException(jsStackTrace, error.message, null);
else if (errorIdentifier) {
Instabug.reportJsException(jsStackTrace, error.message, errorIdentifier);
}
},
/** /**
* The event used to invoke the feedback form * The event used to invoke the feedback form
* @readonly * @readonly

View File

@ -28,5 +28,8 @@
"android": { "android": {
"packageInstance": "new RNInstabugReactnativePackage(\"YOUR_ANDROID_APPLICATION_TOKEN\",MainApplication.this,\"button\")" "packageInstance": "new RNInstabugReactnativePackage(\"YOUR_ANDROID_APPLICATION_TOKEN\",MainApplication.this,\"button\")"
} }
},
"dependencies": {
"stacktrace-parser": "0.1.3"
} }
} }

13
utils/instabugParser.js Normal file
View File

@ -0,0 +1,13 @@
'use strict';
var stacktraceParser = require('stacktrace-parser');
function parseErrorStack(error) {
if (!error || !error.stack) {
return [];
}
return Array.isArray(error.stack) ? error.stack :
stacktraceParser.parse(error.stack);
}
module.exports = parseErrorStack;