Skip to content

Commit

Permalink
Fix problem with ErrorCode initialization wrt config access (#1380)
Browse files Browse the repository at this point in the history
  • Loading branch information
tatu-at-datastax authored and amorton committed Aug 30, 2024
1 parent 31be9ae commit 096080a
Showing 1 changed file with 30 additions and 12 deletions.
42 changes: 30 additions & 12 deletions src/main/java/io/stargate/sgv2/jsonapi/exception/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -207,17 +207,6 @@ public enum ErrorCode {
TABLE_COLUMN_UNKNOWN("Column unknown");

private final String message;
private final boolean extendError =
ApiConstants.isOffline()
? new SmallRyeConfigBuilder()
.withMapping(OperationsConfig.class)
.build()
.getConfigMapping(OperationsConfig.class)
.extendError()
: ConfigProvider.getConfig()
.unwrap(SmallRyeConfig.class)
.getConfigMapping(OperationsConfig.class)
.extendError();

ErrorCode(String message) {
this.message = message;
Expand All @@ -241,7 +230,7 @@ public JsonApiException toApiException(Throwable cause, String format, Object...
}

private String getErrorMessage(String format, Object... args) {
if (extendError) {
if (ExtendError.enabled()) {
return String.format(format, args);
}
return message + ": " + String.format(format, args);
Expand All @@ -254,4 +243,33 @@ public JsonApiException toApiException() {
public JsonApiException toApiException(Response.Status httpStatus) {
return new JsonApiException(this, message, null, httpStatus);
}

/**
* Helper class to cache loading of settings from the configuration. This is used to avoid having
* to access Config during Enum class initialization. It will also prevent repeated configuration
* loading calls.
*/
static class ExtendError {
private static final ExtendError instance = new ExtendError();

private final boolean enabled;

public ExtendError() {
enabled =
ApiConstants.isOffline()
? new SmallRyeConfigBuilder()
.withMapping(OperationsConfig.class)
.build()
.getConfigMapping(OperationsConfig.class)
.extendError()
: ConfigProvider.getConfig()
.unwrap(SmallRyeConfig.class)
.getConfigMapping(OperationsConfig.class)
.extendError();
}

public static boolean enabled() {
return instance.enabled;
}
}
}

0 comments on commit 096080a

Please sign in to comment.