Skip to content

Commit 3d4b992

Browse files
committed
fix: adding reason, and removing stacktraces from errors
Signed-off-by: Simon Schrottner <[email protected]>
1 parent 7276acb commit 3d4b992

File tree

6 files changed

+34
-14
lines changed

6 files changed

+34
-14
lines changed

src/main/java/dev/openfeature/sdk/OpenFeatureClient.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ private <T> FlagEvaluationDetails<T> evaluateFlag(FlagValueType type, String key
135135
OpenFeatureError error = ExceptionUtils.instantiateErrorByErrorCode(
136136
details.getErrorCode(),
137137
details.getErrorMessage());
138-
details.setValue(defaultValue);
138+
enrichDetailsWithErrorDefaults(defaultValue, details);
139139
hookSupport.errorHooks(type, afterHookContext, error, mergedHooks, hints);
140140
} else {
141141
hookSupport.afterHooks(type, afterHookContext, details, mergedHooks, hints);
@@ -150,8 +150,7 @@ private <T> FlagEvaluationDetails<T> evaluateFlag(FlagValueType type, String key
150150
details.setErrorCode(ErrorCode.GENERAL);
151151
}
152152
details.setErrorMessage(e.getMessage());
153-
details.setValue(defaultValue);
154-
details.setReason(Reason.ERROR.toString());
153+
enrichDetailsWithErrorDefaults(defaultValue, details);
155154
hookSupport.errorHooks(type, afterHookContext, e, mergedHooks, hints);
156155
} finally {
157156
hookSupport.afterAllHooks(type, afterHookContext, mergedHooks, hints);
@@ -160,6 +159,11 @@ private <T> FlagEvaluationDetails<T> evaluateFlag(FlagValueType type, String key
160159
return details;
161160
}
162161

162+
private static <T> void enrichDetailsWithErrorDefaults(T defaultValue, FlagEvaluationDetails<T> details) {
163+
details.setValue(defaultValue);
164+
details.setReason(Reason.ERROR.toString());
165+
}
166+
163167
/**
164168
* Merge invocation contexts with API, transaction and client contexts.
165169
* Does not merge before context.

src/main/java/dev/openfeature/sdk/exceptions/FlagNotFoundError.java

+4-7
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,11 @@
44
import lombok.Getter;
55
import lombok.experimental.StandardException;
66

7-
@SuppressWarnings("checkstyle:MissingJavadocType")
7+
@SuppressWarnings({"checkstyle:MissingJavadocType", "squid:S110"})
88
@StandardException
9-
public class FlagNotFoundError extends OpenFeatureError {
9+
public class FlagNotFoundError extends OpenFeatureErrorWithoutStacktrace {
1010
private static final long serialVersionUID = 1L;
11-
@Getter private final ErrorCode errorCode = ErrorCode.FLAG_NOT_FOUND;
11+
@Getter
12+
private final ErrorCode errorCode = ErrorCode.FLAG_NOT_FOUND;
1213

13-
@Override
14-
public synchronized Throwable fillInStackTrace() {
15-
return this;
16-
}
1714
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package dev.openfeature.sdk.exceptions;
2+
3+
import lombok.experimental.StandardException;
4+
5+
@SuppressWarnings("checkstyle:MissingJavadocType")
6+
@StandardException
7+
public abstract class OpenFeatureErrorWithoutStacktrace extends OpenFeatureError {
8+
private static final long serialVersionUID = 1L;
9+
10+
@Override
11+
public synchronized Throwable fillInStackTrace() {
12+
return this;
13+
}
14+
}

src/main/java/dev/openfeature/sdk/exceptions/ProviderNotReadyError.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
import lombok.Getter;
55
import lombok.experimental.StandardException;
66

7-
@SuppressWarnings("checkstyle:MissingJavadocType")
7+
@SuppressWarnings({"checkstyle:MissingJavadocType", "squid:S110"})
88
@StandardException
9-
public class ProviderNotReadyError extends OpenFeatureError {
9+
public class ProviderNotReadyError extends OpenFeatureErrorWithoutStacktrace {
1010
private static final long serialVersionUID = 1L;
1111
@Getter private final ErrorCode errorCode = ErrorCode.PROVIDER_NOT_READY;
1212
}

src/main/java/dev/openfeature/sdk/exceptions/TypeMismatchError.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
/**
88
* The type of the flag value does not match the expected type.
99
*/
10+
@SuppressWarnings({"checkstyle:MissingJavadocType", "squid:S110"})
1011
@StandardException
11-
public class TypeMismatchError extends OpenFeatureError {
12+
public class TypeMismatchError extends OpenFeatureErrorWithoutStacktrace {
1213
private static final long serialVersionUID = 1L;
1314

1415
@Getter private final ErrorCode errorCode = ErrorCode.TYPE_MISMATCH;

src/test/java/dev/openfeature/sdk/FlagEvaluationSpecTest.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,9 @@ public void initialize(EvaluationContext evaluationContext) throws Exception {
113113
OpenFeatureAPI.getInstance().setProvider(providerName, provider);
114114
assertThat(api.getProvider(providerName).getState()).isEqualTo(ProviderState.NOT_READY);
115115
Client client = OpenFeatureAPI.getInstance().getClient(providerName);
116-
assertEquals(ErrorCode.PROVIDER_NOT_READY, client.getBooleanDetails("return_error_when_not_initialized", false).getErrorCode());
116+
FlagEvaluationDetails<Boolean> details = client.getBooleanDetails("return_error_when_not_initialized", false);
117+
assertEquals(ErrorCode.PROVIDER_NOT_READY, details.getErrorCode());
118+
assertEquals(Reason.ERROR.toString(), details.getReason());
117119
}
118120

119121
@Specification(number="1.1.5", text="The API MUST provide a function for retrieving the metadata field of the configured provider.")
@@ -264,6 +266,7 @@ public void initialize(EvaluationContext evaluationContext) throws Exception {
264266
FlagEvaluationDetails<Boolean> details = c.getBooleanDetails("key", defaultValue);
265267
assertEquals(ErrorCode.FLAG_NOT_FOUND, details.getErrorCode());
266268
assertEquals(TestConstants.BROKEN_MESSAGE, details.getErrorMessage());
269+
assertEquals(Reason.ERROR.toString(), details.getReason());
267270
assertEquals(defaultValue, details.getValue());
268271
}
269272

@@ -279,6 +282,7 @@ public void initialize(EvaluationContext evaluationContext) throws Exception {
279282
FlagEvaluationDetails<Boolean> details = c.getBooleanDetails("key", defaultValue);
280283
assertEquals(ErrorCode.FLAG_NOT_FOUND, details.getErrorCode());
281284
assertEquals(TestConstants.BROKEN_MESSAGE, details.getErrorMessage());
285+
assertEquals(Reason.ERROR.toString(), details.getReason());
282286
assertEquals(defaultValue, details.getValue());
283287
}
284288

0 commit comments

Comments
 (0)