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
10 changes: 6 additions & 4 deletions core/src/main/java/org/apache/iceberg/rest/ErrorHandlers.java
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,6 @@ public void accept(ErrorResponse error) {
throw new NoSuchNamespaceException("%s", error.message());
case 409:
throw new AlreadyExistsException("%s", error.message());
case 422:
throw new RESTException("Unable to process: %s", error.message());
}

super.accept(error);
Expand Down Expand Up @@ -297,7 +295,9 @@ public void accept(ErrorResponse error) {
throw new ServiceUnavailableException("Service unavailable: %s", error.message());
}

throw new RESTException("Unable to process: %s", error.message());
throw new RESTException(
"Unable to process (code: %s, type: %s): %s",
error.code(), error.type(), error.message());
}
}

Expand Down Expand Up @@ -330,7 +330,9 @@ public void accept(ErrorResponse error) {
"Malformed request: %s: %s", error.type(), error.message());
}
}
throw new RESTException("Unable to process: %s", error.message());
throw new RESTException(
"Unable to process (code: %s, type: %s): %s",
error.code(), error.type(), error.message());
}
}
}
101 changes: 101 additions & 0 deletions core/src/test/java/org/apache/iceberg/rest/TestErrorHandlers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.iceberg.rest;

import static org.assertj.core.api.Assertions.assertThatThrownBy;

import org.apache.iceberg.exceptions.RESTException;
import org.apache.iceberg.rest.responses.ErrorResponse;
import org.junit.jupiter.api.Test;

class TestErrorHandlers {

@Test
public void testErrorHandlerIncludesCodeAndType() {
ErrorResponse error =
ErrorResponse.builder()
.responseCode(422)
.withType("ValidationException")
.withMessage("Invalid input")
.build();

assertThatThrownBy(() -> ErrorHandlers.defaultErrorHandler().accept(error))
.isInstanceOf(RESTException.class)
.hasMessageContaining(
"Unable to process (code: 422, type: ValidationException): Invalid input");
}

@Test
public void testErrorHandlerWithCodeOnly() {
ErrorResponse error = ErrorResponse.builder().responseCode(422).build();

assertThatThrownBy(() -> ErrorHandlers.defaultErrorHandler().accept(error))
.isInstanceOf(RESTException.class)
.hasMessageContaining("Unable to process (code: 422, type: null): null");
}

@Test
public void testErrorHandlerWithCodeAndMessageOnly() {
ErrorResponse error =
ErrorResponse.builder().responseCode(422).withMessage("Invalid input").build();

assertThatThrownBy(() -> ErrorHandlers.defaultErrorHandler().accept(error))
.isInstanceOf(RESTException.class)
.hasMessageContaining("Unable to process (code: 422, type: null): Invalid input");
}

@Test
public void testErrorHandlerWithCodeAndTypeOnly() {
ErrorResponse error =
ErrorResponse.builder().responseCode(422).withType("ValidationException").build();

assertThatThrownBy(() -> ErrorHandlers.defaultErrorHandler().accept(error))
.isInstanceOf(RESTException.class)
.hasMessageContaining("Unable to process (code: 422, type: ValidationException): null");
}

@Test
public void testNamespaceErrorHandlerFallsBackToDefaultFor422() {
ErrorResponse error =
ErrorResponse.builder()
.responseCode(422)
.withType("ValidationException")
.withMessage("Invalid input")
.build();

// NamespaceErrorHandler should fall back to DefaultErrorHandler for 422,
// which includes code, type, and message in the error message
assertThatThrownBy(() -> ErrorHandlers.namespaceErrorHandler().accept(error))
.isInstanceOf(RESTException.class)
.hasMessageContaining(
"Unable to process (code: 422, type: ValidationException): Invalid input");
}

@Test
public void testNamespaceErrorHandlerFallsBackToDefaultFor422WithOptionalFields() {
ErrorResponse error =
ErrorResponse.builder().responseCode(422).withMessage("Invalid input").build();

// NamespaceErrorHandler should fall back to DefaultErrorHandler for 422,
// even when type is missing
assertThatThrownBy(() -> ErrorHandlers.namespaceErrorHandler().accept(error))
.isInstanceOf(RESTException.class)
.hasMessageContaining("Unable to process (code: 422, type: null): Invalid input");
}
}