Skip to content

Commit 4a61e34

Browse files
committed
refactor(OnErrorCallback): renamed Callback -> OnErrorCallback & removed BugsnagEvent.cancel()
1 parent fa5400c commit 4a61e34

26 files changed

Lines changed: 81 additions & 106 deletions

File tree

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.bugsnag;
22

3-
import com.bugsnag.callbacks.Callback;
3+
import com.bugsnag.callbacks.OnErrorCallback;
44

55
import org.springframework.beans.factory.InitializingBean;
66
import org.springframework.beans.factory.annotation.Autowired;
@@ -25,15 +25,15 @@ public class BugsnagSpringConfiguration implements InitializingBean {
2525
* Add a callback to add the version of Spring used by the application
2626
*/
2727
@Bean
28-
Callback springVersionErrorCallback() {
29-
Callback callback = new Callback() {
28+
OnErrorCallback springVersionErrorCallback() {
29+
OnErrorCallback callback = new OnErrorCallback() {
3030
@Override
3131
public boolean onError(BugsnagEvent report) {
3232
addSpringRuntimeVersion(report.getDevice());
3333
return true;
3434
}
3535
};
36-
bugsnag.addCallback(callback);
36+
bugsnag.addOnError(callback);
3737
return callback;
3838
}
3939

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.bugsnag;
22

33
import com.bugsnag.HandledState.SeverityReasonType;
4-
import com.bugsnag.callbacks.Callback;
4+
import com.bugsnag.callbacks.OnErrorCallback;
55

66
import org.springframework.beans.ConversionNotSupportedException;
77
import org.springframework.beans.TypeMismatchException;
@@ -35,7 +35,7 @@
3535
* Exceptions that automatically resolve to 500 (internal server error) responses
3636
* map to severity to ERROR.
3737
*/
38-
class ExceptionClassCallback implements Callback {
38+
class ExceptionClassCallback implements OnErrorCallback {
3939

4040
private static final Map<Class<? extends java.lang.Exception>, Severity> EXCEPTION_TO_SEVERITY;
4141

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ BugsnagJakartaMvcExceptionHandler bugsnagHandlerExceptionResolver() {
3030
*/
3131
@Override
3232
public void afterPropertiesSet() {
33-
bugsnag.addCallback(new ExceptionClassCallback());
33+
bugsnag.addOnError(new ExceptionClassCallback());
3434
}
3535
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.bugsnag;
22

3-
import com.bugsnag.callbacks.Callback;
3+
import com.bugsnag.callbacks.OnErrorCallback;
44

55
import org.springframework.beans.factory.annotation.Autowired;
66
import org.springframework.boot.SpringBootVersion;
@@ -16,15 +16,15 @@ public class SpringBootConfiguration {
1616
* Add a callback to add the version of Spring Boot used by the application.
1717
*/
1818
@Bean
19-
Callback springBootVersionErrorCallback() {
20-
Callback callback = new Callback() {
19+
OnErrorCallback springBootVersionErrorCallback() {
20+
OnErrorCallback callback = new OnErrorCallback() {
2121
@Override
2222
public boolean onError(BugsnagEvent report) {
2323
addSpringRuntimeVersion(report.getDevice());
2424
return true;
2525
}
2626
};
27-
bugsnag.addCallback(callback);
27+
bugsnag.addOnError(callback);
2828
return callback;
2929
}
3030

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import com.bugsnag.HandledState.SeverityReasonType;
1616

17-
import com.bugsnag.callbacks.Callback;
17+
import com.bugsnag.callbacks.OnErrorCallback;
1818
import com.bugsnag.delivery.Delivery;
1919

2020
import com.bugsnag.serialization.Serializer;
@@ -188,7 +188,7 @@ public void unhandledTypeMismatchExceptionSeverityInfo() {
188188
public void unhandledTypeMismatchExceptionCallbackSeverity()
189189
throws IllegalAccessException, NoSuchFieldException {
190190
BugsnagEvent event;
191-
Callback callback = new Callback() {
191+
OnErrorCallback callback = new OnErrorCallback() {
192192
@Override
193193
public boolean onError(BugsnagEvent report) {
194194
report.setSeverity(Severity.WARNING);
@@ -197,16 +197,16 @@ public boolean onError(BugsnagEvent report) {
197197
};
198198

199199
try {
200-
bugsnag.addCallback(callback);
200+
bugsnag.addOnError(callback);
201201

202202
callUnhandledTypeMismatchExceptionEndpoint();
203203

204204
event = verifyAndGetReport(delivery);
205205
} finally {
206206
// Remove the callback via reflection so that subsequent tests do not use it
207207
Field callbacksField = Configuration.class.getDeclaredField("callbacks");
208-
@SuppressWarnings(value = "unchecked") Collection<Callback> callbacks =
209-
(Collection<Callback>) callbacksField.get(bugsnag.getConfig());
208+
@SuppressWarnings(value = "unchecked") Collection<OnErrorCallback> callbacks =
209+
(Collection<OnErrorCallback>) callbacksField.get(bugsnag.getConfig());
210210
callbacks.remove(callback);
211211
}
212212

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import com.bugsnag.BugsnagEvent;
66
import com.bugsnag.Severity;
7-
import com.bugsnag.callbacks.Callback;
7+
import com.bugsnag.callbacks.OnErrorCallback;
88

99
import org.springframework.beans.TypeMismatchException;
1010
import org.springframework.beans.factory.annotation.Autowired;
@@ -65,7 +65,7 @@ public void handledTypeMismatchExceptionCallbackSeverity() {
6565
try {
6666
throw new TypeMismatchException("Test", String.class);
6767
} catch (TypeMismatchException ex) {
68-
bugsnag.notify(ex, new Callback() {
68+
bugsnag.notify(ex, new OnErrorCallback() {
6969
@Override
7070
public boolean onError(BugsnagEvent event) {
7171
event.setSeverity(Severity.WARNING);

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.bugsnag;
22

3-
import com.bugsnag.callbacks.Callback;
3+
import com.bugsnag.callbacks.OnErrorCallback;
44
import com.bugsnag.delivery.Delivery;
55
import com.bugsnag.delivery.HttpDelivery;
66
import com.bugsnag.util.DaemonThreadFactory;
@@ -142,10 +142,10 @@ public void run() {
142142
* sent to Bugsnag completely.
143143
*
144144
* @param callback a callback to run before sending errors to Bugsnag
145-
* @see Callback
145+
* @see OnErrorCallback
146146
*/
147-
public void addCallback(Callback callback) {
148-
config.addCallback(callback);
147+
public void addOnError(OnErrorCallback callback) {
148+
config.addOnError(callback);
149149
}
150150

151151
/**
@@ -370,10 +370,10 @@ public boolean notify(Throwable throwable) {
370370
* Notify Bugsnag of a handled exception.
371371
*
372372
* @param throwable the exception to send to Bugsnag
373-
* @param callback the {@link Callback} object to run for this Report
373+
* @param callback the {@link OnErrorCallback} object to run for this Report
374374
* @return true unless the error report was ignored
375375
*/
376-
public boolean notify(Throwable throwable, Callback callback) {
376+
public boolean notify(Throwable throwable, OnErrorCallback callback) {
377377
return notify(buildReport(throwable), callback);
378378
}
379379

@@ -395,10 +395,10 @@ public boolean notify(Throwable throwable, Severity severity) {
395395
* @param throwable the exception to send to Bugsnag
396396
* @param severity the severity of the error, one of {#link Severity#ERROR},
397397
* {@link Severity#WARNING} or {@link Severity#INFO}
398-
* @param callback the {@link Callback} object to run for this Report
398+
* @param callback the {@link OnErrorCallback} object to run for this Report
399399
* @return true unless the error report was ignored
400400
*/
401-
public boolean notify(Throwable throwable, Severity severity, Callback callback) {
401+
public boolean notify(Throwable throwable, Severity severity, OnErrorCallback callback) {
402402
if (throwable == null) {
403403
LOGGER.warn("Tried to notify with a null Throwable");
404404
return false;
@@ -436,12 +436,12 @@ boolean notify(Throwable throwable, HandledState handledState, java.lang.Thread
436436
* for this particular error report.
437437
*
438438
* @param event the {@link BugsnagEvent} object to send to Bugsnag
439-
* @param reportCallback the {@link Callback} object to run for this Report
439+
* @param reportCallback the {@link OnErrorCallback} object to run for this Report
440440
* @return false if the error report was ignored
441441
* @see BugsnagEvent
442442
* @see #buildReport
443443
*/
444-
public boolean notify(BugsnagEvent event, Callback reportCallback) {
444+
public boolean notify(BugsnagEvent event, OnErrorCallback reportCallback) {
445445
if (event == null) {
446446
LOGGER.warn("Tried to call notify with a null Report");
447447
return false;
@@ -462,10 +462,10 @@ public boolean notify(BugsnagEvent event, Callback reportCallback) {
462462
}
463463

464464
// Run all client-wide onError callbacks
465-
for (Callback callback : config.callbacks) {
465+
for (OnErrorCallback callback : config.callbacks) {
466466
try {
467467
boolean proceed = callback.onError(event);
468-
if (!proceed || event.getShouldCancel()) {
468+
if (!proceed) {
469469
LOGGER.debug("Error not reported to Bugsnag - cancelled by a client-wide onError callback");
470470
return false;
471471
}
@@ -481,7 +481,7 @@ public boolean notify(BugsnagEvent event, Callback reportCallback) {
481481
if (reportCallback != null) {
482482
try {
483483
boolean proceed = reportCallback.onError(event);
484-
if (!proceed || event.getShouldCancel()) {
484+
if (!proceed) {
485485
LOGGER.debug("Error not reported to Bugsnag - cancelled by a report-specific callback");
486486
return false;
487487
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.bugsnag;
22

3-
import com.bugsnag.callbacks.Callback;
3+
import com.bugsnag.callbacks.OnErrorCallback;
44
import com.bugsnag.delivery.Delivery;
55
import com.bugsnag.logback.BugsnagMarker;
66
import com.bugsnag.logback.LogbackFeatureFlag;
@@ -120,7 +120,7 @@ protected void append(final ILoggingEvent loggingEvent) {
120120
if (bugsnag != null) {
121121
Throwable throwable = extractThrowable(loggingEvent);
122122

123-
final Callback reportCallback;
123+
final OnErrorCallback reportCallback;
124124
Marker marker = loggingEvent.getMarker();
125125
if (marker instanceof BugsnagMarker) {
126126
reportCallback = ((BugsnagMarker) marker).getCallback();
@@ -136,7 +136,7 @@ protected void append(final ILoggingEvent loggingEvent) {
136136
bugsnag.notify(
137137
throwable,
138138
calculateSeverity(loggingEvent),
139-
new Callback() {
139+
new OnErrorCallback() {
140140
@Override
141141
public boolean onError(BugsnagEvent event) {
142142

@@ -281,7 +281,7 @@ private Bugsnag createBugsnag() {
281281
}
282282

283283
// Add a callback to put global metadata on every report
284-
bugsnag.addCallback(new Callback() {
284+
bugsnag.addOnError(new OnErrorCallback() {
285285
@Override
286286
public boolean onError(BugsnagEvent event) {
287287

@@ -325,11 +325,11 @@ private Pattern[] compileDiscardPatterns(Collection<String> patternStrings) {
325325
* sent to Bugsnag completely.
326326
*
327327
* @param callback a callback to run before sending errors to Bugsnag
328-
* @see Callback
328+
* @see OnErrorCallback
329329
*/
330-
public void addCallback(Callback callback) {
330+
public void addCallback(OnErrorCallback callback) {
331331
if (bugsnag != null) {
332-
bugsnag.addCallback(callback);
332+
bugsnag.addOnError(callback);
333333
}
334334
}
335335

bugsnag/src/main/java/com/bugsnag/BugsnagEvent.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ public class BugsnagEvent {
2323
private Severity severity;
2424
private String groupingHash;
2525
private Diagnostics diagnostics;
26-
private boolean shouldCancel = false;
2726
private Map<String, Object> sessionMap;
2827
private final List<BugsnagThread> threads;
2928
private final FeatureFlagStore featureFlagStore;
@@ -334,15 +333,6 @@ public BugsnagEvent setUserName(String name) {
334333
return this;
335334
}
336335

337-
public BugsnagEvent cancel() {
338-
this.shouldCancel = true;
339-
return this;
340-
}
341-
342-
public boolean getShouldCancel() {
343-
return this.shouldCancel;
344-
}
345-
346336
HandledState getHandledState() {
347337
return handledState;
348338
}

bugsnag/src/main/java/com/bugsnag/Configuration.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.bugsnag;
22

33
import com.bugsnag.callbacks.AppCallback;
4-
import com.bugsnag.callbacks.Callback;
4+
import com.bugsnag.callbacks.OnErrorCallback;
55
import com.bugsnag.callbacks.DeviceCallback;
66
import com.bugsnag.callbacks.JakartaServletCallback;
77
import com.bugsnag.delivery.AsyncHttpDelivery;
@@ -19,7 +19,7 @@
1919
import java.util.HashSet;
2020
import java.util.Map;
2121
import java.util.Set;
22-
import java.util.concurrent.ConcurrentLinkedQueue;
22+
import java.util.concurrent.CopyOnWriteArraySet;
2323
import java.util.concurrent.atomic.AtomicBoolean;
2424
import java.util.regex.Pattern;
2525

@@ -37,7 +37,7 @@ public class Configuration {
3737
private Delivery delivery;
3838
private EndpointConfiguration endpoints;
3939
private Delivery sessionDelivery;
40-
private String[] redactedKeys = new String[] {"password", "secret", "Authorization", "Cookie"};
40+
private String[] redactedKeys = new String[]{"password", "secret", "Authorization", "Cookie"};
4141
private Set<Pattern> discardClassRegexPatterns = new HashSet<Pattern>();
4242
private Set<String> discardClassStringPatterns = new HashSet<String>();
4343
private Set<String> enabledReleaseStages = null;
@@ -46,16 +46,16 @@ public class Configuration {
4646
private ThreadSendPolicy sendThreads = ThreadSendPolicy.NEVER;
4747
private Serializer serializer = new DefaultSerializer();
4848

49-
Collection<Callback> callbacks = new ConcurrentLinkedQueue<Callback>();
49+
Set<OnErrorCallback> callbacks = new CopyOnWriteArraySet<>();
5050
private final AtomicBoolean autoCaptureSessions = new AtomicBoolean(true);
5151
private final AtomicBoolean sendUncaughtExceptions = new AtomicBoolean(true);
5252
private final FeatureFlagStore featureFlagStore = new FeatureFlagStore();
5353

5454
Configuration(String apiKey) {
5555
this.apiKey = apiKey;
5656
// Add built-in callbacks
57-
addCallback(new AppCallback(this));
58-
addCallback(new DeviceCallback());
57+
addOnError(new AppCallback(this));
58+
addOnError(new DeviceCallback());
5959
DeviceCallback.initializeCache();
6060

6161
endpoints = EndpointConfiguration.fromApiKey(apiKey);
@@ -64,7 +64,7 @@ public class Configuration {
6464
this.sessionDelivery = new AsyncHttpDelivery(endpoints.getSessionEndpoint());
6565

6666
if (JakartaServletCallback.isAvailable()) {
67-
addCallback(new JakartaServletCallback());
67+
addOnError(new JakartaServletCallback());
6868
}
6969
}
7070

@@ -88,10 +88,8 @@ boolean shouldIgnoreClass(String className) {
8888
return false;
8989
}
9090

91-
void addCallback(Callback callback) {
92-
if (!callbacks.contains(callback)) {
93-
callbacks.add(callback);
94-
}
91+
void addOnError(OnErrorCallback callback) {
92+
callbacks.add(callback);
9593
}
9694

9795
boolean inProject(String className) {
@@ -348,7 +346,7 @@ public void setSerializer(Serializer serializer) {
348346
* Add a feature flag with the specified name and variant.
349347
* If the name already exists, the variant will be updated.
350348
*
351-
* @param name the feature flag name
349+
* @param name the feature flag name
352350
* @param variant the feature flag variant (can be null)
353351
*/
354352
public void addFeatureFlag(String name, String variant) {

0 commit comments

Comments
 (0)