Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix problem with ErrorCode initialization wrt config access #1380

Merged
merged 1 commit into from
Aug 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 =
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Access from constructor of EVERY enum constant is pretty wasteful to begin with, but more importantly caused odd initialization failure from any Integration Test that tried to refer to ErrorCode enums.
Hence moved to be done lazily when needed, not eagerly before everything else.

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()) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First access to getErrorMessage() of any ErrorCode instance will load ExtendError helper class, which will get configuration settings. This is properly synchronized as guaranteed by JVM classloading, and all further access is via properly initialized singleton.

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();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of private static instance and accessor, could have exposed this as public. But looks to me slightly cleaner from caller this way.


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;
}
}
}