Skip to content

Commit 84f220d

Browse files
authored
feat!: errorCode as enum, reason as string (#80)
* feat!: errorCode as enum, reason as string - makes errorCode an enum - makes reason a string - adds errorMessage to resolution/evaluation details
1 parent e108666 commit 84f220d

18 files changed

+98
-47
lines changed

.github/workflows/pullrequest.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ jobs:
3434
${{ runner.os }}-maven-
3535
3636
- name: Build with Maven
37-
run: mvn --batch-mode --update-snapshots verify -P integration-test
37+
run: mvn --batch-mode --update-snapshots verify # -P integration-test - add this back once we have a compatible flagd
3838

3939
- name: Upload coverage to Codecov
4040
uses: codecov/codecov-action@v2

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/dev.openfeature/javasdk/badge.svg)](https://maven-badges.herokuapp.com/maven-central/dev.openfeature/javasdk)
44
[![javadoc](https://javadoc.io/badge2/dev.openfeature/javasdk/javadoc.svg)](https://javadoc.io/doc/dev.openfeature/javasdk)
55
[![Project Status: WIP – Initial development is in progress, but there has not yet been a stable, usable release suitable for the public.](https://www.repostatus.org/badges/latest/wip.svg)](https://www.repostatus.org/#wip)
6-
[![Specification](https://img.shields.io/static/v1?label=Specification&message=v0.4.0&color=yellow)](https://github.com/open-feature/spec/tree/v0.4.0)
6+
[![Specification](https://img.shields.io/static/v1?label=Specification&message=v0.5.0&color=yellow)](https://github.com/open-feature/spec/tree/v0.5.0)
77
[![Known Vulnerabilities](https://snyk.io/test/github/open-feature/java-sdk/badge.svg)](https://snyk.io/test/github/open-feature/java-sdk)
88
[![on-merge](https://github.com/open-feature/java-sdk/actions/workflows/merge.yml/badge.svg)](https://github.com/open-feature/java-sdk/actions/workflows/merge.yml)
99
[![codecov](https://codecov.io/gh/open-feature/java-sdk/branch/main/graph/badge.svg?token=XMS9L7PBY1)](https://codecov.io/gh/open-feature/java-sdk)

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@
141141
<dependency>
142142
<groupId>dev.openfeature.contrib.providers</groupId>
143143
<artifactId>flagd</artifactId>
144+
<!-- TODO: update this version -->
144145
<version>0.3.2</version>
145146
<scope>test</scope>
146147
</dependency>

src/main/java/dev/openfeature/javasdk/BaseEvaluation.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,18 @@ public interface BaseEvaluation<T> {
2121
* Describes how we came to the value that we're returning.
2222
* @return {Reason}
2323
*/
24-
Reason getReason();
24+
String getReason();
2525

2626
/**
2727
* The error code, if applicable. Should only be set when the Reason is ERROR.
2828
* @return {ErrorCode}
2929
*/
30-
String getErrorCode();
30+
ErrorCode getErrorCode();
31+
32+
/**
33+
* The error message (usually from exception.getMessage()), if applicable.
34+
* Should only be set when the Reason is ERROR.
35+
* @return {String}
36+
*/
37+
String getErrorMessage();
3138
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package dev.openfeature.javasdk;
22

33
public enum ErrorCode {
4-
PROVIDER_NOT_READY, FLAG_NOT_FOUND, PARSE_ERROR, TYPE_MISMATCH, GENERAL
4+
PROVIDER_NOT_READY, FLAG_NOT_FOUND, PARSE_ERROR, TYPE_MISMATCH, TARGETING_KEY_MISSING, INVALID_CONTEXT, GENERAL
55
}

src/main/java/dev/openfeature/javasdk/FlagEvaluationDetails.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ public class FlagEvaluationDetails<T> implements BaseEvaluation<T> {
1414
private String flagKey;
1515
private T value;
1616
@Nullable private String variant;
17-
private Reason reason;
18-
@Nullable private String errorCode;
17+
@Nullable private String reason;
18+
private ErrorCode errorCode;
19+
@Nullable private String errorMessage;
1920

2021
/**
2122
* Generate detail payload from the provider response.

src/main/java/dev/openfeature/javasdk/NoOpProvider.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public ProviderEvaluation<Boolean> getBooleanEvaluation(String key, Boolean defa
2525
return ProviderEvaluation.<Boolean>builder()
2626
.value(defaultValue)
2727
.variant(PASSED_IN_DEFAULT)
28-
.reason(Reason.DEFAULT)
28+
.reason(Reason.DEFAULT.toString())
2929
.build();
3030
}
3131

@@ -34,7 +34,7 @@ public ProviderEvaluation<String> getStringEvaluation(String key, String default
3434
return ProviderEvaluation.<String>builder()
3535
.value(defaultValue)
3636
.variant(PASSED_IN_DEFAULT)
37-
.reason(Reason.DEFAULT)
37+
.reason(Reason.DEFAULT.toString())
3838
.build();
3939
}
4040

@@ -43,7 +43,7 @@ public ProviderEvaluation<Integer> getIntegerEvaluation(String key, Integer defa
4343
return ProviderEvaluation.<Integer>builder()
4444
.value(defaultValue)
4545
.variant(PASSED_IN_DEFAULT)
46-
.reason(Reason.DEFAULT)
46+
.reason(Reason.DEFAULT.toString())
4747
.build();
4848
}
4949

@@ -52,7 +52,7 @@ public ProviderEvaluation<Double> getDoubleEvaluation(String key, Double default
5252
return ProviderEvaluation.<Double>builder()
5353
.value(defaultValue)
5454
.variant(PASSED_IN_DEFAULT)
55-
.reason(Reason.DEFAULT)
55+
.reason(Reason.DEFAULT.toString())
5656
.build();
5757
}
5858

@@ -62,7 +62,7 @@ public ProviderEvaluation<Value> getObjectEvaluation(String key, Value defaultVa
6262
return ProviderEvaluation.<Value>builder()
6363
.value(defaultValue)
6464
.variant(PASSED_IN_DEFAULT)
65-
.reason(Reason.DEFAULT)
65+
.reason(Reason.DEFAULT.toString())
6666
.build();
6767
}
6868
}

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.Map;
88

99
import dev.openfeature.javasdk.exceptions.GeneralError;
10+
import dev.openfeature.javasdk.exceptions.OpenFeatureError;
1011
import dev.openfeature.javasdk.internal.ObjectUtils;
1112
import lombok.Getter;
1213
import lombok.Setter;
@@ -94,9 +95,14 @@ private <T> FlagEvaluationDetails<T> evaluateFlag(FlagValueType type, String key
9495
if (details == null) {
9596
details = FlagEvaluationDetails.<T>builder().build();
9697
}
98+
if (e instanceof OpenFeatureError) {
99+
details.setErrorCode(((OpenFeatureError)e).getErrorCode());
100+
} else {
101+
details.setErrorCode(ErrorCode.GENERAL);
102+
}
103+
details.setErrorMessage(e.getMessage());
97104
details.setValue(defaultValue);
98-
details.setReason(Reason.ERROR);
99-
details.setErrorCode(e.getMessage());
105+
details.setReason(Reason.ERROR.toString());
100106
hookSupport.errorHooks(type, hookCtx, e, mergedHooks, hints);
101107
} finally {
102108
hookSupport.afterAllHooks(type, hookCtx, mergedHooks, hints);

src/main/java/dev/openfeature/javasdk/ProviderEvaluation.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
public class ProviderEvaluation<T> implements BaseEvaluation<T> {
1010
T value;
1111
@Nullable String variant;
12-
Reason reason;
13-
@Nullable String errorCode;
12+
@Nullable private String reason;
13+
ErrorCode errorCode;
14+
@Nullable private String errorMessage;
1415
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
@StandardException
88
public class FlagNotFoundError extends OpenFeatureError {
99
private static final long serialVersionUID = 1L;
10-
@Getter private final ErrorCode errorCode = ErrorCode.GENERAL;
10+
@Getter private final ErrorCode errorCode = ErrorCode.FLAG_NOT_FOUND;
1111
}

0 commit comments

Comments
 (0)