Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(crashlytics): Extend recordException to provide a possibility to add custom properties per exception and not only globally #825

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/giant-kids-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@capacitor-firebase/crashlytics': minor
---

[android]
- adds the possibility to add custom properties when recording a non-fatal exception as outlined here:
> https://firebase.google.com/docs/crashlytics/customize-crash-reports?platform=android#log-excepts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import com.getcapacitor.JSArray;
import com.getcapacitor.PluginCall;
import com.google.firebase.crashlytics.CustomKeysAndValues;

import org.json.JSONException;
import org.json.JSONObject;

public class FirebaseCrashlytics {

Expand Down Expand Up @@ -56,9 +59,67 @@ public void deleteUnsentReports() {
getFirebaseCrashlyticsInstance().deleteUnsentReports();
}

public void recordException(String message, JSArray stacktrace) {
public void recordException(String message, JSArray stacktrace, JSArray customProperties) {
Throwable throwable = getJavaScriptException(message, stacktrace);
getFirebaseCrashlyticsInstance().recordException(throwable);
CustomKeysAndValues keysAndValues = getCustomKeysAndValues(customProperties);
getFirebaseCrashlyticsInstance().recordException(throwable, keysAndValues);
}

private CustomKeysAndValues getCustomKeysAndValues(JSArray keysAndValues) {
if (keysAndValues == null) {
return null;
}

CustomKeysAndValues.Builder builder = new CustomKeysAndValues.Builder();
try {
for (int i = 0; i < keysAndValues.length(); i++) {
JSONObject object = keysAndValues.getJSONObject(i);
String type = object.getString("type");
String key = object.getString("key");
Object value = getValueForType(type, object);

if (value != null) {
addCustomKeyToBuilder(builder, key, type, value);
}
}
} catch (JSONException error) {
System.err.println("CustomProperties cannot be converted to CustomKeysAndValues! " + error.getMessage());
}

return builder.build();
}

private Object getValueForType(String type, JSONObject object) throws JSONException {
switch (type) {
case "long":
return Long.valueOf(object.getInt("value"));
case "int":
return object.getInt("value");
case "boolean":
return object.getBoolean("value");
case "float":
return (float) object.getDouble("value");
case "double":
return object.getDouble("value");
default:
return object.getString("value");
}
}

private void addCustomKeyToBuilder(CustomKeysAndValues.Builder builder, String key, String type, Object value) {
if (value instanceof Long) {
builder.putLong(key, (Long) value);
} else if (value instanceof Integer) {
builder.putInt(key, (Integer) value);
} else if (value instanceof Boolean) {
builder.putBoolean(key, (Boolean) value);
} else if (value instanceof Float) {
builder.putFloat(key, (Float) value);
} else if (value instanceof Double) {
builder.putDouble(key, (Double) value);
} else if (value instanceof String) {
builder.putString(key, (String) value);
}
}

private com.google.firebase.crashlytics.FirebaseCrashlytics getFirebaseCrashlyticsInstance() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ public void recordException(PluginCall call) {
}

JSArray stacktrace = call.getArray("stacktrace", null);
implementation.recordException(message, stacktrace);
JSArray customProperties = call.getArray("customProperties", null);
implementation.recordException(message, stacktrace, customProperties);
call.resolve();
} catch (Exception exception) {
Logger.error(TAG, exception.getMessage(), exception);
Expand Down
8 changes: 8 additions & 0 deletions packages/crashlytics/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,14 @@ export interface RecordExceptionOptions {
* @since 1.1.0
*/
stacktrace?: StackFrame[];
/**
* Sets a custom key and value that is associated with exactly this report.
*
* Only available for Android.
*
* @since 7.1.0
*/
customProperties?: SetCustomKeyOptions[];
}

/**
Expand Down