diff --git a/src/main/java/io/stargate/sgv2/jsonapi/exception/ErrorCode.java b/src/main/java/io/stargate/sgv2/jsonapi/exception/ErrorCode.java index b70703b9e0..9222dc239e 100644 --- a/src/main/java/io/stargate/sgv2/jsonapi/exception/ErrorCode.java +++ b/src/main/java/io/stargate/sgv2/jsonapi/exception/ErrorCode.java @@ -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; @@ -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); @@ -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; + } + } }