Skip to content

Commit

Permalink
add ability to pass stack trace to nonFatalCrash
Browse files Browse the repository at this point in the history
  • Loading branch information
wodka committed Nov 23, 2016
1 parent 887d790 commit 54bd1b1
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 8 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ window.fabric.Crashlytics.sendNonFatalCrash("Error message");
//iOS only. Send message and error code
window.fabric.Crashlytics.addLog("about to send a non fatal crash for testing!");
window.fabric.Crashlytics.recordError("Error message", -1);

//Android only. Send stack trace with non fatal crash (requires https://www.stacktracejs.com/)
window.fabric.Crashlytics.sendNonFatalCrash("Error message", StackTrace.getSync());
```

Issue Grouping
Expand Down
21 changes: 19 additions & 2 deletions src/android/FabricPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Currency;
import java.util.Iterator;

Expand Down Expand Up @@ -122,11 +123,27 @@ public void run() {

private void sendNonFatalCrash(final JSONArray data,
final CallbackContext callbackContext) {

this.cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
Crashlytics.logException(new Throwable(data.optString(0, "No Message Provided")));
if (data.length == 2) {
// well, we got more, let's asume arg 2 was a stack trace
JSONArray stackTrace = args.getJSONArray(1);

ArrayList<StackTraceElement> trace = new ArrayList<StackTraceElement>();
for(int i = 0; i < stackTrace.length(); i++) {
JSONObject elem = stackTrace.getJSONObject(i);

trace.add(new StackTraceElement("undefined", elem.get("functionName"),elem.get("fileName"), elem.get("lineNumber")));
}

JavaScriptException ex = new JavaScriptException(data.getString(0));
ex.setStackTrace((StackTraceElement[])trace.toArray());

Crashlytics.logException(ex);
} else {
Crashlytics.logException(new Throwable(data.optString(0, "No Message Provided")));
}
}
});
}
Expand Down
4 changes: 4 additions & 0 deletions src/android/JavaScriptException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.sarriaroman.fabric;

public class JavaScriptException extends Exception {
}
25 changes: 21 additions & 4 deletions www/FabricPlugin.Crashlytics.es6
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,27 @@ class FabricCrashlytics {
]);
}

sendNonFatalCrash(message) {
window.fabric.core.execPlugin('sendNonFatalCrash', [
message
]);
sendNonFatalCrash(message, trace) {
let params = [message];

if (trace) {
// validate trace (easier here)
let tmp = [];

for (let entry of trace) {
tmp.push({
functionName: entry.functionName || 'unknown',
fileName: entry.fileName || 'unknown',
lineNumber: entry.lineNumber || 0
});
}

if (tmp.length > 0) {
params.push(tmp);
}
}

window.fabric.core.execPlugin('sendNonFatalCrash', params);
}

recordError(message, code) {
Expand Down
44 changes: 42 additions & 2 deletions www/FabricPlugin.Crashlytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,48 @@ var FabricCrashlytics = (function () {
}
}, {
key: 'sendNonFatalCrash',
value: function sendNonFatalCrash(message) {
window.fabric.core.execPlugin('sendNonFatalCrash', [message]);
value: function sendNonFatalCrash(message, trace) {
var params = [message];

if (trace) {
// validate trace (easier here)
var tmp = [];

var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;

try {
for (var _iterator = trace[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var entry = _step.value;

tmp.push({
functionName: entry.functionName || 'unknown',
fileName: entry.fileName || 'unknown',
lineNumber: entry.lineNumber || 0
});
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator['return']) {
_iterator['return']();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}

if (tmp.length > 0) {
params.push(tmp);
}
}

window.fabric.core.execPlugin('sendNonFatalCrash', params);
}
}, {
key: 'recordError',
Expand Down

0 comments on commit 54bd1b1

Please sign in to comment.