Skip to content

Commit

Permalink
Run beforeSend callbacks before delivery
Browse files Browse the repository at this point in the history
  • Loading branch information
kattrali committed Oct 24, 2018
1 parent b5b5f3f commit 4448f2f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,12 @@ public void testFileQueueDuplication() {
* Ensures that the file can be serialised back into a JSON report, and contains the same info
* as the original
*/
private static void checkFileMatchesErrorReport(File file, Error error) throws Exception {
private void checkFileMatchesErrorReport(File file, Error error) throws Exception {
// ensure that the file isn't empty
assertFalse(file.length() <= 0);

// ensure the file can be serialised into JSON report
JSONObject memory = getJsonObjectFromReport(new Report("api-key", file));
JSONObject memory = getJsonObjectFromReport(new Report(config, file));
JSONObject disk = getJsonObjectFromReport(new Report("api-key", error));

// validate info
Expand Down
19 changes: 19 additions & 0 deletions sdk/src/main/java/com/bugsnag/android/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -1207,6 +1207,10 @@ public void disableExceptionHandler() {
}

void deliver(@NonNull Report report, @NonNull Error error) {
if (!runBeforeSendTasks(report)) {
Logger.info("Skipping notification - beforeSend task returned false");
return;
}
try {
config.getDelivery().deliver(report, config);
Logger.info("Sent 1 new error to Bugsnag");
Expand Down Expand Up @@ -1238,6 +1242,21 @@ void cacheAndNotify(@NonNull Throwable exception, Severity severity, MetaData me
notify(error, DeliveryStyle.ASYNC_WITH_CACHE, null);
}

private boolean runBeforeSendTasks(Report report) {
for (BeforeSend beforeSend : config.getBeforeSendTasks()) {
try {
if (!beforeSend.run(report)) {
return false;
}
} catch (Throwable ex) {
Logger.warn("BeforeSend threw an Exception", ex);
}
}

// By default, allow the error to be sent if there were no objections
return true;
}

private boolean runBeforeNotifyTasks(Error error) {
for (BeforeNotify beforeNotify : config.getBeforeNotifyTasks()) {
try {
Expand Down
13 changes: 12 additions & 1 deletion sdk/src/main/java/com/bugsnag/android/ErrorStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,18 @@ private void flushReports(Collection<File> storedReports) {

private void flushErrorReport(File errorFile) {
try {
Report report = new Report(config.getApiKey(), errorFile);
Report report = new Report(config, errorFile);
for (BeforeSend beforeSend : config.getBeforeSendTasks()) {
try {
if (!beforeSend.run(report)) {
deleteStoredFiles(Collections.singleton(errorFile));
Logger.info("Deleting cancelled error file " + errorFile.getName());
return;
}
} catch (Throwable ex) {
Logger.warn("BeforeSend threw an Exception", ex);
}
}
config.getDelivery().deliver(report, config);

deleteStoredFiles(Collections.singleton(errorFile));
Expand Down
19 changes: 4 additions & 15 deletions sdk/src/main/java/com/bugsnag/android/Report.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
*/
public class Report implements JsonStream.Streamable {

@Nullable
private final File errorFile;

@Nullable
private final Error error;

Expand All @@ -26,16 +23,14 @@ public class Report implements JsonStream.Streamable {
@NonNull
private String apiKey;

Report(@NonNull String apiKey, @Nullable File errorFile) {
this.error = null;
this.errorFile = errorFile;
Report(@NonNull Configuration config, @Nullable File errorFile) throws IOException {
this.error = ErrorReader.readError(config, errorFile);
this.notifier = Notifier.getInstance();
this.apiKey = apiKey;
this.apiKey = config.getApiKey();
}

Report(@NonNull String apiKey, @Nullable Error error) {
this.error = error;
this.errorFile = null;
this.notifier = Notifier.getInstance();
this.apiKey = apiKey;
}
Expand All @@ -55,13 +50,7 @@ public void toStream(@NonNull JsonStream writer) throws IOException {
writer.name("events").beginArray();

// Write in-memory event
if (error != null) {
writer.value(error);
} else if (errorFile != null) { // Write on-disk event
writer.value(errorFile);
} else {
Logger.warn("Expected error or errorFile, found empty payload instead");
}
writer.value(error);

// End events array
writer.endArray();
Expand Down

0 comments on commit 4448f2f

Please sign in to comment.