Skip to content

Commit

Permalink
feat: add public accessors to error class
Browse files Browse the repository at this point in the history
  • Loading branch information
fractalwrench committed Jun 19, 2018
1 parent f0eac1f commit dcc3f0a
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 19 deletions.
6 changes: 5 additions & 1 deletion sdk/src/androidTest/java/com/bugsnag/android/ErrorTest.java
Expand Up @@ -216,6 +216,7 @@ public void testSetContext() throws JSONException, IOException {
public void testSetGroupingHash() throws JSONException, IOException {
String groupingHash = "herpderp";
error.setGroupingHash(groupingHash);
assertEquals(groupingHash, error.getGroupingHash());

JSONObject errorJson = streamableToJson(error);
assertEquals(groupingHash, errorJson.get("groupingHash"));
Expand Down Expand Up @@ -313,7 +314,9 @@ public void testAppDataContext() throws Exception {
String expectedContext = "FooActivity";
sessionTracker.updateForegroundTracker(expectedContext,
true, System.currentTimeMillis());
error.setAppData(new AppData(context, config, sessionTracker));
AppData appData = new AppData(context, config, sessionTracker);
error.setAppData(appData);
assertEquals(appData, error.getAppData());
assertEquals(expectedContext, error.getContext());
}

Expand Down Expand Up @@ -375,6 +378,7 @@ public void testSetDeviceId() throws Throwable {
Context context = InstrumentationRegistry.getContext();
DeviceData deviceData = new DeviceData(context, BugsnagTestUtils.getSharedPrefs(context));
error.setDeviceData(deviceData);
assertEquals(deviceData, error.getDeviceData());

JSONObject errorJson = streamableToJson(error);
JSONObject device = errorJson.getJSONObject("device");
Expand Down
3 changes: 3 additions & 0 deletions sdk/src/main/java/com/bugsnag/android/Client.java
Expand Up @@ -68,7 +68,10 @@ public class Client extends Observable implements Observer {
protected final DeviceData deviceData;
@NonNull
final Breadcrumbs breadcrumbs;

@NonNull
protected final User user = new User();

@NonNull
protected final ErrorStore errorStore;

Expand Down
90 changes: 72 additions & 18 deletions sdk/src/main/java/com/bugsnag/android/Error.java
Expand Up @@ -2,7 +2,6 @@

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import java.io.IOException;

Expand All @@ -17,29 +16,52 @@
*/
public class Error implements JsonStream.Streamable {

@SuppressWarnings("NullableProblems") // set after construction
@NonNull
final Configuration config;
private AppData appData;

@SuppressWarnings("NullableProblems") // set after construction
@NonNull
private DeviceData deviceData;
private Breadcrumbs breadcrumbs;

@SuppressWarnings("NullableProblems") // set after construction
@NonNull
private User user;
private final Throwable exception;
private Severity severity = Severity.WARNING;
@NonNull private MetaData metaData = new MetaData();

@Nullable
private Severity severity;

@NonNull
private MetaData metaData = new MetaData();

@Nullable
private String groupingHash;

@Nullable
private String context;

@NonNull
final Configuration config;
private final String[] projectPackages;
private final Exceptions exceptions;
private Breadcrumbs breadcrumbs;
private final Throwable exception;
private final HandledState handledState;
private final Session session;
private final ThreadState threadState;

Error(@NonNull Configuration config, @NonNull Throwable exception,
HandledState handledState, Severity severity, Session session, ThreadState threadState) {
HandledState handledState, @NonNull Severity severity,
Session session, ThreadState threadState) {
this.threadState = threadState;
this.config = config;
this.exception = exception;
this.handledState = handledState;
this.severity = severity;
this.session = session;

projectPackages = config.getProjectPackages();
exceptions = new Exceptions(config, exception);
}

@Override
Expand All @@ -56,16 +78,16 @@ public void toStream(@NonNull JsonStream writer) throws IOException {
writer.name("severityReason").value(handledState);
writer.name("unhandled").value(handledState.isUnhandled());

if (config.getProjectPackages() != null) {
if (projectPackages != null) {
writer.name("projectPackages").beginArray();
for (String projectPackage : config.getProjectPackages()) {
for (String projectPackage : projectPackages) {
writer.value(projectPackage);
}
writer.endArray();
}

// Write exception info
writer.name("exceptions").value(new Exceptions(config, exception));
writer.name("exceptions").value(exceptions);

// Write user info
writer.name("user").value(user);
Expand Down Expand Up @@ -103,7 +125,7 @@ public void toStream(@NonNull JsonStream writer) throws IOException {
*
* @param context what was happening at the time of a crash
*/
public void setContext(String context) {
public void setContext(@Nullable String context) {
this.context = context;
}

Expand All @@ -112,7 +134,7 @@ public void setContext(String context) {
*/
@Nullable
public String getContext() {
if (!TextUtils.isEmpty(context)) {
if (context != null) {
return context;
} else if (config.getContext() != null) {
return config.getContext();
Expand All @@ -131,10 +153,20 @@ public String getContext() {
*
* @param groupingHash a string to use when grouping errors
*/
public void setGroupingHash(String groupingHash) {
public void setGroupingHash(@Nullable String groupingHash) {
this.groupingHash = groupingHash;
}

/**
* Get the grouping hash associated with this Error.
*
* @return the grouping hash, if set
*/
@Nullable
public String getGroupingHash() {
return groupingHash;
}

/**
* Set the Severity of this Error.
* <p>
Expand Down Expand Up @@ -172,13 +204,14 @@ public void setUser(String id, String email, String name) {
this.user = new User(id, email, name);
}

void setUser(User user) {
void setUser(@NonNull User user) {
this.user = user;
}

/**
* @return user information associated with this Error
*/
@NonNull
public User getUser() {
return user;
}
Expand Down Expand Up @@ -286,7 +319,8 @@ public String getExceptionName() {
/**
* Get the message from the exception contained in this Error report.
*/
@NonNull public String getExceptionMessage() {
@NonNull
public String getExceptionMessage() {
String localizedMessage = exception.getLocalizedMessage();
return localizedMessage != null ? localizedMessage : "";
}
Expand All @@ -307,11 +341,31 @@ public void setDeviceId(@Nullable String id) {
deviceData.id = id;
}

void setAppData(AppData appData) {
/**
* Retrieves the {@link AppData} associated with this error
*
* @return the app metadata
*/
@NonNull
public AppData getAppData() {
return appData;
}
/**
* Retrieves the {@link DeviceData} associated with this error
*
* @return the device metadata
*/

@NonNull
public DeviceData getDeviceData() {
return deviceData;
}

void setAppData(@NonNull AppData appData) {
this.appData = appData;
}

void setDeviceData(DeviceData deviceData) {
void setDeviceData(@NonNull DeviceData deviceData) {
this.deviceData = deviceData;
}

Expand Down Expand Up @@ -354,7 +408,7 @@ static class Builder {
}

Builder(@NonNull Configuration config, @NonNull String name,
@NonNull String message, @NonNull StackTraceElement[] frames, Session session) {
@NonNull String message, @NonNull StackTraceElement[] frames, Session session) {
this(config, new BugsnagException(name, message, frames), session);
}

Expand Down

0 comments on commit dcc3f0a

Please sign in to comment.