Skip to content

Commit

Permalink
Merge ec10381 into 2fec8a4
Browse files Browse the repository at this point in the history
  • Loading branch information
kattrali committed Oct 31, 2018
2 parents 2fec8a4 + ec10381 commit a283ac3
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 30 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## TBD

### Bug fixes

* Allow setting context to null from callbacks
[#380](https://github.com/bugsnag/bugsnag-android/pull/380)

## 4.9.0 (2018-10-29)

### Enhancements
Expand Down
11 changes: 11 additions & 0 deletions features/before_send.feature
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,14 @@ Feature: Run callbacks before reports delivered
And the exception "type" equals "android"
And the event "severity" equals "error"
And the event "unhandled" is false

Scenario: Unset context
When I run "NotifyBeforeSendUnsetContextScenario"
Then I should receive a request
And the request is a valid for the error reporting API
And the exception "errorClass" equals "java.lang.Exception"
And the exception "message" equals "Registration failure"
And the event "context" is null
And the exception "type" equals "android"
And the event "severity" equals "error"
And the event "unhandled" is false
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.bugsnag.android.mazerunner.scenarios;

import com.bugsnag.android.Bugsnag;
import com.bugsnag.android.BeforeSend;
import com.bugsnag.android.Configuration;
import com.bugsnag.android.Report;
import com.bugsnag.android.Severity;

import android.content.Context;
import android.support.annotation.NonNull;


public class NotifyBeforeSendUnsetContextScenario extends Scenario {

public NotifyBeforeSendUnsetContextScenario(@NonNull Configuration config, @NonNull Context context) {
super(config, context);
config.setAutoCaptureSessions(false);
config.beforeSend(new BeforeSend() {
@Override
public boolean run(Report report) {
report.getError().setContext(null);
report.getError().setSeverity(Severity.ERROR);

return true;
}
});
}

@Override
public void run() {
super.run();
Bugsnag.notify(new Exception("Registration failure"));
}
}
15 changes: 0 additions & 15 deletions sdk/src/androidTest/java/com/bugsnag/android/ErrorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -314,28 +314,13 @@ public void testBugsnagExceptionName() throws Exception {
assertEquals("Busgang", err.getExceptionName());
}

@Test
public void testConfigContext() throws Exception {
String expected = "Junit test suite";
error.setContext(null);
config.setContext(expected);
assertEquals(expected, error.getContext());
}

@Test
public void testNullContext() throws Exception {
error.setContext(null);
error.setAppData(null);
assertNull(error.getContext());
}

@Test
public void testActiveScreen() throws Exception {
error.setContext(null);
error.getMetaData().addToTab("app", "activeScreen", "FooActivity");
assertEquals("FooActivity", error.getContext());
}

@Test
public void testSetUser() throws Exception {
String firstId = "123";
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/main/java/com/bugsnag/android/AppData.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private long calculateDurationInForeground() {
return client.sessionTracker.getDurationInForegroundMs(nowMs);
}

private String getActiveScreenClass() {
String getActiveScreenClass() {
return client.sessionTracker.getContextActivity();
}

Expand Down
4 changes: 4 additions & 0 deletions sdk/src/main/java/com/bugsnag/android/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,10 @@ void notify(@NonNull Error error,
// Attach user info to the error
error.setUser(user);

// Attach default context from active activity
String context = config.getContext();
error.setContext(context != null ? context : appData.getActiveScreenClass());

// Run beforeNotify tasks, don't notify if any return true
if (!runBeforeNotifyTasks(error)) {
Logger.info("Skipping notification - beforeNotify task returned false");
Expand Down
16 changes: 2 additions & 14 deletions sdk/src/main/java/com/bugsnag/android/Error.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void toStream(@NonNull JsonStream writer) throws IOException {

// Write error basics
writer.beginObject();
writer.name("context").value(getContext());
writer.name("context").value(context);
writer.name("metaData").value(mergedMetaData);

writer.name("severity").value(severity);
Expand Down Expand Up @@ -134,19 +134,7 @@ public void setContext(@Nullable String context) {
*/
@Nullable
public String getContext() {
if (!TextUtils.isEmpty(context)) {
return context;
} else if (config.getContext() != null) {
return config.getContext();
} else if (metaData != null) {
Map<String, Object> app = metaData.getTab("app");
Object activeScreen = app.get("activeScreen");

if (activeScreen instanceof String) {
return (String) activeScreen;
}
}
return null;
return context;
}

/**
Expand Down

0 comments on commit a283ac3

Please sign in to comment.