Skip to content

Commit

Permalink
Replace RuntimeException with FlagsmithRuntimeError (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewelwell authored Jul 14, 2023
1 parent b59359c commit fc0ec75
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
7 changes: 4 additions & 3 deletions src/main/java/com/flagsmith/FlagsmithApiWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.flagsmith.config.FlagsmithConfig;
import com.flagsmith.exceptions.FlagsmithRuntimeError;
import com.flagsmith.flagengine.environments.EnvironmentModel;
import com.flagsmith.flagengine.features.FeatureStateModel;
import com.flagsmith.flagengine.identities.traits.TraitModel;
Expand Down Expand Up @@ -159,7 +160,7 @@ public Flags getFeatureFlags(boolean doThrow) {
} catch (ExecutionException ee) {
logger.error("Execution failed on fetching Feature flags.", ee);
if (doThrow) {
throw new RuntimeException(ee);
throw new FlagsmithRuntimeError(ee);
}
}

Expand Down Expand Up @@ -232,7 +233,7 @@ public Flags identifyUserWithTraits(
} catch (ExecutionException ee) {
logger.error("Execution failed on fetching Feature flags.", ee);
if (doThrow) {
throw new RuntimeException(ee);
throw new FlagsmithRuntimeError(ee);
}
}

Expand All @@ -258,7 +259,7 @@ public EnvironmentModel getEnvironment() {
logger.error("Environment loading interrupted.", ie);
} catch (ExecutionException ee) {
logger.error("Execution failed on Environment loading.", ee);
throw new RuntimeException(ee);
throw new FlagsmithRuntimeError(ee);
}

return null;
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/flagsmith/FlagsmithClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.flagsmith.config.FlagsmithConfig;
import com.flagsmith.exceptions.FlagsmithApiError;
import com.flagsmith.exceptions.FlagsmithClientError;
import com.flagsmith.exceptions.FlagsmithRuntimeError;
import com.flagsmith.flagengine.Engine;
import com.flagsmith.flagengine.environments.EnvironmentModel;
import com.flagsmith.flagengine.features.FeatureStateModel;
Expand Down Expand Up @@ -478,7 +479,7 @@ public FlagsmithClient build() {

if (configuration.getEnableLocalEvaluation()) {
if (!apiKey.startsWith("ser.")) {
throw new RuntimeException(
throw new FlagsmithRuntimeError(
"In order to use local evaluation, please generate a server key "
+ "in the environment settings page.");
}
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/com/flagsmith/exceptions/FlagsmithRuntimeError.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.flagsmith.exceptions;

/*
* Custom RuntimeException for use in the Flagsmith client code.
*
* TODO: this only extends RuntimeException to maintain backwards compatibility
* for any implementations that previously caught RuntimeException. We should
* consider changing this in a future major release since it's unnecessary.
*/
public class FlagsmithRuntimeError extends RuntimeException {

public FlagsmithRuntimeError() {
super();
}

public FlagsmithRuntimeError(String message) {
super(message);
}

public FlagsmithRuntimeError(Throwable cause) {
super(cause);
}
}
5 changes: 3 additions & 2 deletions src/main/java/com/flagsmith/threads/RequestProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
import com.flagsmith.MapperFactory;
import com.flagsmith.config.Retry;
import com.flagsmith.exceptions.FlagsmithApiError;
import com.flagsmith.exceptions.FlagsmithRuntimeError;

import java.io.IOException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import lombok.Data;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
Expand Down Expand Up @@ -104,7 +105,7 @@ public <T> Future<T> executeAsync(
localRetry.retryAttempted();
} while (localRetry.isRetry(statusCode));
} catch (Exception e) {
throw new RuntimeException();
throw new FlagsmithRuntimeError();
} finally {
if (!completableFuture.isDone()) {
if (doThrow) {
Expand Down

0 comments on commit fc0ec75

Please sign in to comment.