Skip to content
56 changes: 19 additions & 37 deletions bugsnag/src/main/java/com/bugsnag/Bugsnag.java
Original file line number Diff line number Diff line change
Expand Up @@ -318,43 +318,10 @@ public void setSendThreads(ThreadSendPolicy sendThreads) {
config.setSendThreads(sendThreads);
}

/**
* Set a timeout (in ms) to use when delivering Bugsnag error reports and
* sessions.
* This is a convenient shorthand for bugsnag.getDelivery().setTimeout();
*
* @param timeout the timeout to set (in ms)
* @see #setDelivery
*/
public void setTimeout(int timeout) {
Delivery delivery = config.getDelivery();
if (delivery instanceof HttpDelivery) {
((HttpDelivery) delivery).setTimeout(timeout);
}

Delivery sessionDelivery = config.getSessionDelivery();
if (sessionDelivery instanceof HttpDelivery) {
((HttpDelivery) sessionDelivery).setTimeout(timeout);
}
}

//
// Notification
//

/**
* Build an Report object to send to Bugsnag.
*
* @param throwable the exception to send to Bugsnag
* @return the report object
* @see BugsnagEvent
* @see #notify(BugsnagEvent)
*/
public BugsnagEvent buildReport(Throwable throwable) {
HandledState handledState = HandledState.newInstance(
HandledState.SeverityReasonType.REASON_HANDLED_EXCEPTION);
return new BugsnagEvent(config, throwable, handledState, Thread.currentThread(), featureFlagStore);
}

/**
* Notify Bugsnag of a handled exception.
Expand All @@ -363,7 +330,11 @@ public BugsnagEvent buildReport(Throwable throwable) {
* @return true unless the error report was ignored
*/
public boolean notify(Throwable throwable) {
return notify(buildReport(throwable));
HandledState handledState = HandledState.newInstance(
HandledState.SeverityReasonType.REASON_HANDLED_EXCEPTION);
BugsnagEvent event = new BugsnagEvent(config, throwable, handledState,
Thread.currentThread(), featureFlagStore);
return notify(event);
}

/**
Expand All @@ -374,7 +345,11 @@ public boolean notify(Throwable throwable) {
* @return true unless the error report was ignored
*/
public boolean notify(Throwable throwable, OnErrorCallback callback) {
return notify(buildReport(throwable), callback);
HandledState handledState = HandledState.newInstance(
HandledState.SeverityReasonType.REASON_HANDLED_EXCEPTION);
BugsnagEvent event = new BugsnagEvent(config, throwable, handledState,
Thread.currentThread(), featureFlagStore);
return notify(event, callback);
}

