[NO ISSUE][MISC] HTTP error handling, exception message cleanup

Change-Id: If8fc109aee55571aff4a87029cad2a7a0c516d0c
Reviewed-on: https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/15023
Reviewed-by: Hussain Towaileb <hussainht@gmail.com>
Tested-by: Michael Blow <mblow@apache.org>
diff --git a/hyracks-fullstack/hyracks/hyracks-http/pom.xml b/hyracks-fullstack/hyracks/hyracks-http/pom.xml
index 466a447..e06f6be 100644
--- a/hyracks-fullstack/hyracks/hyracks-http/pom.xml
+++ b/hyracks-fullstack/hyracks/hyracks-http/pom.xml
@@ -125,5 +125,10 @@
       <version>${project.version}</version>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.apache.hyracks</groupId>
+      <artifactId>hyracks-api</artifactId>
+      <version>${project.version}</version>
+    </dependency>
   </dependencies>
 </project>
\ No newline at end of file
diff --git a/hyracks-fullstack/hyracks/hyracks-http/src/main/java/org/apache/hyracks/http/server/AbstractServlet.java b/hyracks-fullstack/hyracks/hyracks-http/src/main/java/org/apache/hyracks/http/server/AbstractServlet.java
index ff23ac2..64a76f7 100644
--- a/hyracks-fullstack/hyracks/hyracks-http/src/main/java/org/apache/hyracks/http/server/AbstractServlet.java
+++ b/hyracks-fullstack/hyracks/hyracks-http/src/main/java/org/apache/hyracks/http/server/AbstractServlet.java
@@ -26,6 +26,7 @@
 import java.util.Arrays;
 import java.util.concurrent.ConcurrentMap;
 
+import org.apache.hyracks.api.exceptions.IFormattedException;
 import org.apache.hyracks.http.api.IServlet;
 import org.apache.hyracks.http.api.IServletRequest;
 import org.apache.hyracks.http.api.IServletResponse;
@@ -98,7 +99,7 @@
             } else if (HttpMethod.OPTIONS.equals(method)) {
                 options(request, response);
             } else {
-                notAllowed(method, response);
+                methodNotAllowed(method, response);
             }
         } catch (Exception e) {
             LOGGER.log(Level.WARN, "Unhandled exception", e);
@@ -113,21 +114,31 @@
     }
 
     protected void sendError(IServletResponse response, HttpResponseStatus status, String message) throws IOException {
+        sendError(response, HttpUtil.ContentType.TEXT_PLAIN, status, message);
+    }
+
+    protected void sendError(IServletResponse response, String contentType, HttpResponseStatus status, String message)
+            throws IOException {
         response.setStatus(status);
-        HttpUtil.setContentType(response, HttpUtil.ContentType.TEXT_PLAIN, StandardCharsets.UTF_8);
+        HttpUtil.setContentType(response, contentType, StandardCharsets.UTF_8);
         if (message != null) {
             response.writer().println(message);
         }
         if (LOGGER.isInfoEnabled()) {
-            LOGGER.info("sendError: status=" + status + ", message=" + message);
+            LOGGER.info("sendError: status={}, message={}", status, message);
         }
     }
 
-    protected void sendError(IServletResponse response, HttpResponseStatus status) throws IOException {
-        sendError(response, status, null);
+    protected void sendError(IServletResponse response, HttpResponseStatus status, IFormattedException ex)
+            throws IOException {
+        sendError(response, status, ex != null ? ex.getMessage() : null);
     }
 
-    protected void notAllowed(HttpMethod method, IServletResponse response) throws IOException {
+    protected void sendError(IServletResponse response, HttpResponseStatus status) throws IOException {
+        sendError(response, status, status.reasonPhrase());
+    }
+
+    protected void methodNotAllowed(HttpMethod method, IServletResponse response) throws IOException {
         sendError(response, HttpResponseStatus.METHOD_NOT_ALLOWED,
                 "Method " + method + " not allowed for the requested resource.");
     }
@@ -135,37 +146,37 @@
     @SuppressWarnings("squid:S1172")
     protected void get(IServletRequest request, IServletResponse response) throws Exception {
         // designed to be extended but an error in standard case
-        notAllowed(HttpMethod.GET, response);
+        methodNotAllowed(HttpMethod.GET, response);
     }
 
     @SuppressWarnings("squid:S1172")
     protected void head(IServletRequest request, IServletResponse response) throws Exception {
         // designed to be extended but an error in standard case
-        notAllowed(HttpMethod.HEAD, response);
+        methodNotAllowed(HttpMethod.HEAD, response);
     }
 
     @SuppressWarnings("squid:S1172")
     protected void post(IServletRequest request, IServletResponse response) throws Exception {
         // designed to be extended but an error in standard case
-        notAllowed(HttpMethod.POST, response);
+        methodNotAllowed(HttpMethod.POST, response);
     }
 
     @SuppressWarnings("squid:S1172")
     protected void put(IServletRequest request, IServletResponse response) throws Exception {
         // designed to be extended but an error in standard case
-        notAllowed(HttpMethod.PUT, response);
+        methodNotAllowed(HttpMethod.PUT, response);
     }
 
     @SuppressWarnings("squid:S1172")
     protected void delete(IServletRequest request, IServletResponse response) throws Exception {
         // designed to be extended but an error in standard case
-        notAllowed(HttpMethod.DELETE, response);
+        methodNotAllowed(HttpMethod.DELETE, response);
     }
 
     @SuppressWarnings("squid:S1172")
     protected void options(IServletRequest request, IServletResponse response) throws Exception {
         // designed to be extended but an error in standard case
-        notAllowed(HttpMethod.OPTIONS, response);
+        methodNotAllowed(HttpMethod.OPTIONS, response);
     }
 
     public String host(IServletRequest request) {