You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by jh...@apache.org on 2022/11/08 23:36:39 UTC

[calcite-avatica] branch main updated: [CALCITE-5358] Add in HTTP_BAD_REQUEST

This is an automated email from the ASF dual-hosted git repository.

jhyde pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/calcite-avatica.git


The following commit(s) were added to refs/heads/main by this push:
     new bc2857816 [CALCITE-5358] Add in HTTP_BAD_REQUEST
bc2857816 is described below

commit bc285781666ea1cbc35868e0373ed532d45292d8
Author: Oliver Lee <ol...@google.com>
AuthorDate: Wed Nov 2 21:37:11 2022 +0000

    [CALCITE-5358] Add in HTTP_BAD_REQUEST
    
    Close apache/calcite-avatica#190
---
 .../calcite/avatica/remote/AbstractHandler.java    |  7 +++++
 .../org/apache/calcite/avatica/remote/Handler.java |  1 +
 .../avatica/remote/AbstractHandlerTest.java        | 13 +++++++++
 .../calcite/avatica/server/AvaticaJsonHandler.java |  3 ++
 .../avatica/server/AvaticaProtobufHandler.java     |  3 ++
 .../avatica/server/BadRequestException.java        | 34 ++++++++++++++++++++++
 6 files changed, 61 insertions(+)

diff --git a/core/src/main/java/org/apache/calcite/avatica/remote/AbstractHandler.java b/core/src/main/java/org/apache/calcite/avatica/remote/AbstractHandler.java
index b291d4c89..4d57dfcdb 100644
--- a/core/src/main/java/org/apache/calcite/avatica/remote/AbstractHandler.java
+++ b/core/src/main/java/org/apache/calcite/avatica/remote/AbstractHandler.java
@@ -109,6 +109,13 @@ public abstract class AbstractHandler<T> implements Handler<T> {
     return createErrorResponse(e, HTTP_INTERNAL_SERVER_ERROR);
   }
 
+  /**
+   * Attempts to convert an Exception to an ErrorResponse with an HTTP status code of {@code 400}.
+   */
+  public HandlerResponse<T> badRequestErrorResponse(Exception e) {
+    return createErrorResponse(e, HTTP_BAD_REQUEST);
+  }
+
   /**
    * Attempts to convert an Exception to an ErrorResponse with an HTTP status code of {@code 401}.
    */
diff --git a/core/src/main/java/org/apache/calcite/avatica/remote/Handler.java b/core/src/main/java/org/apache/calcite/avatica/remote/Handler.java
index 1a7e916fd..807a63b08 100644
--- a/core/src/main/java/org/apache/calcite/avatica/remote/Handler.java
+++ b/core/src/main/java/org/apache/calcite/avatica/remote/Handler.java
@@ -27,6 +27,7 @@ import java.util.Objects;
  */
 public interface Handler<T> {
   int HTTP_OK = 200;
+  int HTTP_BAD_REQUEST = 400;
   int HTTP_UNAUTHENTICATED = 401;
   int HTTP_UNAUTHORIZED = 403;
   int HTTP_INTERNAL_SERVER_ERROR = 500;
diff --git a/core/src/test/java/org/apache/calcite/avatica/remote/AbstractHandlerTest.java b/core/src/test/java/org/apache/calcite/avatica/remote/AbstractHandlerTest.java
index 83bf50f51..64c7468d6 100644
--- a/core/src/test/java/org/apache/calcite/avatica/remote/AbstractHandlerTest.java
+++ b/core/src/test/java/org/apache/calcite/avatica/remote/AbstractHandlerTest.java
@@ -164,6 +164,19 @@ public class AbstractHandlerTest {
     assertEquals(serializedErrorResponse, response.getResponse());
     assertEquals(500, response.getStatusCode());
   }
+
+  @Test public void testUsingBadRequestExceptionResponse() throws IOException {
+    final AbstractHandler<String> handler = Mockito.mock(AbstractHandler.class);
+    final Exception exception = new Exception("Bad request");
+    final ErrorResponse errorResponse = Mockito.mock(ErrorResponse.class);
+    final String serializedErrorResponse = "An ErrorResponse";
+    Mockito.when(handler.unwrapException(exception)).thenReturn(errorResponse);
+    Mockito.when(handler.encode(errorResponse)).thenReturn(serializedErrorResponse);
+
+    Mockito.when(handler.badRequestErrorResponse(Mockito.any())).thenCallRealMethod();
+    HandlerResponse<String> response = handler.badRequestErrorResponse(exception);
+    assertEquals(400, response.getStatusCode());
+  }
 }
 
 // End AbstractHandlerTest.java
diff --git a/server/src/main/java/org/apache/calcite/avatica/server/AvaticaJsonHandler.java b/server/src/main/java/org/apache/calcite/avatica/server/AvaticaJsonHandler.java
index 709b2d80b..c92d356b7 100644
--- a/server/src/main/java/org/apache/calcite/avatica/server/AvaticaJsonHandler.java
+++ b/server/src/main/java/org/apache/calcite/avatica/server/AvaticaJsonHandler.java
@@ -138,6 +138,9 @@ public class AvaticaJsonHandler extends AbstractAvaticaHandler {
         } catch (RemoteUserDisallowedException e) {
           LOG.debug("Remote user is not authorized", e);
           jsonResponse = jsonHandler.unauthorizedErrorResponse(e);
+        } catch (BadRequestException e) {
+          LOG.debug("Bad request exception", e);
+          jsonResponse = jsonHandler.badRequestErrorResponse(e);
         } catch (Exception e) {
           LOG.debug("Error invoking request from {}", baseRequest.getRemoteAddr(), e);
           jsonResponse = jsonHandler.convertToErrorResponse(e);
diff --git a/server/src/main/java/org/apache/calcite/avatica/server/AvaticaProtobufHandler.java b/server/src/main/java/org/apache/calcite/avatica/server/AvaticaProtobufHandler.java
index 9cd02a772..32dd26da3 100644
--- a/server/src/main/java/org/apache/calcite/avatica/server/AvaticaProtobufHandler.java
+++ b/server/src/main/java/org/apache/calcite/avatica/server/AvaticaProtobufHandler.java
@@ -139,6 +139,9 @@ public class AvaticaProtobufHandler extends AbstractAvaticaHandler {
       } catch (RemoteUserDisallowedException e) {
         LOG.debug("Remote user is not authorized", e);
         handlerResponse = pbHandler.unauthorizedErrorResponse(e);
+      } catch (BadRequestException e) {
+        LOG.debug("Bad request exception", e);
+        handlerResponse = pbHandler.badRequestErrorResponse(e);
       } catch (Exception e) {
         LOG.debug("Error invoking request from {}", baseRequest.getRemoteAddr(), e);
         // Catch at the highest level of exceptions
diff --git a/server/src/main/java/org/apache/calcite/avatica/server/BadRequestException.java b/server/src/main/java/org/apache/calcite/avatica/server/BadRequestException.java
new file mode 100644
index 000000000..1c53141fc
--- /dev/null
+++ b/server/src/main/java/org/apache/calcite/avatica/server/BadRequestException.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.calcite.avatica.server;
+
+/**
+ * An exception thrown when encountering a malformed request.
+ */
+public class BadRequestException extends Exception {
+  private static final long serialVersionUID = 1L;
+
+  public BadRequestException(String message) {
+    super(message);
+  }
+
+  public BadRequestException(String message, Throwable cause) {
+    super(message, cause);
+  }
+}
+
+// End BadRequestException.java