Skip to content

Commit 13eadc9

Browse files
committed
feat: address comments and use consts
1 parent f869a62 commit 13eadc9

File tree

2 files changed

+100
-73
lines changed

2 files changed

+100
-73
lines changed

src/main/java/dev/openfga/sdk/errors/FgaError.java

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,20 @@
22

33
import static dev.openfga.sdk.errors.HttpStatusCode.*;
44

5+
import com.fasterxml.jackson.annotation.JsonInclude;
56
import com.fasterxml.jackson.core.JsonProcessingException;
7+
import com.fasterxml.jackson.databind.DeserializationFeature;
68
import com.fasterxml.jackson.databind.ObjectMapper;
9+
import com.fasterxml.jackson.databind.SerializationFeature;
10+
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
711
import dev.openfga.sdk.api.configuration.Configuration;
812
import dev.openfga.sdk.api.configuration.CredentialsMethod;
913
import dev.openfga.sdk.constants.FgaConstants;
1014
import java.net.http.HttpHeaders;
1115
import java.net.http.HttpRequest;
1216
import java.net.http.HttpResponse;
1317
import java.util.Optional;
18+
import org.openapitools.jackson.nullable.JsonNullableModule;
1419

1520
public class FgaError extends ApiException {
1621
private String method = null;
@@ -24,10 +29,24 @@ public class FgaError extends ApiException {
2429
private String apiErrorMessage = null;
2530
private String operationName = null;
2631

27-
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
32+
private static final ObjectMapper OBJECT_MAPPER = createConfiguredObjectMapper();
33+
34+
private static ObjectMapper createConfiguredObjectMapper() {
35+
ObjectMapper mapper = new ObjectMapper();
36+
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
37+
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
38+
mapper.configure(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE, false);
39+
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
40+
mapper.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
41+
mapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING);
42+
mapper.disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE);
43+
mapper.registerModule(new JavaTimeModule());
44+
mapper.registerModule(new JsonNullableModule());
45+
return mapper;
46+
}
2847

2948
@com.fasterxml.jackson.annotation.JsonIgnoreProperties(ignoreUnknown = true)
30-
public static class ApiErrorResponse {
49+
private static class ApiErrorResponse {
3150
@com.fasterxml.jackson.annotation.JsonProperty("code")
3251
private String code;
3352

@@ -312,9 +331,7 @@ public boolean isRateLimitError() {
312331
* @return true if this error is retryable
313332
*/
314333
public boolean isRetryable() {
315-
int status = getStatusCode();
316-
// 429 (Rate Limit) and 5xx (Server Errors) are typically retryable.
317-
return status == TOO_MANY_REQUESTS || (status >= 500 && status < 600);
334+
return HttpStatusCode.isRetryable(getStatusCode());
318335
}
319336

320337
/**
@@ -333,7 +350,6 @@ public boolean isClientError() {
333350
* @return true if this is a 5xx error
334351
*/
335352
public boolean isServerError() {
336-
int status = getStatusCode();
337-
return status >= 500 && status < 600;
353+
return HttpStatusCode.isServerError(getStatusCode());
338354
}
339355
}

0 commit comments

Comments
 (0)