rdblue commented on a change in pull request #4097:
URL: https://github.com/apache/iceberg/pull/4097#discussion_r807130701
##########
File path: build.gradle
##########
@@ -221,6 +223,8 @@ project(':iceberg-core') {
exclude group: 'org.slf4j', module: 'slf4j-log4j12'
}
+ implementation 'org.apache.httpcomponents.client5:httpclient5'
Review comment:
This is already included with Spark, right? So we should make sure that
it is excluded from the Spark runtime Jars.
##########
File path: core/src/main/java/org/apache/iceberg/rest/ErrorHandlers.java
##########
@@ -0,0 +1,137 @@
+/*
+ * 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 java.io.IOException;
+import java.util.function.Consumer;
+import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
+import org.apache.hc.core5.http.HttpStatus;
+import org.apache.hc.core5.http.ParseException;
+import org.apache.hc.core5.http.io.entity.EntityUtils;
+import org.apache.iceberg.exceptions.AlreadyExistsException;
+import org.apache.iceberg.exceptions.BadRequestException;
+import org.apache.iceberg.exceptions.ForbiddenException;
+import org.apache.iceberg.exceptions.NoSuchNamespaceException;
+import org.apache.iceberg.exceptions.NoSuchTableException;
+import org.apache.iceberg.exceptions.NotAuthorizedException;
+import org.apache.iceberg.exceptions.RESTException;
+import org.apache.iceberg.exceptions.ServiceFailureException;
+import org.apache.iceberg.rest.responses.ErrorResponse;
+
+/**
+ * A set of consumers to handle errors for requests for table entities or for
namespace entities,
+ * to throw the correct exception.
+ */
+public class ErrorHandlers {
+
+ private ErrorHandlers() {
+ }
+
+ /**
+ * Table level error handlers.
+ * Should be chained wih the {@linkplain ErrorHandlers#defaultErrorHandler},
which takes care of common cases.
+ */
+ public static Consumer<ErrorResponse> tableErrorHandler() {
+ return error -> {
+
+ switch (error.code()) {
+ // table resource routes can encounter namespace not found or table
not found.
+ // TODO - Need to define response types (can probably use the HTTP
values and specialize from there).
+ case HttpStatus.SC_NOT_FOUND:
+ // TODO - error.type() could be null for HEAD responses. We probably
want to pass
Review comment:
For HEAD requests, this should simply throw `NoSuchTableException`
because the request is specifically for the table.
##########
File path: core/src/main/java/org/apache/iceberg/rest/ErrorHandlers.java
##########
@@ -0,0 +1,137 @@
+/*
+ * 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 java.io.IOException;
+import java.util.function.Consumer;
+import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
+import org.apache.hc.core5.http.HttpStatus;
+import org.apache.hc.core5.http.ParseException;
+import org.apache.hc.core5.http.io.entity.EntityUtils;
+import org.apache.iceberg.exceptions.AlreadyExistsException;
+import org.apache.iceberg.exceptions.BadRequestException;
+import org.apache.iceberg.exceptions.ForbiddenException;
+import org.apache.iceberg.exceptions.NoSuchNamespaceException;
+import org.apache.iceberg.exceptions.NoSuchTableException;
+import org.apache.iceberg.exceptions.NotAuthorizedException;
+import org.apache.iceberg.exceptions.RESTException;
+import org.apache.iceberg.exceptions.ServiceFailureException;
+import org.apache.iceberg.rest.responses.ErrorResponse;
+
+/**
+ * A set of consumers to handle errors for requests for table entities or for
namespace entities,
+ * to throw the correct exception.
+ */
+public class ErrorHandlers {
+
+ private ErrorHandlers() {
+ }
+
+ /**
+ * Table level error handlers.
+ * Should be chained wih the {@linkplain ErrorHandlers#defaultErrorHandler},
which takes care of common cases.
+ */
+ public static Consumer<ErrorResponse> tableErrorHandler() {
+ return error -> {
+
+ switch (error.code()) {
+ // table resource routes can encounter namespace not found or table
not found.
+ // TODO - Need to define response types (can probably use the HTTP
values and specialize from there).
+ case HttpStatus.SC_NOT_FOUND:
+ // TODO - error.type() could be null for HEAD responses. We probably
want to pass
+ // in some request info using a BiConsumer so we can have the
table name etc
+ // from the request.
+ if
(NoSuchNamespaceException.class.getSimpleName().equals(error.type())) {
+ throw new NoSuchNamespaceException("Resource not found: %s",
error);
+ } else {
+ throw new NoSuchTableException("Resource not found: %s", error);
+ }
+ case HttpStatus.SC_CONFLICT:
+ throw new AlreadyExistsException("The table already exists: %s",
error);
+ default:
+ throw new RESTException("Unknown error: %s", error);
+ }
+ };
+ }
+
+ /**
+ * Request error handlers specifically for CRUD ops on namespaces.
+ * Should be chained wih the {@linkplain ErrorHandlers#defaultErrorHandler},
which takes care of common cases.
+ */
+ public static Consumer<ErrorResponse> namespaceErrorHandler() {
+ return error -> {
+ switch (error.code()) {
+ case HttpStatus.SC_NOT_FOUND:
+ throw new NoSuchNamespaceException("Namespace not found: %s", error);
+ case HttpStatus.SC_CONFLICT:
+ throw new AlreadyExistsException("Namespace already exists: %s",
error);
+ // Invalid UpdateNamespacePropertiesRequest - keys to remove
overlapped with keys of properties to add
Review comment:
What does this comment mean? Isn't this what `SC_UNPROCESSABLE_ENTITY`
means?
([spec](https://github.com/apache/iceberg/blob/master/docs/rest/rest-catalog-open-api.yaml#L303))
##########
File path: core/src/main/java/org/apache/iceberg/rest/ErrorHandlers.java
##########
@@ -0,0 +1,137 @@
+/*
+ * 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 java.io.IOException;
+import java.util.function.Consumer;
+import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
+import org.apache.hc.core5.http.HttpStatus;
+import org.apache.hc.core5.http.ParseException;
+import org.apache.hc.core5.http.io.entity.EntityUtils;
+import org.apache.iceberg.exceptions.AlreadyExistsException;
+import org.apache.iceberg.exceptions.BadRequestException;
+import org.apache.iceberg.exceptions.ForbiddenException;
+import org.apache.iceberg.exceptions.NoSuchNamespaceException;
+import org.apache.iceberg.exceptions.NoSuchTableException;
+import org.apache.iceberg.exceptions.NotAuthorizedException;
+import org.apache.iceberg.exceptions.RESTException;
+import org.apache.iceberg.exceptions.ServiceFailureException;
+import org.apache.iceberg.rest.responses.ErrorResponse;
+
+/**
+ * A set of consumers to handle errors for requests for table entities or for
namespace entities,
+ * to throw the correct exception.
+ */
+public class ErrorHandlers {
+
+ private ErrorHandlers() {
+ }
+
+ /**
+ * Table level error handlers.
+ * Should be chained wih the {@linkplain ErrorHandlers#defaultErrorHandler},
which takes care of common cases.
+ */
+ public static Consumer<ErrorResponse> tableErrorHandler() {
+ return error -> {
+
+ switch (error.code()) {
+ // table resource routes can encounter namespace not found or table
not found.
+ // TODO - Need to define response types (can probably use the HTTP
values and specialize from there).
+ case HttpStatus.SC_NOT_FOUND:
+ // TODO - error.type() could be null for HEAD responses. We probably
want to pass
+ // in some request info using a BiConsumer so we can have the
table name etc
+ // from the request.
+ if
(NoSuchNamespaceException.class.getSimpleName().equals(error.type())) {
+ throw new NoSuchNamespaceException("Resource not found: %s",
error);
+ } else {
+ throw new NoSuchTableException("Resource not found: %s", error);
+ }
+ case HttpStatus.SC_CONFLICT:
+ throw new AlreadyExistsException("The table already exists: %s",
error);
+ default:
+ throw new RESTException("Unknown error: %s", error);
+ }
+ };
+ }
+
+ /**
+ * Request error handlers specifically for CRUD ops on namespaces.
+ * Should be chained wih the {@linkplain ErrorHandlers#defaultErrorHandler},
which takes care of common cases.
+ */
+ public static Consumer<ErrorResponse> namespaceErrorHandler() {
+ return error -> {
+ switch (error.code()) {
+ case HttpStatus.SC_NOT_FOUND:
+ throw new NoSuchNamespaceException("Namespace not found: %s", error);
+ case HttpStatus.SC_CONFLICT:
+ throw new AlreadyExistsException("Namespace already exists: %s",
error);
+ // Invalid UpdateNamespacePropertiesRequest - keys to remove
overlapped with keys of properties to add
+ case HttpStatus.SC_UNPROCESSABLE_ENTITY:
+ throw new RESTException("Unable to process request due to bad
property updates");
+ case HttpStatus.SC_FORBIDDEN:
Review comment:
There is an exception class for this. Should this throw it?
##########
File path: core/src/main/java/org/apache/iceberg/rest/ErrorHandlers.java
##########
@@ -0,0 +1,137 @@
+/*
+ * 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 java.io.IOException;
+import java.util.function.Consumer;
+import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
+import org.apache.hc.core5.http.HttpStatus;
+import org.apache.hc.core5.http.ParseException;
+import org.apache.hc.core5.http.io.entity.EntityUtils;
+import org.apache.iceberg.exceptions.AlreadyExistsException;
+import org.apache.iceberg.exceptions.BadRequestException;
+import org.apache.iceberg.exceptions.ForbiddenException;
+import org.apache.iceberg.exceptions.NoSuchNamespaceException;
+import org.apache.iceberg.exceptions.NoSuchTableException;
+import org.apache.iceberg.exceptions.NotAuthorizedException;
+import org.apache.iceberg.exceptions.RESTException;
+import org.apache.iceberg.exceptions.ServiceFailureException;
+import org.apache.iceberg.rest.responses.ErrorResponse;
+
+/**
+ * A set of consumers to handle errors for requests for table entities or for
namespace entities,
+ * to throw the correct exception.
+ */
+public class ErrorHandlers {
+
+ private ErrorHandlers() {
+ }
+
+ /**
+ * Table level error handlers.
+ * Should be chained wih the {@linkplain ErrorHandlers#defaultErrorHandler},
which takes care of common cases.
+ */
+ public static Consumer<ErrorResponse> tableErrorHandler() {
+ return error -> {
+
+ switch (error.code()) {
+ // table resource routes can encounter namespace not found or table
not found.
+ // TODO - Need to define response types (can probably use the HTTP
values and specialize from there).
+ case HttpStatus.SC_NOT_FOUND:
+ // TODO - error.type() could be null for HEAD responses. We probably
want to pass
+ // in some request info using a BiConsumer so we can have the
table name etc
+ // from the request.
+ if
(NoSuchNamespaceException.class.getSimpleName().equals(error.type())) {
+ throw new NoSuchNamespaceException("Resource not found: %s",
error);
+ } else {
+ throw new NoSuchTableException("Resource not found: %s", error);
+ }
+ case HttpStatus.SC_CONFLICT:
+ throw new AlreadyExistsException("The table already exists: %s",
error);
+ default:
+ throw new RESTException("Unknown error: %s", error);
+ }
+ };
+ }
+
+ /**
+ * Request error handlers specifically for CRUD ops on namespaces.
+ * Should be chained wih the {@linkplain ErrorHandlers#defaultErrorHandler},
which takes care of common cases.
+ */
+ public static Consumer<ErrorResponse> namespaceErrorHandler() {
+ return error -> {
+ switch (error.code()) {
+ case HttpStatus.SC_NOT_FOUND:
+ throw new NoSuchNamespaceException("Namespace not found: %s", error);
+ case HttpStatus.SC_CONFLICT:
+ throw new AlreadyExistsException("Namespace already exists: %s",
error);
+ // Invalid UpdateNamespacePropertiesRequest - keys to remove
overlapped with keys of properties to add
+ case HttpStatus.SC_UNPROCESSABLE_ENTITY:
+ throw new RESTException("Unable to process request due to bad
property updates");
+ case HttpStatus.SC_FORBIDDEN:
+ case HttpStatus.SC_UNAUTHORIZED:
+ throw new NotAuthorizedException("Not Authorized: %s", error);
+ default:
+ throw new RESTException("Unknown error: %s", error);
+ }
+ };
+ }
+
+ /**
+ * Request error handler that handles the common cases that are included
with all responses,
+ * such as 400, 401, 403, 500, etc.
+ */
+ public static Consumer<ErrorResponse> defaultErrorHandler() {
+ return errorResponse -> {
+ switch (errorResponse.code()) {
+ // 400
+ case HttpStatus.SC_CLIENT_ERROR:
+ throw new BadRequestException("Unable to process the request as it
is somehow malformed: %s", errorResponse);
+ // 401
+ case HttpStatus.SC_UNAUTHORIZED:
+ throw new NotAuthorizedException("Not Authorized: %s",
errorResponse);
+ // 403
+ case HttpStatus.SC_FORBIDDEN:
+ throw new ForbiddenException("Forbidden: %s", errorResponse);
+ // 405 & 406 & 501 - All of these can represent not supported.
+ case HttpStatus.SC_METHOD_NOT_ALLOWED:
+ case HttpStatus.SC_NOT_ACCEPTABLE:
+ case HttpStatus.SC_NOT_IMPLEMENTED:
+ throw new UnsupportedOperationException(String.format("Not
supported: %s", errorResponse));
+ // 500
+ case HttpStatus.SC_SERVER_ERROR:
+ throw new ServiceFailureException("Server error: %s", errorResponse);
+ default:
+ throw new RESTException("Unknown error: %s", errorResponse);
Review comment:
Both this handler and the others throw in a default case, so they can't
be chained. I think you want to chain them, right?
##########
File path: core/src/main/java/org/apache/iceberg/rest/ErrorHandlers.java
##########
@@ -0,0 +1,137 @@
+/*
+ * 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 java.io.IOException;
+import java.util.function.Consumer;
+import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
+import org.apache.hc.core5.http.HttpStatus;
+import org.apache.hc.core5.http.ParseException;
+import org.apache.hc.core5.http.io.entity.EntityUtils;
+import org.apache.iceberg.exceptions.AlreadyExistsException;
+import org.apache.iceberg.exceptions.BadRequestException;
+import org.apache.iceberg.exceptions.ForbiddenException;
+import org.apache.iceberg.exceptions.NoSuchNamespaceException;
+import org.apache.iceberg.exceptions.NoSuchTableException;
+import org.apache.iceberg.exceptions.NotAuthorizedException;
+import org.apache.iceberg.exceptions.RESTException;
+import org.apache.iceberg.exceptions.ServiceFailureException;
+import org.apache.iceberg.rest.responses.ErrorResponse;
+
+/**
+ * A set of consumers to handle errors for requests for table entities or for
namespace entities,
+ * to throw the correct exception.
+ */
+public class ErrorHandlers {
+
+ private ErrorHandlers() {
+ }
+
+ /**
+ * Table level error handlers.
+ * Should be chained wih the {@linkplain ErrorHandlers#defaultErrorHandler},
which takes care of common cases.
+ */
+ public static Consumer<ErrorResponse> tableErrorHandler() {
+ return error -> {
+
+ switch (error.code()) {
+ // table resource routes can encounter namespace not found or table
not found.
+ // TODO - Need to define response types (can probably use the HTTP
values and specialize from there).
+ case HttpStatus.SC_NOT_FOUND:
+ // TODO - error.type() could be null for HEAD responses. We probably
want to pass
+ // in some request info using a BiConsumer so we can have the
table name etc
+ // from the request.
+ if
(NoSuchNamespaceException.class.getSimpleName().equals(error.type())) {
+ throw new NoSuchNamespaceException("Resource not found: %s",
error);
+ } else {
+ throw new NoSuchTableException("Resource not found: %s", error);
+ }
+ case HttpStatus.SC_CONFLICT:
+ throw new AlreadyExistsException("The table already exists: %s",
error);
+ default:
+ throw new RESTException("Unknown error: %s", error);
+ }
+ };
+ }
+
+ /**
+ * Request error handlers specifically for CRUD ops on namespaces.
+ * Should be chained wih the {@linkplain ErrorHandlers#defaultErrorHandler},
which takes care of common cases.
+ */
+ public static Consumer<ErrorResponse> namespaceErrorHandler() {
+ return error -> {
+ switch (error.code()) {
+ case HttpStatus.SC_NOT_FOUND:
+ throw new NoSuchNamespaceException("Namespace not found: %s", error);
+ case HttpStatus.SC_CONFLICT:
+ throw new AlreadyExistsException("Namespace already exists: %s",
error);
+ // Invalid UpdateNamespacePropertiesRequest - keys to remove
overlapped with keys of properties to add
+ case HttpStatus.SC_UNPROCESSABLE_ENTITY:
+ throw new RESTException("Unable to process request due to bad
property updates");
+ case HttpStatus.SC_FORBIDDEN:
+ case HttpStatus.SC_UNAUTHORIZED:
+ throw new NotAuthorizedException("Not Authorized: %s", error);
+ default:
+ throw new RESTException("Unknown error: %s", error);
+ }
+ };
+ }
+
+ /**
+ * Request error handler that handles the common cases that are included
with all responses,
+ * such as 400, 401, 403, 500, etc.
+ */
+ public static Consumer<ErrorResponse> defaultErrorHandler() {
+ return errorResponse -> {
+ switch (errorResponse.code()) {
+ // 400
+ case HttpStatus.SC_CLIENT_ERROR:
+ throw new BadRequestException("Unable to process the request as it
is somehow malformed: %s", errorResponse);
+ // 401
+ case HttpStatus.SC_UNAUTHORIZED:
+ throw new NotAuthorizedException("Not Authorized: %s",
errorResponse);
+ // 403
+ case HttpStatus.SC_FORBIDDEN:
+ throw new ForbiddenException("Forbidden: %s", errorResponse);
+ // 405 & 406 & 501 - All of these can represent not supported.
+ case HttpStatus.SC_METHOD_NOT_ALLOWED:
+ case HttpStatus.SC_NOT_ACCEPTABLE:
+ case HttpStatus.SC_NOT_IMPLEMENTED:
+ throw new UnsupportedOperationException(String.format("Not
supported: %s", errorResponse));
+ // 500
+ case HttpStatus.SC_SERVER_ERROR:
+ throw new ServiceFailureException("Server error: %s", errorResponse);
+ default:
+ throw new RESTException("Unknown error: %s", errorResponse);
+ }
+ };
+ }
+
+ static String extractResponseBodyAsString(CloseableHttpResponse response) {
Review comment:
Is this used? I thought this part of the API was going to avoid
depending on a specific HTTP library.
##########
File path: core/src/main/java/org/apache/iceberg/rest/ErrorHandlers.java
##########
@@ -0,0 +1,137 @@
+/*
+ * 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 java.io.IOException;
+import java.util.function.Consumer;
+import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
+import org.apache.hc.core5.http.HttpStatus;
+import org.apache.hc.core5.http.ParseException;
+import org.apache.hc.core5.http.io.entity.EntityUtils;
+import org.apache.iceberg.exceptions.AlreadyExistsException;
+import org.apache.iceberg.exceptions.BadRequestException;
+import org.apache.iceberg.exceptions.ForbiddenException;
+import org.apache.iceberg.exceptions.NoSuchNamespaceException;
+import org.apache.iceberg.exceptions.NoSuchTableException;
+import org.apache.iceberg.exceptions.NotAuthorizedException;
+import org.apache.iceberg.exceptions.RESTException;
+import org.apache.iceberg.exceptions.ServiceFailureException;
+import org.apache.iceberg.rest.responses.ErrorResponse;
+
+/**
+ * A set of consumers to handle errors for requests for table entities or for
namespace entities,
+ * to throw the correct exception.
+ */
+public class ErrorHandlers {
+
+ private ErrorHandlers() {
+ }
+
+ /**
+ * Table level error handlers.
+ * Should be chained wih the {@linkplain ErrorHandlers#defaultErrorHandler},
which takes care of common cases.
+ */
+ public static Consumer<ErrorResponse> tableErrorHandler() {
+ return error -> {
+
+ switch (error.code()) {
+ // table resource routes can encounter namespace not found or table
not found.
+ // TODO - Need to define response types (can probably use the HTTP
values and specialize from there).
+ case HttpStatus.SC_NOT_FOUND:
+ // TODO - error.type() could be null for HEAD responses. We probably
want to pass
+ // in some request info using a BiConsumer so we can have the
table name etc
+ // from the request.
+ if
(NoSuchNamespaceException.class.getSimpleName().equals(error.type())) {
+ throw new NoSuchNamespaceException("Resource not found: %s",
error);
+ } else {
+ throw new NoSuchTableException("Resource not found: %s", error);
+ }
+ case HttpStatus.SC_CONFLICT:
+ throw new AlreadyExistsException("The table already exists: %s",
error);
+ default:
+ throw new RESTException("Unknown error: %s", error);
+ }
+ };
+ }
+
+ /**
+ * Request error handlers specifically for CRUD ops on namespaces.
+ * Should be chained wih the {@linkplain ErrorHandlers#defaultErrorHandler},
which takes care of common cases.
+ */
+ public static Consumer<ErrorResponse> namespaceErrorHandler() {
+ return error -> {
+ switch (error.code()) {
+ case HttpStatus.SC_NOT_FOUND:
+ throw new NoSuchNamespaceException("Namespace not found: %s", error);
+ case HttpStatus.SC_CONFLICT:
+ throw new AlreadyExistsException("Namespace already exists: %s",
error);
+ // Invalid UpdateNamespacePropertiesRequest - keys to remove
overlapped with keys of properties to add
+ case HttpStatus.SC_UNPROCESSABLE_ENTITY:
+ throw new RESTException("Unable to process request due to bad
property updates");
+ case HttpStatus.SC_FORBIDDEN:
+ case HttpStatus.SC_UNAUTHORIZED:
+ throw new NotAuthorizedException("Not Authorized: %s", error);
+ default:
+ throw new RESTException("Unknown error: %s", error);
+ }
+ };
+ }
+
+ /**
+ * Request error handler that handles the common cases that are included
with all responses,
+ * such as 400, 401, 403, 500, etc.
+ */
+ public static Consumer<ErrorResponse> defaultErrorHandler() {
+ return errorResponse -> {
+ switch (errorResponse.code()) {
+ // 400
+ case HttpStatus.SC_CLIENT_ERROR:
+ throw new BadRequestException("Unable to process the request as it
is somehow malformed: %s", errorResponse);
+ // 401
+ case HttpStatus.SC_UNAUTHORIZED:
+ throw new NotAuthorizedException("Not Authorized: %s",
errorResponse);
+ // 403
+ case HttpStatus.SC_FORBIDDEN:
+ throw new ForbiddenException("Forbidden: %s", errorResponse);
+ // 405 & 406 & 501 - All of these can represent not supported.
+ case HttpStatus.SC_METHOD_NOT_ALLOWED:
+ case HttpStatus.SC_NOT_ACCEPTABLE:
+ case HttpStatus.SC_NOT_IMPLEMENTED:
+ throw new UnsupportedOperationException(String.format("Not
supported: %s", errorResponse));
+ // 500
+ case HttpStatus.SC_SERVER_ERROR:
+ throw new ServiceFailureException("Server error: %s", errorResponse);
+ default:
+ throw new RESTException("Unknown error: %s", errorResponse);
+ }
+ };
+ }
+
+ static String extractResponseBodyAsString(CloseableHttpResponse response) {
Review comment:
Looks like this should be moved into `HttpRESTClient`.
##########
File path: core/src/main/java/org/apache/iceberg/rest/ErrorHandlers.java
##########
@@ -0,0 +1,137 @@
+/*
+ * 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 java.io.IOException;
+import java.util.function.Consumer;
+import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
+import org.apache.hc.core5.http.HttpStatus;
+import org.apache.hc.core5.http.ParseException;
+import org.apache.hc.core5.http.io.entity.EntityUtils;
+import org.apache.iceberg.exceptions.AlreadyExistsException;
+import org.apache.iceberg.exceptions.BadRequestException;
+import org.apache.iceberg.exceptions.ForbiddenException;
+import org.apache.iceberg.exceptions.NoSuchNamespaceException;
+import org.apache.iceberg.exceptions.NoSuchTableException;
+import org.apache.iceberg.exceptions.NotAuthorizedException;
+import org.apache.iceberg.exceptions.RESTException;
+import org.apache.iceberg.exceptions.ServiceFailureException;
+import org.apache.iceberg.rest.responses.ErrorResponse;
+
+/**
+ * A set of consumers to handle errors for requests for table entities or for
namespace entities,
+ * to throw the correct exception.
+ */
+public class ErrorHandlers {
+
+ private ErrorHandlers() {
+ }
+
+ /**
+ * Table level error handlers.
+ * Should be chained wih the {@linkplain ErrorHandlers#defaultErrorHandler},
which takes care of common cases.
+ */
+ public static Consumer<ErrorResponse> tableErrorHandler() {
+ return error -> {
+
+ switch (error.code()) {
+ // table resource routes can encounter namespace not found or table
not found.
+ // TODO - Need to define response types (can probably use the HTTP
values and specialize from there).
+ case HttpStatus.SC_NOT_FOUND:
+ // TODO - error.type() could be null for HEAD responses. We probably
want to pass
+ // in some request info using a BiConsumer so we can have the
table name etc
+ // from the request.
+ if
(NoSuchNamespaceException.class.getSimpleName().equals(error.type())) {
+ throw new NoSuchNamespaceException("Resource not found: %s",
error);
+ } else {
+ throw new NoSuchTableException("Resource not found: %s", error);
+ }
+ case HttpStatus.SC_CONFLICT:
+ throw new AlreadyExistsException("The table already exists: %s",
error);
+ default:
+ throw new RESTException("Unknown error: %s", error);
+ }
+ };
+ }
+
+ /**
+ * Request error handlers specifically for CRUD ops on namespaces.
+ * Should be chained wih the {@linkplain ErrorHandlers#defaultErrorHandler},
which takes care of common cases.
+ */
+ public static Consumer<ErrorResponse> namespaceErrorHandler() {
+ return error -> {
+ switch (error.code()) {
+ case HttpStatus.SC_NOT_FOUND:
+ throw new NoSuchNamespaceException("Namespace not found: %s", error);
+ case HttpStatus.SC_CONFLICT:
+ throw new AlreadyExistsException("Namespace already exists: %s",
error);
+ // Invalid UpdateNamespacePropertiesRequest - keys to remove
overlapped with keys of properties to add
+ case HttpStatus.SC_UNPROCESSABLE_ENTITY:
+ throw new RESTException("Unable to process request due to bad
property updates");
+ case HttpStatus.SC_FORBIDDEN:
+ case HttpStatus.SC_UNAUTHORIZED:
Review comment:
I think this and forbidden should be handled by the default handler.
##########
File path: core/src/main/java/org/apache/iceberg/rest/RESTClient.java
##########
@@ -0,0 +1,40 @@
+/*
+ * 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 java.io.Closeable;
+import java.util.function.Consumer;
+import org.apache.iceberg.rest.responses.ErrorResponse;
+
+/**
+ * Interface for a basic HTTP Client for interfacing with the REST catalog.
+ */
+public interface RESTClient extends Closeable {
Review comment:
Should this have a method that can set the default error handler?
##########
File path: core/src/main/java/org/apache/iceberg/util/JsonUtil.java
##########
@@ -49,6 +52,17 @@ public static ObjectMapper mapper() {
return MAPPER;
}
+ public static ObjectMapper withRESTComponents() {
+ RESTSerializers.registerAll(MAPPER);
+ // These are a workaround for Jackson since Iceberg doesn't use the
standard get/set bean notation.
+ // This allows Jackson to work with the fields directly (both public and
private) and not require
+ // custom (de)serializers for all the request/response objects.
+ MAPPER.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
+ MAPPER.setVisibility(PropertyAccessor.CREATOR,
JsonAutoDetect.Visibility.ANY); // deserialization
+ MAPPER.setVisibility(PropertyAccessor.FIELD,
JsonAutoDetect.Visibility.ANY); // serialization
+ return MAPPER;
+ }
+
Review comment:
I thought we talked about removing these. Can't the tests instantiate
their own mapper?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]