/**
Expand Down Expand Up @@ -429,7 +404,6 @@ public boolean notify(Throwable throwable, Severity severity, OnErrorCallback ca
* @param event the {@link BugsnagEvent} object to send to Bugsnag
* @return true unless the error report was ignored
* @see BugsnagEvent
* @see #buildReport
*/
public boolean notify(BugsnagEvent event) {
return notify(event, null);
Expand All @@ -448,7 +422,6 @@ boolean notify(Throwable throwable, HandledState handledState, Thread currentThr
* @param reportCallback the {@link OnErrorCallback} object to run for this Report
* @return false if the error report was ignored
* @see BugsnagEvent
* @see #buildReport
*/
public boolean notify(BugsnagEvent event, OnErrorCallback reportCallback) {
if (event == null) {
Expand Down Expand Up @@ -748,4 +721,13 @@ public void clearFeatureFlags() {
FeatureFlagStore copyFeatureFlagStore() {
return featureFlagStore.copy();
}

/**
* Get a copy of the feature flag store (package-private for testing).
*
* @return a copy of the feature flag store
*/
FeatureFlagStore getFeatureFlagStoreCopy() {
return featureFlagStore.copy();
}
}
15 changes: 0 additions & 15 deletions bugsnag/src/main/java/com/bugsnag/BugsnagAppender.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ public class BugsnagAppender extends UnsynchronizedAppenderBase<ILoggingEvent> {
/** Whether thread state should be sent to Bugsnag. */
private ThreadSendPolicy sendThreads = ThreadSendPolicy.NEVER;

/** Bugsnag API request timeout. */
private int timeout;

/** Application version. */
private String appVersion;
Expand Down Expand Up @@ -258,9 +256,6 @@ private Bugsnag createBugsnag() {
bugsnag.setReleaseStage(releaseStage);
}

if (timeout > 0) {
bugsnag.setTimeout(timeout);
}

if (!redactedKeys.isEmpty()) {
bugsnag.setRedactedKeys(redactedKeys.toArray(new String[0]));
Expand Down Expand Up @@ -537,16 +532,6 @@ public void setSendThreads(ThreadSendPolicy sendThreads) {
}
}

/**
* @see Bugsnag#setTimeout(int)
*/
public void setTimeout(int timeout) {
this.timeout = timeout;

if (bugsnag != null) {
bugsnag.setTimeout(timeout);
}
}

/**
* @see Bugsnag#setAppVersion(String)
Expand Down
10 changes: 5 additions & 5 deletions bugsnag/src/main/java/com/bugsnag/EndpointConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ public class EndpointConfiguration {

private static final String DEFAULT_NOTIFY_ENDPOINT = "https://notify.bugsnag.com";
private static final String DEFAULT_SESSION_ENDPOINT = "https://sessions.bugsnag.com";
private static final String HUB_NOTIFY_ENDPOINT = "https://notify.insighthub.smartbear.com";
private static final String HUB_SESSION_ENDPOINT = "https://sessions.insighthub.smartbear.com";
private static final String HUB_KEY_PREFIX = "00000";
private static final String SECONDARY_NOTIFY_ENDPOINT = "https://notify.bugsnag.smartbear.com";
private static final String SECONDARY_SESSION_ENDPOINT = "https://sessions.bugsnag.smartbear.com";
private static final String SECONDARY_KEY_PREFIX = "00000";

private final String notifyEndpoint;
private final String sessionEndpoint;
Expand All @@ -30,8 +30,8 @@ public EndpointConfiguration(String notify, String sessions) {
* Constructs an EndpointConfiguration with default notify and session endpoints.
*/
public static EndpointConfiguration fromApiKey(String apiKey) {
if (apiKey != null && apiKey.startsWith(HUB_KEY_PREFIX)) {
return new EndpointConfiguration(HUB_NOTIFY_ENDPOINT, HUB_SESSION_ENDPOINT);
if (apiKey != null && apiKey.startsWith(SECONDARY_KEY_PREFIX)) {
return new EndpointConfiguration(SECONDARY_NOTIFY_ENDPOINT, SECONDARY_SESSION_ENDPOINT);
} else {
return new EndpointConfiguration(DEFAULT_NOTIFY_ENDPOINT, DEFAULT_SESSION_ENDPOINT);
}
Expand Down
23 changes: 15 additions & 8 deletions bugsnag/src/test/java/com/bugsnag/BugsnagFeatureFlagTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,18 @@ public void tearDown() {
bugsnag.close();
}

private BugsnagEvent createEvent(Bugsnag client, Exception exception) {
HandledState handledState = HandledState.newInstance(
HandledState.SeverityReasonType.REASON_HANDLED_EXCEPTION);
return new BugsnagEvent(client.getConfig(), exception, handledState,
Thread.currentThread(), client.getFeatureFlagStoreCopy());
}

@Test
public void testAddFeatureFlag() {
bugsnag.addFeatureFlag("flag1", "variant-a");

BugsnagEvent event = bugsnag.buildReport(new RuntimeException("Test"));
BugsnagEvent event = createEvent(bugsnag, new RuntimeException("Test"));
List<FeatureFlag> flags = event.getFeatureFlags();

assertEquals(1, flags.size());
Expand All @@ -42,7 +49,7 @@ public void testAddFeatureFlag() {
public void testAddFeatureFlagWithoutVariant() {
bugsnag.addFeatureFlag("flag1");

BugsnagEvent event = bugsnag.buildReport(new RuntimeException("Test"));
BugsnagEvent event = createEvent(bugsnag, new RuntimeException("Test"));
List<FeatureFlag> flags = event.getFeatureFlags();

assertEquals(1, flags.size());
Expand All @@ -58,7 +65,7 @@ public void testAddFeatureFlags() {

bugsnag.addFeatureFlags(flagsToAdd);

BugsnagEvent event = bugsnag.buildReport(new RuntimeException("Test"));
BugsnagEvent event = createEvent(bugsnag, new RuntimeException("Test"));
List<FeatureFlag> flags = event.getFeatureFlags();

assertEquals(2, flags.size());
Expand All @@ -72,7 +79,7 @@ public void testClearFeatureFlag() {
bugsnag.addFeatureFlag("flag2", "variant-b");
bugsnag.clearFeatureFlag("flag1");

BugsnagEvent event = bugsnag.buildReport(new RuntimeException("Test"));
BugsnagEvent event = createEvent(bugsnag, new RuntimeException("Test"));
List<FeatureFlag> flags = event.getFeatureFlags();

assertEquals(1, flags.size());
Expand All @@ -85,7 +92,7 @@ public void testClearFeatureFlags() {
bugsnag.addFeatureFlag("flag2", "variant-b");
bugsnag.clearFeatureFlags();

BugsnagEvent event = bugsnag.buildReport(new RuntimeException("Test"));
BugsnagEvent event = createEvent(bugsnag, new RuntimeException("Test"));
List<FeatureFlag> flags = event.getFeatureFlags();

assertEquals(0, flags.size());
Expand All @@ -99,7 +106,7 @@ public void testClientFlagsInheritFromConfiguration() {
Bugsnag client = new Bugsnag("api-key", false);
client.getConfig().addFeatureFlag("config-flag", "config-variant");

BugsnagEvent event = client.buildReport(new RuntimeException("Test"));
BugsnagEvent event = createEvent(client, new RuntimeException("Test"));
List<FeatureFlag> flags = event.getFeatureFlags();

assertEquals(1, flags.size());
Expand All @@ -118,7 +125,7 @@ public void testClientFlagsOverrideConfigurationFlags() {
client.getConfig().addFeatureFlag("flag1", "config-variant");
client.addFeatureFlag("flag1", "client-variant");

BugsnagEvent event = client.buildReport(new RuntimeException("Test"));
BugsnagEvent event = createEvent(client, new RuntimeException("Test"));
List<FeatureFlag> flags = event.getFeatureFlags();

assertEquals(1, flags.size());
Expand All @@ -139,7 +146,7 @@ public void testFeatureFlagOrderPreservedAcrossScopes() {
client.getConfig().addFeatureFlag("flag2", "config-variant");
client.addFeatureFlag("flag3", "client-variant");

BugsnagEvent event = client.buildReport(new RuntimeException("Test"));
BugsnagEvent event = createEvent(client, new RuntimeException("Test"));
List<FeatureFlag> flags = event.getFeatureFlags();

assertEquals(3, flags.size());
Expand Down
16 changes: 8 additions & 8 deletions bugsnag/src/test/java/com/bugsnag/ConfigurationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

public class ConfigurationTest {

private static final String HUB_KEY = "00000aaaaaaaaaaaaaaaaaaaaaaaaaaa";
private static final String SECONDARY_KEY = "00000aaaaaaaaaaaaaaaaaaaaaaaaaaa";
private static final String CLASSIC_KEY = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
private Configuration config;

Expand Down Expand Up @@ -176,14 +176,14 @@ public void testStaticEndpointHelpers() {
assertEquals(defaultSessionEndpoint,
normalConfig.getSessionEndpoint());

String hubNotifyEndpoint = "https://notify.insighthub.smartbear.com";
String hubSessionEndpoint = "https://sessions.insighthub.smartbear.com";
EndpointConfiguration hubConfig = EndpointConfiguration.fromApiKey(HUB_KEY);
String secondaryNotifyEndpoint = "https://notify.bugsnag.smartbear.com";
String secondarySessionEndpoint = "https://sessions.bugsnag.smartbear.com";
EndpointConfiguration secondaryConfig = EndpointConfiguration.fromApiKey(SECONDARY_KEY);

assertEquals(hubNotifyEndpoint,
hubConfig.getNotifyEndpoint());
assertEquals(hubSessionEndpoint,
hubConfig.getSessionEndpoint());
assertEquals(secondaryNotifyEndpoint,
secondaryConfig.getNotifyEndpoint());
assertEquals(secondarySessionEndpoint,
secondaryConfig.getSessionEndpoint());

}

Expand Down
34 changes: 21 additions & 13 deletions bugsnag/src/test/java/com/bugsnag/EventFeatureFlagTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.bugsnag;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import org.junit.After;
import org.junit.Before;
Expand All @@ -26,9 +27,16 @@ public void tearDown() {
bugsnag.close();
}

private BugsnagEvent createEvent(Bugsnag client, Exception exception) {
HandledState handledState = HandledState.newInstance(
HandledState.SeverityReasonType.REASON_HANDLED_EXCEPTION);
return new BugsnagEvent(client.getConfig(), exception, handledState,
Thread.currentThread(), client.getFeatureFlagStoreCopy());
}

@Test
public void testAddFeatureFlagOnReport() {
BugsnagEvent event = bugsnag.buildReport(new RuntimeException("Test"));
BugsnagEvent event = createEvent(bugsnag, new RuntimeException("Test"));
event.addFeatureFlag("report-flag", "report-variant");

List<FeatureFlag> flags = event.getFeatureFlags();
Expand All @@ -40,23 +48,23 @@ public void testAddFeatureFlagOnReport() {

@Test
public void testAddFeatureFlagWithoutVariant() {
BugsnagEvent event = bugsnag.buildReport(new RuntimeException("Test"));
BugsnagEvent event = createEvent(bugsnag, new RuntimeException("Test"));
event.addFeatureFlag("report-flag");

List<FeatureFlag> flags = event.getFeatureFlags();

assertEquals(1, flags.size());
assertEquals("report-flag", flags.get(0).getName());
assertEquals(null, flags.get(0).getVariant());
assertNull(flags.get(0).getVariant());
}

@Test
public void testAddFeatureFlags() {
List<FeatureFlag> flagsToAdd = new ArrayList<FeatureFlag>();
List<FeatureFlag> flagsToAdd = new ArrayList<>();
flagsToAdd.add(FeatureFlag.of("flag1", "variant-a"));
flagsToAdd.add(FeatureFlag.of("flag2", "variant-b"));

BugsnagEvent event = bugsnag.buildReport(new RuntimeException("Test"));
BugsnagEvent event = createEvent(bugsnag, new RuntimeException("Test"));
event.addFeatureFlags(flagsToAdd);

List<FeatureFlag> flags = event.getFeatureFlags();
Expand All @@ -68,7 +76,7 @@ public void testAddFeatureFlags() {

@Test
public void testClearFeatureFlag() {
BugsnagEvent event = bugsnag.buildReport(new RuntimeException("Test"));
BugsnagEvent event = createEvent(bugsnag, new RuntimeException("Test"));
event.addFeatureFlag("flag1", "variant-a");
event.addFeatureFlag("flag2", "variant-b");
event.clearFeatureFlag("flag1");
Expand All @@ -81,7 +89,7 @@ public void testClearFeatureFlag() {

@Test
public void testClearFeatureFlags() {
BugsnagEvent event = bugsnag.buildReport(new RuntimeException("Test"));
BugsnagEvent event = createEvent(bugsnag, new RuntimeException("Test"));
event.addFeatureFlag("flag1", "variant-a");
event.addFeatureFlag("flag2", "variant-b");
event.clearFeatureFlags();
Expand All @@ -95,7 +103,7 @@ public void testClearFeatureFlags() {
public void testReportFlagsInheritFromClient() {
bugsnag.addFeatureFlag("client-flag", "client-variant");

BugsnagEvent event = bugsnag.buildReport(new RuntimeException("Test"));
BugsnagEvent event = createEvent(bugsnag, new RuntimeException("Test"));
List<FeatureFlag> flags = event.getFeatureFlags();

assertEquals(1, flags.size());
Expand All @@ -107,7 +115,7 @@ public void testReportFlagsInheritFromClient() {
public void testReportFlagsOverrideClientFlags() {
bugsnag.addFeatureFlag("flag1", "client-variant");

BugsnagEvent event = bugsnag.buildReport(new RuntimeException("Test"));
BugsnagEvent event = createEvent(bugsnag, new RuntimeException("Test"));
event.addFeatureFlag("flag1", "report-variant");

List<FeatureFlag> flags = event.getFeatureFlags();
Expand All @@ -128,7 +136,7 @@ public void testFeatureFlagOrderAcrossAllScopes() {
bugsnag.addFeatureFlag("flag3", "client-variant");

// Add flags to report (one new, one override)
BugsnagEvent event = bugsnag.buildReport(new RuntimeException("Test"));
BugsnagEvent event = createEvent(bugsnag, new RuntimeException("Test"));
event.addFeatureFlag("flag3", "report-variant");
event.addFeatureFlag("flag4", "report-variant");

Expand All @@ -152,7 +160,7 @@ public void testClearAndReAddChangesPosition() {
bugsnag.getConfig().addFeatureFlag("flag2", "value2");
bugsnag.getConfig().clearFeatureFlag("flag1");

BugsnagEvent event = bugsnag.buildReport(new RuntimeException("Test"));
BugsnagEvent event = createEvent(bugsnag, new RuntimeException("Test"));
event.addFeatureFlag("flag1", "value1-readded");

List<FeatureFlag> flags = event.getFeatureFlags();
Expand All @@ -166,7 +174,7 @@ public void testClearAndReAddChangesPosition() {

@Test
public void testFeatureFlagChaining() {
BugsnagEvent event = bugsnag.buildReport(new RuntimeException("Test"));
BugsnagEvent event = createEvent(bugsnag, new RuntimeException("Test"));

event.addFeatureFlag("flag1", "variant-a")
.addFeatureFlag("flag2", "variant-b")
Expand All @@ -192,7 +200,7 @@ public void testMultipleScopesMaintainInsertionOrder() {

// Report adds flag1 with updated value (overrides config value but keeps position)
// and adds flag2 with updated value
BugsnagEvent event = bugsnag.buildReport(new RuntimeException("Test"));
BugsnagEvent event = createEvent(bugsnag, new RuntimeException("Test"));
event.addFeatureFlag("flag1", "value1-updated");
event.addFeatureFlag("flag2", "value2-updated");

Expand Down
Loading