kbendick commented on a change in pull request #4097:
URL: https://github.com/apache/iceberg/pull/4097#discussion_r807300504



##########
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:
       Yeah I'll remove the default case from the others.




-- 
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]

Reply via email to