Skip to content

Commit a531075

Browse files
authored
rename callbacks and update functionality (#241)
1 parent 4de3d6a commit a531075

File tree

39 files changed

+313
-214
lines changed

39 files changed

+313
-214
lines changed

bugsnag-spring/src/main/java/com/bugsnag/BugsnagSpringConfiguration.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,26 @@ public class BugsnagSpringConfiguration implements InitializingBean {
2828
Callback springVersionErrorCallback() {
2929
Callback callback = new Callback() {
3030
@Override
31-
public void beforeNotify(Report report) {
31+
public boolean onError(Report report) {
3232
addSpringRuntimeVersion(report.getDevice());
33+
return true;
3334
}
3435
};
3536
bugsnag.addCallback(callback);
3637
return callback;
3738
}
3839

3940
@Bean
40-
BeforeSendSession springVersionSessionCallback() {
41-
BeforeSendSession beforeSendSession = new BeforeSendSession() {
41+
OnSession springVersionSessionCallback() {
42+
OnSession onSession = new OnSession() {
4243
@Override
43-
public void beforeSendSession(SessionPayload payload) {
44+
public boolean onSession(SessionPayload payload) {
4445
addSpringRuntimeVersion(payload.getDevice());
46+
return true;
4547
}
4648
};
47-
bugsnag.addBeforeSendSession(beforeSendSession);
48-
return beforeSendSession;
49+
bugsnag.addOnSession(onSession);
50+
return onSession;
4951
}
5052

5153
private void addSpringRuntimeVersion(Map<String, Object> device) {

bugsnag-spring/src/main/java/com/bugsnag/ExceptionClassCallback.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,15 @@ class ExceptionClassCallback implements Callback {
111111
}
112112

113113
@Override
114-
public void beforeNotify(Report report) {
114+
public boolean onError(Report report) {
115115

116116
HandledState handledState = report.getHandledState();
117117

118118
// A manually-set severity takes precedence
119119
SeverityReasonType severityReasonType = handledState.calculateSeverityReasonType();
120120
if (severityReasonType == SeverityReasonType.REASON_USER_SPECIFIED
121121
|| severityReasonType == SeverityReasonType.REASON_CALLBACK_SPECIFIED) {
122-
return;
122+
return true; // do not change delivery decision
123123
}
124124

125125
Class exceptionClass = report.getException().getClass();
@@ -133,5 +133,6 @@ public void beforeNotify(Report report) {
133133
severity,
134134
handledState.isUnhandled()));
135135
}
136+
return true;
136137
}
137138
}

bugsnag-spring/src/main/java/com/bugsnag/SpringBootConfiguration.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,26 @@ public class SpringBootConfiguration {
1919
Callback springBootVersionErrorCallback() {
2020
Callback callback = new Callback() {
2121
@Override
22-
public void beforeNotify(Report report) {
22+
public boolean onError(Report report) {
2323
addSpringRuntimeVersion(report.getDevice());
24+
return true;
2425
}
2526
};
2627
bugsnag.addCallback(callback);
2728
return callback;
2829
}
2930

3031
@Bean
31-
BeforeSendSession springBootVersionSessionCallback() {
32-
BeforeSendSession beforeSendSession = new BeforeSendSession() {
32+
OnSession springBootVersionSessionCallback() {
33+
OnSession onSession = new OnSession() {
3334
@Override
34-
public void beforeSendSession(SessionPayload payload) {
35+
public boolean onSession(SessionPayload payload) {
3536
addSpringRuntimeVersion(payload.getDevice());
37+
return true;
3638
}
3739
};
38-
bugsnag.addBeforeSendSession(beforeSendSession);
39-
return beforeSendSession;
40+
bugsnag.addOnSession(onSession);
41+
return onSession;
4042
}
4143

4244
private void addSpringRuntimeVersion(Map<String, Object> device) {

bugsnag-spring/src/test/java/com/bugsnag/SpringMvcTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,9 @@ public void unhandledTypeMismatchExceptionCallbackSeverity()
190190
Report report;
191191
Callback callback = new Callback() {
192192
@Override
193-
public void beforeNotify(Report report) {
193+
public boolean onError(Report report) {
194194
report.setSeverity(Severity.WARNING);
195+
return true;
195196
}
196197
};
197198

bugsnag-spring/src/test/java/com/bugsnag/testapp/springboot/TestController.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,9 @@ public void handledTypeMismatchExceptionCallbackSeverity() {
6767
} catch (TypeMismatchException ex) {
6868
bugsnag.notify(ex, new Callback() {
6969
@Override
70-
public void beforeNotify(Report report) {
70+
public boolean onError(Report report) {
7171
report.setSeverity(Severity.WARNING);
72+
return true;
7273
}
7374
});
7475
}

bugsnag/src/main/java/com/bugsnag/BeforeSendSession.java

Lines changed: 0 additions & 5 deletions
This file was deleted.

bugsnag/src/main/java/com/bugsnag/Bugsnag.java

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -452,16 +452,12 @@ public boolean notify(Report report, Callback reportCallback) {
452452
return false;
453453
}
454454

455-
// Run all client-wide beforeNotify callbacks
455+
// Run all client-wide onError callbacks
456456
for (Callback callback : config.callbacks) {
457457
try {
458-
// Run the callback
459-
callback.beforeNotify(report);
460-
461-
// Check if callback cancelled delivery
462-
if (report.getShouldCancel()) {
463-
LOGGER.debug("Error not reported to Bugsnag - "
464-
+ "cancelled by a client-wide beforeNotify callback");
458+
boolean proceed = callback.onError(report);
459+
if (!proceed || report.getShouldCancel()) {
460+
LOGGER.debug("Error not reported to Bugsnag - cancelled by a client-wide onError callback");
465461
return false;
466462
}
467463
} catch (Throwable ex) {
@@ -472,16 +468,12 @@ public boolean notify(Report report, Callback reportCallback) {
472468
// Add thread metadata to the report
473469
report.mergeMetadata(THREAD_METADATA.get());
474470

475-
// Run the report-specific beforeNotify callback, if given
471+
// Run the report-specific onError callback, if given
476472
if (reportCallback != null) {
477473
try {
478-
// Run the callback
479-
reportCallback.beforeNotify(report);
480-
481-
// Check if callback cancelled delivery
482-
if (report.getShouldCancel()) {
483-
LOGGER.debug(
484-
"Error not reported to Bugsnag - cancelled by a report-specific callback");
474+
boolean proceed = reportCallback.onError(report);
475+
if (!proceed || report.getShouldCancel()) {
476+
LOGGER.debug("Error not reported to Bugsnag - cancelled by a report-specific callback");
485477
return false;
486478
}
487479
} catch (Throwable ex) {
@@ -676,7 +668,7 @@ public static Set<Bugsnag> uncaughtExceptionClients() {
676668
return Collections.emptySet();
677669
}
678670

679-
void addBeforeSendSession(BeforeSendSession beforeSendSession) {
680-
sessionTracker.addBeforeSendSession(beforeSendSession);
671+
void addOnSession(OnSession onSession) {
672+
sessionTracker.addOnSession(onSession);
681673
}
682674
}

bugsnag/src/main/java/com/bugsnag/BugsnagAppender.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ protected void append(final ILoggingEvent event) {
134134
calculateSeverity(event),
135135
new Callback() {
136136
@Override
137-
public void beforeNotify(Report report) {
137+
public boolean onError(Report report) {
138138

139139
// Add some data from the logging event
140140
report.addToTab("Log event data",
@@ -146,8 +146,12 @@ public void beforeNotify(Report report) {
146146
populateContextData(report, event);
147147

148148
if (reportCallback != null) {
149-
reportCallback.beforeNotify(report);
149+
boolean proceed = reportCallback.onError(report);
150+
if (!proceed) {
151+
return false; // suppress delivery
152+
}
150153
}
154+
return true;
151155
}
152156
});
153157
}
@@ -270,7 +274,7 @@ private Bugsnag createBugsnag() {
270274
// Add a callback to put global metadata on every report
271275
bugsnag.addCallback(new Callback() {
272276
@Override
273-
public void beforeNotify(Report report) {
277+
public boolean onError(Report report) {
274278

275279
for (LogbackMetadata metadata : globalMetadata) {
276280
for (LogbackMetadataTab tab : metadata.getTabs()) {
@@ -282,6 +286,7 @@ public void beforeNotify(Report report) {
282286
}
283287

284288
}
289+
return true;
285290
}
286291
});
287292

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.bugsnag;
2+
3+
@FunctionalInterface
4+
interface OnSession {
5+
boolean onSession(SessionPayload payload);
6+
}

bugsnag/src/main/java/com/bugsnag/SessionPayload.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,21 @@ final class SessionPayload {
99

1010
private final Collection<SessionCount> sessionCounts;
1111
private final Diagnostics diagnostics;
12+
private final Map<String, Object> device;
13+
private final Map<String, Object> app;
1214

1315
SessionPayload(Collection<SessionCount> sessionCounts, Configuration configuration) {
1416
this.sessionCounts = sessionCounts;
1517
diagnostics = new Diagnostics(configuration);
18+
this.device = null;
19+
this.app = null;
20+
}
21+
22+
SessionPayload(Collection<SessionCount> sessionCounts, Map<String, Object> device, Map<String, Object> app) {
23+
this.sessionCounts = sessionCounts;
24+
this.diagnostics = null;
25+
this.device = device;
26+
this.app = app;
1627
}
1728

1829
@Expose
@@ -22,12 +33,12 @@ Notifier getNotifier() {
2233

2334
@Expose
2435
Map<String, Object> getDevice() {
25-
return diagnostics.device;
36+
return device != null ? device : diagnostics.device;
2637
}
2738

2839
@Expose
2940
Map<String, Object> getApp() {
30-
return diagnostics.app;
41+
return app != null ? app : diagnostics.app;
3142
}
3243

3344
@Expose

0 commit comments

Comments
 (0)