You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by GitBox <gi...@apache.org> on 2022/02/23 01:46:58 UTC

[GitHub] [iceberg] kbendick opened a new pull request #4202: CORE - Add main error handlers for use with REST catalog's HTTP client

kbendick opened a new pull request #4202:
URL: https://github.com/apache/iceberg/pull/4202


   These are a set of error handlers, to be used with the `RESTClient` and its HTTP implementation, when an `ErrorResponse` is received.
   
   This allows for individual routes to override behavior, based on their specification.
   
   As the error handlers are just `Consumer<ErrorResponse>`, they can be chained together via `andThen`.
   
   A base error handler is provided, which covers response codes we expect from any route. It should be last in the chain.
   
   See also https://github.com/apache/iceberg/pull/4201, which is where `ErrorResponse` is defined.
   
   This is being posted to unblock work on the testing harnesses and infrastructure for the REST catalog.
   
   cc @rdblue 


-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] rdblue commented on a change in pull request #4202: CORE - Add main error handlers for use with REST catalog's HTTP client

Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #4202:
URL: https://github.com/apache/iceberg/pull/4202#discussion_r812527925



##########
File path: core/src/main/java/org/apache/iceberg/rest/ErrorHandlers.java
##########
@@ -0,0 +1,124 @@
+/*
+ * 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.util.function.Consumer;
+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() {
+  }
+
+  public static Consumer<ErrorResponse> namespaceErrorHandler() {
+    return baseNamespaceErrorHandler().andThen(defaultErrorHandler());
+  }
+
+  public static Consumer<ErrorResponse> tableErrorHandler() {
+    return baseTableErrorHandler().andThen(defaultErrorHandler());
+  }
+
+  /**
+   * Table level error handlers.
+   * Should be chained wih the {@link #defaultErrorHandler}, which takes care of common cases.
+   */
+  private static Consumer<ErrorResponse> baseTableErrorHandler() {
+    return error -> {
+
+      switch (error.code()) {
+        // HttpStatus.SC_NOT_FOUND
+        case 404:
+          if (NoSuchNamespaceException.class.getSimpleName().equals(error.type())) {
+            throw new NoSuchNamespaceException("Resource not found: %s", error);
+          } else {
+            throw new NoSuchTableException("Resource not found: %s", error);
+          }
+        // HttpStatus.SC_CONFLICT
+        case 409:
+          throw new AlreadyExistsException("The table already exists: %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.
+   */
+  private static Consumer<ErrorResponse> baseNamespaceErrorHandler() {
+    return error -> {
+      switch (error.code()) {
+        // HttpStatus.SC_NOT_FOUND
+        case 404:
+          throw new NoSuchNamespaceException("Namespace not found: %s", error);
+        // HttpStatus.SC_CONFLICT
+        case 409:
+          throw new AlreadyExistsException("Namespace already exists: %s", error);
+        // HttpStatus.SC_UNPROCESSABLE_ENTITY
+        case 422:
+          throw new RESTException("Unable to process request due to bad property updates");
+      }
+    };
+  }
+
+  /**
+   * 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 -> {

Review comment:
       The handlers above use `error` instead of `errorResponse`.




-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] rdblue commented on a change in pull request #4202: CORE - Add main error handlers for use with REST catalog's HTTP client

Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #4202:
URL: https://github.com/apache/iceberg/pull/4202#discussion_r812527855



##########
File path: core/src/main/java/org/apache/iceberg/rest/ErrorHandlers.java
##########
@@ -0,0 +1,124 @@
+/*
+ * 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.util.function.Consumer;
+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() {
+  }
+
+  public static Consumer<ErrorResponse> namespaceErrorHandler() {
+    return baseNamespaceErrorHandler().andThen(defaultErrorHandler());
+  }
+
+  public static Consumer<ErrorResponse> tableErrorHandler() {
+    return baseTableErrorHandler().andThen(defaultErrorHandler());
+  }
+
+  /**
+   * Table level error handlers.
+   * Should be chained wih the {@link #defaultErrorHandler}, which takes care of common cases.
+   */
+  private static Consumer<ErrorResponse> baseTableErrorHandler() {
+    return error -> {
+
+      switch (error.code()) {
+        // HttpStatus.SC_NOT_FOUND
+        case 404:
+          if (NoSuchNamespaceException.class.getSimpleName().equals(error.type())) {
+            throw new NoSuchNamespaceException("Resource not found: %s", error);
+          } else {
+            throw new NoSuchTableException("Resource not found: %s", error);
+          }
+        // HttpStatus.SC_CONFLICT
+        case 409:
+          throw new AlreadyExistsException("The table already exists: %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.
+   */
+  private static Consumer<ErrorResponse> baseNamespaceErrorHandler() {
+    return error -> {
+      switch (error.code()) {
+        // HttpStatus.SC_NOT_FOUND
+        case 404:
+          throw new NoSuchNamespaceException("Namespace not found: %s", error);
+        // HttpStatus.SC_CONFLICT
+        case 409:
+          throw new AlreadyExistsException("Namespace already exists: %s", error);
+        // HttpStatus.SC_UNPROCESSABLE_ENTITY
+        case 422:
+          throw new RESTException("Unable to process request due to bad property updates");
+      }
+    };
+  }
+
+  /**
+   * 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()) {
+        // HttpStatus.SC_CLIENT_ERROR:
+        case 400:
+          throw new BadRequestException("Unable to process the request as it is somehow malformed: %s", errorResponse);

Review comment:
       This doesn't need to be fixed in this PR, but I'm using 400 for `NamespaceNotEmptyException`. There wasn't a defined error code for that in the spec. We should follow up and add it.
   
   For now, can we make this error message more generic, like `"Cannot process request: %s", error.message()`




-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] rdblue commented on a change in pull request #4202: CORE - Add main error handlers for use with REST catalog's HTTP client

Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #4202:
URL: https://github.com/apache/iceberg/pull/4202#discussion_r812526086



##########
File path: core/src/main/java/org/apache/iceberg/rest/ErrorHandlers.java
##########
@@ -0,0 +1,124 @@
+/*
+ * 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.util.function.Consumer;
+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() {
+  }
+
+  public static Consumer<ErrorResponse> namespaceErrorHandler() {
+    return baseNamespaceErrorHandler().andThen(defaultErrorHandler());
+  }
+
+  public static Consumer<ErrorResponse> tableErrorHandler() {
+    return baseTableErrorHandler().andThen(defaultErrorHandler());
+  }
+
+  /**
+   * Table level error handlers.
+   * Should be chained wih the {@link #defaultErrorHandler}, which takes care of common cases.
+   */
+  private static Consumer<ErrorResponse> baseTableErrorHandler() {
+    return error -> {
+
+      switch (error.code()) {
+        // HttpStatus.SC_NOT_FOUND
+        case 404:
+          if (NoSuchNamespaceException.class.getSimpleName().equals(error.type())) {
+            throw new NoSuchNamespaceException("Resource not found: %s", error);
+          } else {
+            throw new NoSuchTableException("Resource not found: %s", error);
+          }
+        // HttpStatus.SC_CONFLICT
+        case 409:
+          throw new AlreadyExistsException("The table already exists: %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.
+   */
+  private static Consumer<ErrorResponse> baseNamespaceErrorHandler() {
+    return error -> {
+      switch (error.code()) {
+        // HttpStatus.SC_NOT_FOUND
+        case 404:
+          throw new NoSuchNamespaceException("Namespace not found: %s", error);
+        // HttpStatus.SC_CONFLICT
+        case 409:
+          throw new AlreadyExistsException("Namespace already exists: %s", error);
+        // HttpStatus.SC_UNPROCESSABLE_ENTITY
+        case 422:
+          throw new RESTException("Unable to process request due to bad property updates");
+      }
+    };
+  }
+
+  /**
+   * 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()) {
+        // HttpStatus.SC_CLIENT_ERROR:
+        case 400:
+          throw new BadRequestException("Unable to process the request as it is somehow malformed: %s", errorResponse);
+        // HttpStatus.SC_UNAUTHORIZED
+        case 401:
+          throw new NotAuthorizedException("Not Authorized: %s", errorResponse);
+        // HttpStatus.SC_FORBIDDEN
+        case 403:
+          throw new ForbiddenException("Forbidden: %s", errorResponse);
+        // HttpStatus.SC_METHOD_NOT_ALLOWED
+        case 405:
+        // HttpStatus.SC_NOT_ACCEPTABLE

Review comment:
       At a minimum, I think 405 and 406 should break. Then move the `throw` from default to after the switch statement to catch both default and breaks.




-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] rdblue commented on a change in pull request #4202: CORE - Add main error handlers for use with REST catalog's HTTP client

Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #4202:
URL: https://github.com/apache/iceberg/pull/4202#discussion_r812525072



##########
File path: core/src/main/java/org/apache/iceberg/rest/ErrorHandlers.java
##########
@@ -0,0 +1,124 @@
+/*
+ * 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.util.function.Consumer;
+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() {
+  }
+
+  public static Consumer<ErrorResponse> namespaceErrorHandler() {
+    return baseNamespaceErrorHandler().andThen(defaultErrorHandler());
+  }
+
+  public static Consumer<ErrorResponse> tableErrorHandler() {
+    return baseTableErrorHandler().andThen(defaultErrorHandler());
+  }
+
+  /**
+   * Table level error handlers.
+   * Should be chained wih the {@link #defaultErrorHandler}, which takes care of common cases.
+   */
+  private static Consumer<ErrorResponse> baseTableErrorHandler() {
+    return error -> {
+
+      switch (error.code()) {
+        // HttpStatus.SC_NOT_FOUND
+        case 404:
+          if (NoSuchNamespaceException.class.getSimpleName().equals(error.type())) {
+            throw new NoSuchNamespaceException("Resource not found: %s", error);
+          } else {
+            throw new NoSuchTableException("Resource not found: %s", error);
+          }
+        // HttpStatus.SC_CONFLICT
+        case 409:
+          throw new AlreadyExistsException("The table already exists: %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.
+   */
+  private static Consumer<ErrorResponse> baseNamespaceErrorHandler() {
+    return error -> {
+      switch (error.code()) {
+        // HttpStatus.SC_NOT_FOUND
+        case 404:
+          throw new NoSuchNamespaceException("Namespace not found: %s", error);

Review comment:
       I think that it's likely that the error message from the error payload has this full human-readable error. In my implementation, I'm catching `NoSuchNamespaceException` and setting `message` from `e.getMessage()`. So I think this would result in a duplicate: "Namespace not found: Namespace not found: ns".
   
   Instead, how about this:
   
   ```java
     throw new NoSuchNamespaceException(error.message());
   ```




-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] rdblue commented on a change in pull request #4202: CORE - Add main error handlers for use with REST catalog's HTTP client

Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #4202:
URL: https://github.com/apache/iceberg/pull/4202#discussion_r812525561



##########
File path: core/src/main/java/org/apache/iceberg/rest/ErrorHandlers.java
##########
@@ -0,0 +1,124 @@
+/*
+ * 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.util.function.Consumer;
+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() {
+  }
+
+  public static Consumer<ErrorResponse> namespaceErrorHandler() {
+    return baseNamespaceErrorHandler().andThen(defaultErrorHandler());
+  }
+
+  public static Consumer<ErrorResponse> tableErrorHandler() {
+    return baseTableErrorHandler().andThen(defaultErrorHandler());
+  }
+
+  /**
+   * Table level error handlers.
+   * Should be chained wih the {@link #defaultErrorHandler}, which takes care of common cases.
+   */
+  private static Consumer<ErrorResponse> baseTableErrorHandler() {
+    return error -> {
+
+      switch (error.code()) {
+        // HttpStatus.SC_NOT_FOUND
+        case 404:
+          if (NoSuchNamespaceException.class.getSimpleName().equals(error.type())) {
+            throw new NoSuchNamespaceException("Resource not found: %s", error);
+          } else {
+            throw new NoSuchTableException("Resource not found: %s", error);
+          }
+        // HttpStatus.SC_CONFLICT
+        case 409:
+          throw new AlreadyExistsException("The table already exists: %s", error);

Review comment:
       Where did we land on checking `type` to see if this is `AlreadyExistsException` or `CommitConflictException`? Are we going to use a special handler for table commits?




-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] rdblue commented on a change in pull request #4202: CORE - Add main error handlers for use with REST catalog's HTTP client

Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #4202:
URL: https://github.com/apache/iceberg/pull/4202#discussion_r812526617



##########
File path: core/src/main/java/org/apache/iceberg/rest/ErrorHandlers.java
##########
@@ -0,0 +1,124 @@
+/*
+ * 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.util.function.Consumer;
+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() {
+  }
+
+  public static Consumer<ErrorResponse> namespaceErrorHandler() {
+    return baseNamespaceErrorHandler().andThen(defaultErrorHandler());
+  }
+
+  public static Consumer<ErrorResponse> tableErrorHandler() {
+    return baseTableErrorHandler().andThen(defaultErrorHandler());
+  }
+
+  /**
+   * Table level error handlers.
+   * Should be chained wih the {@link #defaultErrorHandler}, which takes care of common cases.
+   */
+  private static Consumer<ErrorResponse> baseTableErrorHandler() {
+    return error -> {
+
+      switch (error.code()) {
+        // HttpStatus.SC_NOT_FOUND
+        case 404:
+          if (NoSuchNamespaceException.class.getSimpleName().equals(error.type())) {
+            throw new NoSuchNamespaceException("Resource not found: %s", error);
+          } else {
+            throw new NoSuchTableException("Resource not found: %s", error);
+          }
+        // HttpStatus.SC_CONFLICT
+        case 409:
+          throw new AlreadyExistsException("The table already exists: %s", error);

Review comment:
       Nit: "Table already exists" rather than "The table ..."




-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] rdblue commented on a change in pull request #4202: CORE - Add main error handlers for use with REST catalog's HTTP client

Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #4202:
URL: https://github.com/apache/iceberg/pull/4202#discussion_r812524066



##########
File path: core/src/main/java/org/apache/iceberg/rest/ErrorHandlers.java
##########
@@ -0,0 +1,124 @@
+/*
+ * 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.util.function.Consumer;
+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() {
+  }
+
+  public static Consumer<ErrorResponse> namespaceErrorHandler() {
+    return baseNamespaceErrorHandler().andThen(defaultErrorHandler());
+  }
+
+  public static Consumer<ErrorResponse> tableErrorHandler() {
+    return baseTableErrorHandler().andThen(defaultErrorHandler());
+  }
+
+  /**
+   * Table level error handlers.
+   * Should be chained wih the {@link #defaultErrorHandler}, which takes care of common cases.
+   */
+  private static Consumer<ErrorResponse> baseTableErrorHandler() {
+    return error -> {
+
+      switch (error.code()) {
+        // HttpStatus.SC_NOT_FOUND
+        case 404:
+          if (NoSuchNamespaceException.class.getSimpleName().equals(error.type())) {
+            throw new NoSuchNamespaceException("Resource not found: %s", error);
+          } else {
+            throw new NoSuchTableException("Resource not found: %s", error);
+          }
+        // HttpStatus.SC_CONFLICT
+        case 409:
+          throw new AlreadyExistsException("The table already exists: %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.
+   */
+  private static Consumer<ErrorResponse> baseNamespaceErrorHandler() {
+    return error -> {
+      switch (error.code()) {
+        // HttpStatus.SC_NOT_FOUND

Review comment:
       Do we need these comments? I appreciate having a bit more explanation of the code, but that should be more like `case 404: // not found` instead of referring to a library that isn't needed here.




-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] kbendick commented on a change in pull request #4202: CORE - Add main error handlers for use with REST catalog's HTTP client

Posted by GitBox <gi...@apache.org>.
kbendick commented on a change in pull request #4202:
URL: https://github.com/apache/iceberg/pull/4202#discussion_r812536567



##########
File path: core/src/main/java/org/apache/iceberg/rest/ErrorHandlers.java
##########
@@ -0,0 +1,124 @@
+/*
+ * 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.util.function.Consumer;
+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() {
+  }
+
+  public static Consumer<ErrorResponse> namespaceErrorHandler() {
+    return baseNamespaceErrorHandler().andThen(defaultErrorHandler());
+  }
+
+  public static Consumer<ErrorResponse> tableErrorHandler() {
+    return baseTableErrorHandler().andThen(defaultErrorHandler());
+  }
+
+  /**
+   * Table level error handlers.
+   * Should be chained wih the {@link #defaultErrorHandler}, which takes care of common cases.
+   */
+  private static Consumer<ErrorResponse> baseTableErrorHandler() {
+    return error -> {
+
+      switch (error.code()) {
+        // HttpStatus.SC_NOT_FOUND
+        case 404:
+          if (NoSuchNamespaceException.class.getSimpleName().equals(error.type())) {
+            throw new NoSuchNamespaceException("Resource not found: %s", error);
+          } else {
+            throw new NoSuchTableException("Resource not found: %s", error);
+          }
+        // HttpStatus.SC_CONFLICT
+        case 409:
+          throw new AlreadyExistsException("The table already exists: %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.
+   */
+  private static Consumer<ErrorResponse> baseNamespaceErrorHandler() {
+    return error -> {
+      switch (error.code()) {
+        // HttpStatus.SC_NOT_FOUND
+        case 404:
+          throw new NoSuchNamespaceException("Namespace not found: %s", error);
+        // HttpStatus.SC_CONFLICT
+        case 409:
+          throw new AlreadyExistsException("Namespace already exists: %s", error);
+        // HttpStatus.SC_UNPROCESSABLE_ENTITY
+        case 422:
+          throw new RESTException("Unable to process request due to bad property updates");
+      }
+    };
+  }
+
+  /**
+   * 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()) {
+        // HttpStatus.SC_CLIENT_ERROR:
+        case 400:
+          throw new BadRequestException("Unable to process the request as it is somehow malformed: %s", errorResponse);

Review comment:
       I'll make a note of this. I changed it to `Malformed request: %s` as that's what 400 is supposed to represent (the payload is syntactically incorrect).
   
   That's the response that some frameworks use, but I can update it if we'd like.

##########
File path: core/src/main/java/org/apache/iceberg/rest/ErrorHandlers.java
##########
@@ -0,0 +1,112 @@
+/*
+ * 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.util.function.Consumer;
+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() {
+  }
+
+  public static Consumer<ErrorResponse> namespaceErrorHandler() {
+    return baseNamespaceErrorHandler().andThen(defaultErrorHandler());
+  }
+
+  public static Consumer<ErrorResponse> tableErrorHandler() {
+    return baseTableErrorHandler().andThen(defaultErrorHandler());
+  }
+
+  /**
+   * Table level error handlers.
+   * Should be chained wih the {@link #defaultErrorHandler}, which takes care of common cases.
+   */
+  private static Consumer<ErrorResponse> baseTableErrorHandler() {
+    return error -> {
+      switch (error.code()) {
+        case 404:
+          if (NoSuchNamespaceException.class.getSimpleName().equals(error.type())) {
+            throw new NoSuchNamespaceException("Namespace not found: %s", error.message());
+          } else {
+            throw new NoSuchTableException("Table not found: %s", error.message());

Review comment:
       Reverted.




-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] kbendick commented on a change in pull request #4202: CORE - Add main error handlers for use with REST catalog's HTTP client

Posted by GitBox <gi...@apache.org>.
kbendick commented on a change in pull request #4202:
URL: https://github.com/apache/iceberg/pull/4202#discussion_r812501738



##########
File path: core/src/main/java/org/apache/iceberg/rest/responses/ErrorResponse.java
##########
@@ -0,0 +1,105 @@
+/*

Review comment:
       This file is from https://github.com/apache/iceberg/pull/4201, and was only copied over to get the `ErrorHandlers` to compile.
   
   It can be reviewed as part of https://github.com/apache/iceberg/pull/4201 and then it should be removed from here before committing (after #4201 is merged).




-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] kbendick edited a comment on pull request #4202: CORE - Add main error handlers for use with REST catalog's HTTP client

Posted by GitBox <gi...@apache.org>.
kbendick edited a comment on pull request #4202:
URL: https://github.com/apache/iceberg/pull/4202#issuecomment-1048450953


   Updated some of the exceptions that are annotated with `@FormatMessage` to include a constructor with just `String message` to pass checkstyle.
   
   I figure this is ok, as we already allow for a format string with arguments (and no throwable). It's either this, or use a format string of `"%s"` and then pass `error.message()` to it. Seems kind of silly to do that, but if we don't want to expose a constructor with a signature of `String` only for the exceptions, we can.


-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] rdblue merged pull request #4202: CORE - Add main error handlers for use with REST catalog's HTTP client

Posted by GitBox <gi...@apache.org>.
rdblue merged pull request #4202:
URL: https://github.com/apache/iceberg/pull/4202


   


-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] kbendick closed pull request #4202: CORE - Add main error handlers for use with REST catalog's HTTP client

Posted by GitBox <gi...@apache.org>.
kbendick closed pull request #4202:
URL: https://github.com/apache/iceberg/pull/4202


   


-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] kbendick commented on a change in pull request #4202: CORE - Add main error handlers for use with REST catalog's HTTP client

Posted by GitBox <gi...@apache.org>.
kbendick commented on a change in pull request #4202:
URL: https://github.com/apache/iceberg/pull/4202#discussion_r812501738



##########
File path: core/src/main/java/org/apache/iceberg/rest/responses/ErrorResponse.java
##########
@@ -0,0 +1,105 @@
+/*

Review comment:
       This file is from https://github.com/apache/iceberg/pull/4201, and was only copied over to get the `ErrorHandlers` to compile.
   
   It can be reviewed as part of https://github.com/apache/iceberg/pull/4201.




-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] kbendick commented on a change in pull request #4202: CORE - Add main error handlers for use with REST catalog's HTTP client

Posted by GitBox <gi...@apache.org>.
kbendick commented on a change in pull request #4202:
URL: https://github.com/apache/iceberg/pull/4202#discussion_r813131484



##########
File path: api/src/main/java/org/apache/iceberg/exceptions/AlreadyExistsException.java
##########
@@ -25,6 +25,10 @@
  * Exception raised when attempting to create a table that already exists.
  */
 public class AlreadyExistsException extends RuntimeException {
+  public AlreadyExistsException(String message) {
+    super(message);

Review comment:
       It complains about using an expression, but we can just use `AlreadyExistsExcception('%s', error.message())`. I'll update to that instead.




-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] rdblue commented on a change in pull request #4202: CORE - Add main error handlers for use with REST catalog's HTTP client

Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #4202:
URL: https://github.com/apache/iceberg/pull/4202#discussion_r812523263



##########
File path: api/src/main/java/org/apache/iceberg/exceptions/RESTException.java
##########
@@ -0,0 +1,34 @@
+/*
+ * 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.exceptions;
+
+import com.google.errorprone.annotations.FormatMethod;
+
+public class RESTException extends RuntimeException {

Review comment:
       Javadoc: base class for REST client exceptions?




-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] kbendick commented on a change in pull request #4202: CORE - Add main error handlers for use with REST catalog's HTTP client

Posted by GitBox <gi...@apache.org>.
kbendick commented on a change in pull request #4202:
URL: https://github.com/apache/iceberg/pull/4202#discussion_r812530742



##########
File path: core/src/main/java/org/apache/iceberg/rest/ErrorHandlers.java
##########
@@ -0,0 +1,124 @@
+/*
+ * 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.util.function.Consumer;
+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() {
+  }
+
+  public static Consumer<ErrorResponse> namespaceErrorHandler() {
+    return baseNamespaceErrorHandler().andThen(defaultErrorHandler());
+  }
+
+  public static Consumer<ErrorResponse> tableErrorHandler() {
+    return baseTableErrorHandler().andThen(defaultErrorHandler());
+  }
+
+  /**
+   * Table level error handlers.
+   * Should be chained wih the {@link #defaultErrorHandler}, which takes care of common cases.
+   */
+  private static Consumer<ErrorResponse> baseTableErrorHandler() {
+    return error -> {
+
+      switch (error.code()) {
+        // HttpStatus.SC_NOT_FOUND
+        case 404:
+          if (NoSuchNamespaceException.class.getSimpleName().equals(error.type())) {
+            throw new NoSuchNamespaceException("Resource not found: %s", error);
+          } else {
+            throw new NoSuchTableException("Resource not found: %s", error);
+          }
+        // HttpStatus.SC_CONFLICT
+        case 409:
+          throw new AlreadyExistsException("The table already exists: %s", error);

Review comment:
       As far as `type` goes, none of them are formalized in the spec. I can send out a proposal on that if you'd like to sync on it?

##########
File path: core/src/main/java/org/apache/iceberg/rest/ErrorHandlers.java
##########
@@ -0,0 +1,124 @@
+/*
+ * 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.util.function.Consumer;
+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() {
+  }
+
+  public static Consumer<ErrorResponse> namespaceErrorHandler() {
+    return baseNamespaceErrorHandler().andThen(defaultErrorHandler());
+  }
+
+  public static Consumer<ErrorResponse> tableErrorHandler() {
+    return baseTableErrorHandler().andThen(defaultErrorHandler());
+  }
+
+  /**
+   * Table level error handlers.
+   * Should be chained wih the {@link #defaultErrorHandler}, which takes care of common cases.
+   */
+  private static Consumer<ErrorResponse> baseTableErrorHandler() {
+    return error -> {
+
+      switch (error.code()) {
+        // HttpStatus.SC_NOT_FOUND
+        case 404:
+          if (NoSuchNamespaceException.class.getSimpleName().equals(error.type())) {
+            throw new NoSuchNamespaceException("Resource not found: %s", error);
+          } else {
+            throw new NoSuchTableException("Resource not found: %s", error);
+          }
+        // HttpStatus.SC_CONFLICT
+        case 409:
+          throw new AlreadyExistsException("The table already exists: %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.
+   */
+  private static Consumer<ErrorResponse> baseNamespaceErrorHandler() {
+    return error -> {
+      switch (error.code()) {
+        // HttpStatus.SC_NOT_FOUND
+        case 404:
+          throw new NoSuchNamespaceException("Namespace not found: %s", error);
+        // HttpStatus.SC_CONFLICT
+        case 409:
+          throw new AlreadyExistsException("Namespace already exists: %s", error);
+        // HttpStatus.SC_UNPROCESSABLE_ENTITY
+        case 422:
+          throw new RESTException("Unable to process request due to bad property updates");
+      }
+    };
+  }
+
+  /**
+   * 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()) {
+        // HttpStatus.SC_CLIENT_ERROR:
+        case 400:
+          throw new BadRequestException("Unable to process the request as it is somehow malformed: %s", errorResponse);
+        // HttpStatus.SC_UNAUTHORIZED
+        case 401:
+          throw new NotAuthorizedException("Not Authorized: %s", errorResponse);
+        // HttpStatus.SC_FORBIDDEN
+        case 403:
+          throw new ForbiddenException("Forbidden: %s", errorResponse);
+        // HttpStatus.SC_METHOD_NOT_ALLOWED
+        case 405:
+        // HttpStatus.SC_NOT_ACCEPTABLE

Review comment:
       Changed 405 and 406 to break.




-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] kbendick commented on a change in pull request #4202: CORE - Add main error handlers for use with REST catalog's HTTP client

Posted by GitBox <gi...@apache.org>.
kbendick commented on a change in pull request #4202:
URL: https://github.com/apache/iceberg/pull/4202#discussion_r812530638



##########
File path: core/src/main/java/org/apache/iceberg/rest/ErrorHandlers.java
##########
@@ -0,0 +1,124 @@
+/*
+ * 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.util.function.Consumer;
+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() {
+  }
+
+  public static Consumer<ErrorResponse> namespaceErrorHandler() {
+    return baseNamespaceErrorHandler().andThen(defaultErrorHandler());
+  }
+
+  public static Consumer<ErrorResponse> tableErrorHandler() {
+    return baseTableErrorHandler().andThen(defaultErrorHandler());
+  }
+
+  /**
+   * Table level error handlers.
+   * Should be chained wih the {@link #defaultErrorHandler}, which takes care of common cases.
+   */
+  private static Consumer<ErrorResponse> baseTableErrorHandler() {
+    return error -> {
+
+      switch (error.code()) {
+        // HttpStatus.SC_NOT_FOUND
+        case 404:
+          if (NoSuchNamespaceException.class.getSimpleName().equals(error.type())) {
+            throw new NoSuchNamespaceException("Resource not found: %s", error);
+          } else {
+            throw new NoSuchTableException("Resource not found: %s", error);
+          }
+        // HttpStatus.SC_CONFLICT
+        case 409:
+          throw new AlreadyExistsException("The table already exists: %s", error);

Review comment:
       Yeah we'll likely need separate handlers for a few routes, but for now this covers the most common cases.




-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] kbendick commented on pull request #4202: CORE - Add main error handlers for use with REST catalog's HTTP client

Posted by GitBox <gi...@apache.org>.
kbendick commented on pull request #4202:
URL: https://github.com/apache/iceberg/pull/4202#issuecomment-1049061212


   This hit the flakey Flink test. I'm gonna close and reopen to restart the tests.


-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] kbendick commented on a change in pull request #4202: CORE - Add main error handlers for use with REST catalog's HTTP client

Posted by GitBox <gi...@apache.org>.
kbendick commented on a change in pull request #4202:
URL: https://github.com/apache/iceberg/pull/4202#discussion_r812528655



##########
File path: core/src/main/java/org/apache/iceberg/rest/ErrorHandlers.java
##########
@@ -0,0 +1,124 @@
+/*
+ * 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.util.function.Consumer;
+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() {
+  }
+
+  public static Consumer<ErrorResponse> namespaceErrorHandler() {
+    return baseNamespaceErrorHandler().andThen(defaultErrorHandler());
+  }
+
+  public static Consumer<ErrorResponse> tableErrorHandler() {
+    return baseTableErrorHandler().andThen(defaultErrorHandler());
+  }
+
+  /**
+   * Table level error handlers.
+   * Should be chained wih the {@link #defaultErrorHandler}, which takes care of common cases.
+   */
+  private static Consumer<ErrorResponse> baseTableErrorHandler() {
+    return error -> {
+
+      switch (error.code()) {
+        // HttpStatus.SC_NOT_FOUND
+        case 404:
+          if (NoSuchNamespaceException.class.getSimpleName().equals(error.type())) {
+            throw new NoSuchNamespaceException("Resource not found: %s", error);
+          } else {
+            throw new NoSuchTableException("Resource not found: %s", error);
+          }
+        // HttpStatus.SC_CONFLICT
+        case 409:
+          throw new AlreadyExistsException("The table already exists: %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.
+   */
+  private static Consumer<ErrorResponse> baseNamespaceErrorHandler() {
+    return error -> {
+      switch (error.code()) {
+        // HttpStatus.SC_NOT_FOUND

Review comment:
       I'll just remove them entirely as they can be looked up online and then the exception type also provides the context.

##########
File path: core/src/main/java/org/apache/iceberg/rest/ErrorHandlers.java
##########
@@ -0,0 +1,124 @@
+/*
+ * 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.util.function.Consumer;
+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() {
+  }
+
+  public static Consumer<ErrorResponse> namespaceErrorHandler() {
+    return baseNamespaceErrorHandler().andThen(defaultErrorHandler());
+  }
+
+  public static Consumer<ErrorResponse> tableErrorHandler() {
+    return baseTableErrorHandler().andThen(defaultErrorHandler());
+  }
+
+  /**
+   * Table level error handlers.
+   * Should be chained wih the {@link #defaultErrorHandler}, which takes care of common cases.
+   */
+  private static Consumer<ErrorResponse> baseTableErrorHandler() {
+    return error -> {
+
+      switch (error.code()) {
+        // HttpStatus.SC_NOT_FOUND
+        case 404:
+          if (NoSuchNamespaceException.class.getSimpleName().equals(error.type())) {
+            throw new NoSuchNamespaceException("Resource not found: %s", error);
+          } else {
+            throw new NoSuchTableException("Resource not found: %s", error);
+          }
+        // HttpStatus.SC_CONFLICT
+        case 409:
+          throw new AlreadyExistsException("The table already exists: %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.
+   */
+  private static Consumer<ErrorResponse> baseNamespaceErrorHandler() {
+    return error -> {
+      switch (error.code()) {
+        // HttpStatus.SC_NOT_FOUND
+        case 404:
+          throw new NoSuchNamespaceException("Namespace not found: %s", error);
+        // HttpStatus.SC_CONFLICT
+        case 409:
+          throw new AlreadyExistsException("Namespace already exists: %s", error);
+        // HttpStatus.SC_UNPROCESSABLE_ENTITY
+        case 422:
+          throw new RESTException("Unable to process request due to bad property updates");

Review comment:
       Updated!




-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] rdblue commented on a change in pull request #4202: CORE - Add main error handlers for use with REST catalog's HTTP client

Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #4202:
URL: https://github.com/apache/iceberg/pull/4202#discussion_r813037342



##########
File path: api/src/main/java/org/apache/iceberg/exceptions/AlreadyExistsException.java
##########
@@ -25,6 +25,10 @@
  * Exception raised when attempting to create a table that already exists.
  */
 public class AlreadyExistsException extends RuntimeException {
+  public AlreadyExistsException(String message) {
+    super(message);

Review comment:
       Is this needed? I think `args` in the next constructor can simply be empty, right?




-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] kbendick commented on pull request #4202: CORE - Add main error handlers for use with REST catalog's HTTP client

Posted by GitBox <gi...@apache.org>.
kbendick commented on pull request #4202:
URL: https://github.com/apache/iceberg/pull/4202#issuecomment-1048450953


   Updated some of the exceptions that are annotated with `@FormatMessage` to include a constructor with just `String message` to pass checkstyle.
   
   I figure this is ok, as we already allow for a format string with arguments. It's either this, or use a format string of `"%s"` and then pass `error.message()` to it. Seems kind of silly to do that, though if we don't want to expose a constructor with a signature of `String` only for the exceptions, we can.


-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] rdblue commented on a change in pull request #4202: CORE - Add main error handlers for use with REST catalog's HTTP client

Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #4202:
URL: https://github.com/apache/iceberg/pull/4202#discussion_r812535463



##########
File path: core/src/main/java/org/apache/iceberg/rest/ErrorHandlers.java
##########
@@ -0,0 +1,112 @@
+/*
+ * 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.util.function.Consumer;
+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() {
+  }
+
+  public static Consumer<ErrorResponse> namespaceErrorHandler() {
+    return baseNamespaceErrorHandler().andThen(defaultErrorHandler());
+  }
+
+  public static Consumer<ErrorResponse> tableErrorHandler() {
+    return baseTableErrorHandler().andThen(defaultErrorHandler());
+  }
+
+  /**
+   * Table level error handlers.
+   * Should be chained wih the {@link #defaultErrorHandler}, which takes care of common cases.
+   */
+  private static Consumer<ErrorResponse> baseTableErrorHandler() {
+    return error -> {
+      switch (error.code()) {
+        case 404:
+          if (NoSuchNamespaceException.class.getSimpleName().equals(error.type())) {
+            throw new NoSuchNamespaceException("Namespace not found: %s", error.message());
+          } else {
+            throw new NoSuchTableException("Table not found: %s", error.message());

Review comment:
       Oh, right. We were removing the prefixes from these. My bad.




-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] rdblue commented on a change in pull request #4202: CORE - Add main error handlers for use with REST catalog's HTTP client

Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #4202:
URL: https://github.com/apache/iceberg/pull/4202#discussion_r812526767



##########
File path: core/src/main/java/org/apache/iceberg/rest/ErrorHandlers.java
##########
@@ -0,0 +1,124 @@
+/*
+ * 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.util.function.Consumer;
+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() {
+  }
+
+  public static Consumer<ErrorResponse> namespaceErrorHandler() {
+    return baseNamespaceErrorHandler().andThen(defaultErrorHandler());
+  }
+
+  public static Consumer<ErrorResponse> tableErrorHandler() {
+    return baseTableErrorHandler().andThen(defaultErrorHandler());
+  }
+
+  /**
+   * Table level error handlers.
+   * Should be chained wih the {@link #defaultErrorHandler}, which takes care of common cases.
+   */
+  private static Consumer<ErrorResponse> baseTableErrorHandler() {
+    return error -> {
+
+      switch (error.code()) {
+        // HttpStatus.SC_NOT_FOUND
+        case 404:
+          if (NoSuchNamespaceException.class.getSimpleName().equals(error.type())) {
+            throw new NoSuchNamespaceException("Resource not found: %s", error);

Review comment:
       "Namespace not found: %s"

##########
File path: core/src/main/java/org/apache/iceberg/rest/ErrorHandlers.java
##########
@@ -0,0 +1,124 @@
+/*
+ * 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.util.function.Consumer;
+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() {
+  }
+
+  public static Consumer<ErrorResponse> namespaceErrorHandler() {
+    return baseNamespaceErrorHandler().andThen(defaultErrorHandler());
+  }
+
+  public static Consumer<ErrorResponse> tableErrorHandler() {
+    return baseTableErrorHandler().andThen(defaultErrorHandler());
+  }
+
+  /**
+   * Table level error handlers.
+   * Should be chained wih the {@link #defaultErrorHandler}, which takes care of common cases.
+   */
+  private static Consumer<ErrorResponse> baseTableErrorHandler() {
+    return error -> {
+
+      switch (error.code()) {
+        // HttpStatus.SC_NOT_FOUND
+        case 404:
+          if (NoSuchNamespaceException.class.getSimpleName().equals(error.type())) {
+            throw new NoSuchNamespaceException("Resource not found: %s", error);
+          } else {
+            throw new NoSuchTableException("Resource not found: %s", error);

Review comment:
       "Table not found: %s"




-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] rdblue commented on pull request #4202: CORE - Add main error handlers for use with REST catalog's HTTP client

Posted by GitBox <gi...@apache.org>.
rdblue commented on pull request #4202:
URL: https://github.com/apache/iceberg/pull/4202#issuecomment-1049117958


   Looks great. Thanks, @kbendick!


-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] kbendick commented on a change in pull request #4202: CORE - Add main error handlers for use with REST catalog's HTTP client

Posted by GitBox <gi...@apache.org>.
kbendick commented on a change in pull request #4202:
URL: https://github.com/apache/iceberg/pull/4202#discussion_r812531746



##########
File path: core/src/main/java/org/apache/iceberg/rest/ErrorHandlers.java
##########
@@ -0,0 +1,124 @@
+/*
+ * 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.util.function.Consumer;
+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() {
+  }
+
+  public static Consumer<ErrorResponse> namespaceErrorHandler() {
+    return baseNamespaceErrorHandler().andThen(defaultErrorHandler());
+  }
+
+  public static Consumer<ErrorResponse> tableErrorHandler() {
+    return baseTableErrorHandler().andThen(defaultErrorHandler());
+  }
+
+  /**
+   * Table level error handlers.
+   * Should be chained wih the {@link #defaultErrorHandler}, which takes care of common cases.
+   */
+  private static Consumer<ErrorResponse> baseTableErrorHandler() {
+    return error -> {
+
+      switch (error.code()) {
+        // HttpStatus.SC_NOT_FOUND
+        case 404:
+          if (NoSuchNamespaceException.class.getSimpleName().equals(error.type())) {
+            throw new NoSuchNamespaceException("Resource not found: %s", error);
+          } else {
+            throw new NoSuchTableException("Resource not found: %s", error);
+          }
+        // HttpStatus.SC_CONFLICT
+        case 409:
+          throw new AlreadyExistsException("The table already exists: %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.
+   */
+  private static Consumer<ErrorResponse> baseNamespaceErrorHandler() {
+    return error -> {
+      switch (error.code()) {
+        // HttpStatus.SC_NOT_FOUND
+        case 404:
+          throw new NoSuchNamespaceException("Namespace not found: %s", error);
+        // HttpStatus.SC_CONFLICT
+        case 409:
+          throw new AlreadyExistsException("Namespace already exists: %s", error);
+        // HttpStatus.SC_UNPROCESSABLE_ENTITY
+        case 422:
+          throw new RESTException("Unable to process request due to bad property updates");
+      }
+    };
+  }
+
+  /**
+   * 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 -> {

Review comment:
       Updated!




-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] kbendick commented on a change in pull request #4202: CORE - Add main error handlers for use with REST catalog's HTTP client

Posted by GitBox <gi...@apache.org>.
kbendick commented on a change in pull request #4202:
URL: https://github.com/apache/iceberg/pull/4202#discussion_r812530879



##########
File path: core/src/main/java/org/apache/iceberg/rest/ErrorHandlers.java
##########
@@ -0,0 +1,124 @@
+/*
+ * 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.util.function.Consumer;
+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() {
+  }
+
+  public static Consumer<ErrorResponse> namespaceErrorHandler() {
+    return baseNamespaceErrorHandler().andThen(defaultErrorHandler());
+  }
+
+  public static Consumer<ErrorResponse> tableErrorHandler() {
+    return baseTableErrorHandler().andThen(defaultErrorHandler());
+  }
+
+  /**
+   * Table level error handlers.
+   * Should be chained wih the {@link #defaultErrorHandler}, which takes care of common cases.
+   */
+  private static Consumer<ErrorResponse> baseTableErrorHandler() {
+    return error -> {
+
+      switch (error.code()) {
+        // HttpStatus.SC_NOT_FOUND
+        case 404:
+          if (NoSuchNamespaceException.class.getSimpleName().equals(error.type())) {
+            throw new NoSuchNamespaceException("Resource not found: %s", error);
+          } else {
+            throw new NoSuchTableException("Resource not found: %s", error);
+          }
+        // HttpStatus.SC_CONFLICT
+        case 409:
+          throw new AlreadyExistsException("The table already exists: %s", error);

Review comment:
       Oh woops I already updated this to just be `error.message()`. I'll revert it.




-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] kbendick commented on a change in pull request #4202: CORE - Add main error handlers for use with REST catalog's HTTP client

Posted by GitBox <gi...@apache.org>.
kbendick commented on a change in pull request #4202:
URL: https://github.com/apache/iceberg/pull/4202#discussion_r812528655



##########
File path: core/src/main/java/org/apache/iceberg/rest/ErrorHandlers.java
##########
@@ -0,0 +1,124 @@
+/*
+ * 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.util.function.Consumer;
+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() {
+  }
+
+  public static Consumer<ErrorResponse> namespaceErrorHandler() {
+    return baseNamespaceErrorHandler().andThen(defaultErrorHandler());
+  }
+
+  public static Consumer<ErrorResponse> tableErrorHandler() {
+    return baseTableErrorHandler().andThen(defaultErrorHandler());
+  }
+
+  /**
+   * Table level error handlers.
+   * Should be chained wih the {@link #defaultErrorHandler}, which takes care of common cases.
+   */
+  private static Consumer<ErrorResponse> baseTableErrorHandler() {
+    return error -> {
+
+      switch (error.code()) {
+        // HttpStatus.SC_NOT_FOUND
+        case 404:
+          if (NoSuchNamespaceException.class.getSimpleName().equals(error.type())) {
+            throw new NoSuchNamespaceException("Resource not found: %s", error);
+          } else {
+            throw new NoSuchTableException("Resource not found: %s", error);
+          }
+        // HttpStatus.SC_CONFLICT
+        case 409:
+          throw new AlreadyExistsException("The table already exists: %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.
+   */
+  private static Consumer<ErrorResponse> baseNamespaceErrorHandler() {
+    return error -> {
+      switch (error.code()) {
+        // HttpStatus.SC_NOT_FOUND

Review comment:
       I'll just remove them entirely as they can be looked up online.




-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] rdblue commented on a change in pull request #4202: CORE - Add main error handlers for use with REST catalog's HTTP client

Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #4202:
URL: https://github.com/apache/iceberg/pull/4202#discussion_r812525186



##########
File path: core/src/main/java/org/apache/iceberg/rest/ErrorHandlers.java
##########
@@ -0,0 +1,124 @@
+/*
+ * 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.util.function.Consumer;
+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() {
+  }
+
+  public static Consumer<ErrorResponse> namespaceErrorHandler() {
+    return baseNamespaceErrorHandler().andThen(defaultErrorHandler());
+  }
+
+  public static Consumer<ErrorResponse> tableErrorHandler() {
+    return baseTableErrorHandler().andThen(defaultErrorHandler());
+  }
+
+  /**
+   * Table level error handlers.
+   * Should be chained wih the {@link #defaultErrorHandler}, which takes care of common cases.
+   */
+  private static Consumer<ErrorResponse> baseTableErrorHandler() {
+    return error -> {
+
+      switch (error.code()) {
+        // HttpStatus.SC_NOT_FOUND
+        case 404:
+          if (NoSuchNamespaceException.class.getSimpleName().equals(error.type())) {
+            throw new NoSuchNamespaceException("Resource not found: %s", error);
+          } else {
+            throw new NoSuchTableException("Resource not found: %s", error);
+          }
+        // HttpStatus.SC_CONFLICT
+        case 409:
+          throw new AlreadyExistsException("The table already exists: %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.
+   */
+  private static Consumer<ErrorResponse> baseNamespaceErrorHandler() {
+    return error -> {
+      switch (error.code()) {
+        // HttpStatus.SC_NOT_FOUND
+        case 404:
+          throw new NoSuchNamespaceException("Namespace not found: %s", error);
+        // HttpStatus.SC_CONFLICT
+        case 409:
+          throw new AlreadyExistsException("Namespace already exists: %s", error);
+        // HttpStatus.SC_UNPROCESSABLE_ENTITY
+        case 422:
+          throw new RESTException("Unable to process request due to bad property updates");

Review comment:
       Shoudn't this use the error message?




-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] rdblue commented on a change in pull request #4202: CORE - Add main error handlers for use with REST catalog's HTTP client

Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #4202:
URL: https://github.com/apache/iceberg/pull/4202#discussion_r812526483



##########
File path: core/src/main/java/org/apache/iceberg/rest/ErrorHandlers.java
##########
@@ -0,0 +1,124 @@
+/*
+ * 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.util.function.Consumer;
+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() {
+  }
+
+  public static Consumer<ErrorResponse> namespaceErrorHandler() {
+    return baseNamespaceErrorHandler().andThen(defaultErrorHandler());
+  }
+
+  public static Consumer<ErrorResponse> tableErrorHandler() {
+    return baseTableErrorHandler().andThen(defaultErrorHandler());
+  }
+
+  /**
+   * Table level error handlers.
+   * Should be chained wih the {@link #defaultErrorHandler}, which takes care of common cases.
+   */
+  private static Consumer<ErrorResponse> baseTableErrorHandler() {
+    return error -> {
+
+      switch (error.code()) {
+        // HttpStatus.SC_NOT_FOUND
+        case 404:
+          if (NoSuchNamespaceException.class.getSimpleName().equals(error.type())) {
+            throw new NoSuchNamespaceException("Resource not found: %s", error);
+          } else {
+            throw new NoSuchTableException("Resource not found: %s", error);
+          }
+        // HttpStatus.SC_CONFLICT
+        case 409:
+          throw new AlreadyExistsException("The table already exists: %s", error);

Review comment:
       Yeah, I think we need a separate handler for commits that handles 409 and 500 differently. For now, this is good.




-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] kbendick commented on a change in pull request #4202: CORE - Add main error handlers for use with REST catalog's HTTP client

Posted by GitBox <gi...@apache.org>.
kbendick commented on a change in pull request #4202:
URL: https://github.com/apache/iceberg/pull/4202#discussion_r812536567



##########
File path: core/src/main/java/org/apache/iceberg/rest/ErrorHandlers.java
##########
@@ -0,0 +1,124 @@
+/*
+ * 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.util.function.Consumer;
+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() {
+  }
+
+  public static Consumer<ErrorResponse> namespaceErrorHandler() {
+    return baseNamespaceErrorHandler().andThen(defaultErrorHandler());
+  }
+
+  public static Consumer<ErrorResponse> tableErrorHandler() {
+    return baseTableErrorHandler().andThen(defaultErrorHandler());
+  }
+
+  /**
+   * Table level error handlers.
+   * Should be chained wih the {@link #defaultErrorHandler}, which takes care of common cases.
+   */
+  private static Consumer<ErrorResponse> baseTableErrorHandler() {
+    return error -> {
+
+      switch (error.code()) {
+        // HttpStatus.SC_NOT_FOUND
+        case 404:
+          if (NoSuchNamespaceException.class.getSimpleName().equals(error.type())) {
+            throw new NoSuchNamespaceException("Resource not found: %s", error);
+          } else {
+            throw new NoSuchTableException("Resource not found: %s", error);
+          }
+        // HttpStatus.SC_CONFLICT
+        case 409:
+          throw new AlreadyExistsException("The table already exists: %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.
+   */
+  private static Consumer<ErrorResponse> baseNamespaceErrorHandler() {
+    return error -> {
+      switch (error.code()) {
+        // HttpStatus.SC_NOT_FOUND
+        case 404:
+          throw new NoSuchNamespaceException("Namespace not found: %s", error);
+        // HttpStatus.SC_CONFLICT
+        case 409:
+          throw new AlreadyExistsException("Namespace already exists: %s", error);
+        // HttpStatus.SC_UNPROCESSABLE_ENTITY
+        case 422:
+          throw new RESTException("Unable to process request due to bad property updates");
+      }
+    };
+  }
+
+  /**
+   * 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()) {
+        // HttpStatus.SC_CLIENT_ERROR:
+        case 400:
+          throw new BadRequestException("Unable to process the request as it is somehow malformed: %s", errorResponse);

Review comment:
       I'll make a note of this. I changed it to `Malformed request: %s` as that's what 400 is supposed to represent (the payload is syntactically invalid - though it does get used for other things in practice now and again).
   
   That's the response that some frameworks use, but I can update it if we'd like.




-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org