Skip to content
Merged
Show file tree
Hide file tree
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
26 changes: 23 additions & 3 deletions common/src/main/java/com/skyflow/BaseVaultClient.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.skyflow;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.skyflow.config.BaseCredentials;
import com.skyflow.config.BaseVaultConfig;
import com.skyflow.errors.ErrorCode;
import com.skyflow.errors.BaseErrorMessage;
import com.skyflow.errors.SkyflowException;
import com.skyflow.generated.detect.rest.core.ObjectMappers;
import com.skyflow.logs.ErrorLogs;
import com.skyflow.logs.InfoLogs;
import com.skyflow.serviceaccount.util.Token;
Expand Down Expand Up @@ -108,7 +108,27 @@ protected static SkyflowException wrapApiException(int statusCode, Throwable cau
Map<String, List<String>> headers,
Object responseBody, ErrorLogs errorLog) {
LogUtil.printErrorLog(errorLog.getLog());
Gson gson = new GsonBuilder().serializeNulls().create();
return new SkyflowException(statusCode, cause, headers, gson.toJson(responseBody));
return new SkyflowException(statusCode, cause, headers, errorBodyAsJson(responseBody));
}

/**
* Serializes an API error body back to the JSON the server sent. The generated clients hand us either a
* plain {@code Map} or a typed {@code ErrorResponse}; the typed form carries Jackson annotations for the wire
* names ({@code grpc_code}, {@code http_status}) and {@code Optional} fields, so Jackson must do the
* serialization. Gson would emit the Java field names, which {@link SkyflowException} cannot parse, and
* fails outright on {@code Optional} under JDK 17+.
*/
static String errorBodyAsJson(Object responseBody) {
if (responseBody == null) {
return null;
}
if (responseBody instanceof String) {
return (String) responseBody;
}
try {
return ObjectMappers.JSON_MAPPER.writeValueAsString(responseBody);
} catch (Exception e) {
return new GsonBuilder().serializeNulls().create().toJson(responseBody);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.skyflow;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.skyflow.errors.SkyflowException;
import com.skyflow.logs.ErrorLogs;
import org.junit.Assert;
import org.junit.Test;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

public class BaseVaultClientErrorBodyTests {

/** Shape of the Fern-generated ErrorResponse: wire names via @JsonProperty, Optional fields. */
public static final class TypedError {
@JsonProperty("grpc_code") public Optional<Integer> grpcCode = Optional.of(5);
@JsonProperty("http_code") public Optional<Integer> httpCode = Optional.of(404);
@JsonProperty("http_status") public Optional<String> httpStatus = Optional.of("Not Found");
@JsonProperty("message") public Optional<String> message = Optional.of("Detokenization failed. Token x is invalid.");
@JsonProperty("details") public Optional<List<Object>> details = Optional.of(Collections.emptyList());
}

public static final class TypedErrorResponse {
@JsonProperty("error") public TypedError error = new TypedError();
}

private static Map<String, List<String>> headers() {
Map<String, List<String>> h = new HashMap<>();
h.put("x-request-id", Collections.singletonList("req-1"));
return h;
}

@Test
public void typedErrorBodyIsParsedIntoTheExceptionFields() {
SkyflowException e = BaseVaultClient.wrapApiException(404, new RuntimeException("boom"), headers(),
new TypedErrorResponse(), ErrorLogs.DETOKENIZE_REQUEST_REJECTED);
Assert.assertEquals(404, e.getHttpCode());
Assert.assertEquals("Not Found", e.getHttpStatus());
Assert.assertEquals(Integer.valueOf(5), e.getGrpcCode());
Assert.assertEquals("Detokenization failed. Token x is invalid.", e.getMessage());
Assert.assertEquals("req-1", e.getRequestId());
Assert.assertEquals(0, e.getDetails().size());
}

@Test
public void mapErrorBodyStillWorks() {
Map<String, Object> error = new HashMap<>();
error.put("grpc_code", 3);
error.put("http_status", "Bad Request");
error.put("message", "Insert failed. Column cv is invalid.");
error.put("details", Collections.emptyList());
Map<String, Object> body = new HashMap<>();
body.put("error", error);
SkyflowException e = BaseVaultClient.wrapApiException(400, new RuntimeException("boom"), headers(), body,
ErrorLogs.INSERT_RECORDS_REJECTED);
Assert.assertEquals("Bad Request", e.getHttpStatus());
Assert.assertEquals(Integer.valueOf(3), e.getGrpcCode());
Assert.assertEquals("Insert failed. Column cv is invalid.", e.getMessage());
}

@Test
public void nullAndStringBodiesDoNotThrow() {
Assert.assertNull(BaseVaultClient.errorBodyAsJson(null));
Assert.assertEquals("{\"error\":{}}", BaseVaultClient.errorBodyAsJson("{\"error\":{}}"));
}
}
2 changes: 1 addition & 1 deletion flowvault/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</parent>

<artifactId>skyflow-flowvault-java</artifactId>
<version>1.2.0-dev.225f34ec</version>
<version>1.2.0-dev.3657b62d</version>
<packaging>jar</packaging>
<name>${project.groupId}:${project.artifactId}</name>
<description>Skyflow FlowVault SDK for the Java programming language</description>
Expand Down
Loading