Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ public static ErrorHandler getInstance() {

@Override
public void accept(ErrorResponse error, String requestId) {
int code = error.getCode();
Integer errorCode = error.getCode();
// HttpClient always resolves the code before calling this, but the response may also be
// deserialized directly, and then "code" is absent whenever the server omits it.
int code = errorCode == null ? 0 : errorCode;
String message;
if (DEFAULT_REQUEST_ID.equals(requestId)) {
message = error.getMessage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public class ErrorResponse implements RESTResponse {
@JsonProperty(FIELD_MESSAGE)
private final String message;

@Nullable
@JsonProperty(FIELD_CODE)
private final Integer code;

Expand All @@ -79,7 +80,7 @@ public ErrorResponse(
@Nullable @JsonProperty(FIELD_RESOURCE_TYPE) String resourceType,
@Nullable @JsonProperty(FIELD_RESOURCE_NAME) String resourceName,
@JsonProperty(FIELD_MESSAGE) String message,
@JsonProperty(FIELD_CODE) int code) {
@Nullable @JsonProperty(FIELD_CODE) Integer code) {
this.resourceType = resourceType;
this.resourceName = resourceName;
this.message = message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.paimon.rest.auth.RESTAuthFunction;
import org.apache.paimon.rest.auth.RESTAuthParameter;
import org.apache.paimon.rest.exceptions.BadRequestException;
import org.apache.paimon.rest.exceptions.NoSuchResourceException;
import org.apache.paimon.rest.exceptions.RESTException;
import org.apache.paimon.rest.responses.ErrorResponse;

Expand All @@ -45,6 +46,7 @@
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

/** Test for {@link HttpClient}. */
Expand Down Expand Up @@ -255,6 +257,17 @@ public void testUrl() {
assertEquals(restAuthParameter.parameters().get(queryKey), queryParameters.get(queryKey));
}

@Test
public void testErrorCodeFallsBackToHttpStatus() throws Exception {
// "code" is optional in the error schema, so an error body may omit it. The HTTP status
// has to be used then, otherwise a 404 no longer maps to NoSuchResourceException.
assertNull(RESTApi.fromJson("{\"message\":\"x\"}", ErrorResponse.class).getCode());
server.enqueueResponse("{\"message\":\"Table t does not exist\"}", 404);
assertThrows(
NoSuchResourceException.class,
() -> httpClient.get(MOCK_PATH, MockRESTData.class, restAuthFunction));
}

private Map<String, String> getParameters(String path) {
String[] paths = path.split("\\?");
if (paths.length == 1) {
Expand Down Expand Up @@ -293,6 +306,10 @@ public void testGetWithUnparsableJsonErrorResponse() {
Assertions.assertTrue(
e.getMessage().contains("Empty error message"),
"Parsed-but-empty message must not be labelled unparseable");
Assertions.assertTrue(
e.getMessage().contains("403"),
"The HTTP status must be reported, not the absent body code: "
+ e.getMessage());
}
}

Expand Down
Loading