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

karan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/druid.git


The following commit(s) were added to refs/heads/master by this push:
     new bb44f85bb63 Updated error response to hide error stack in case of 
JsonMappingException (#16821)
bb44f85bb63 is described below

commit bb44f85bb6337f646a60a76103f3169c61736fe6
Author: Vivek Dhiman <[email protected]>
AuthorDate: Thu Nov 21 05:41:48 2024 -0800

    Updated error response to hide error stack in case of JsonMappingException 
(#16821)
    
    Added flag druid.server.http.showDetailedJsonMappingError similar 
druid.server.http.showDetailedJettyError to configure error message detail.
---
 .../jetty/CustomExceptionMapper.java               | 12 +++-
 .../jetty/CustomExceptionMapperTest.java           | 75 ++++++++++++++++++++++
 2 files changed, 85 insertions(+), 2 deletions(-)

diff --git 
a/server/src/main/java/org/apache/druid/server/initialization/jetty/CustomExceptionMapper.java
 
b/server/src/main/java/org/apache/druid/server/initialization/jetty/CustomExceptionMapper.java
index 18148b0c348..344f845702c 100644
--- 
a/server/src/main/java/org/apache/druid/server/initialization/jetty/CustomExceptionMapper.java
+++ 
b/server/src/main/java/org/apache/druid/server/initialization/jetty/CustomExceptionMapper.java
@@ -22,6 +22,7 @@ package org.apache.druid.server.initialization.jetty;
 
 import com.fasterxml.jackson.databind.JsonMappingException;
 import com.google.common.collect.ImmutableMap;
+import org.apache.druid.java.util.common.logger.Logger;
 
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
@@ -31,13 +32,20 @@ import javax.ws.rs.ext.Provider;
 @Provider
 public class CustomExceptionMapper implements 
ExceptionMapper<JsonMappingException>
 {
+  private static final Logger log = new Logger(CustomExceptionMapper.class);
+  public static final String ERROR_KEY = "error";
+  public static final String UNABLE_TO_PROCESS_ERROR = "unknown json mapping 
exception";
+
   @Override
   public Response toResponse(JsonMappingException exception)
   {
+    log.warn(exception.getMessage() == null ? UNABLE_TO_PROCESS_ERROR : 
exception.getMessage());
     return Response.status(Response.Status.BAD_REQUEST)
                    .entity(ImmutableMap.of(
-                       "error",
-                       exception.getMessage() == null ? "unknown json mapping 
exception" : exception.getMessage()
+                       ERROR_KEY,
+                       exception.getMessage() == null
+                       ? UNABLE_TO_PROCESS_ERROR
+                       : 
exception.getMessage().split(System.lineSeparator())[0]
                    ))
                    .type(MediaType.APPLICATION_JSON)
                    .build();
diff --git 
a/server/src/test/java/org/apache/druid/server/initialization/jetty/CustomExceptionMapperTest.java
 
b/server/src/test/java/org/apache/druid/server/initialization/jetty/CustomExceptionMapperTest.java
new file mode 100644
index 00000000000..8f2b7284076
--- /dev/null
+++ 
b/server/src/test/java/org/apache/druid/server/initialization/jetty/CustomExceptionMapperTest.java
@@ -0,0 +1,75 @@
+/*
+ * 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.druid.server.initialization.jetty;
+
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.databind.JsonMappingException;
+import com.google.common.collect.ImmutableMap;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import javax.ws.rs.core.Response;
+
+@RunWith(MockitoJUnitRunner.class)
+public class CustomExceptionMapperTest
+{
+  @Mock
+  private JsonParser jsonParser;
+
+  private CustomExceptionMapper customExceptionMapper;
+
+  @Before
+  public void setUp()
+  {
+    customExceptionMapper = new CustomExceptionMapper();
+  }
+
+  @Test
+  public void testResponseWithSimpleMessage()
+  {
+    final JsonMappingException exception = 
JsonMappingException.from(jsonParser, "Test exception");
+    final Response response = customExceptionMapper.toResponse(exception);
+
+    Assert.assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), 
response.getStatus());
+    Assert.assertTrue(response.getEntity() instanceof ImmutableMap);
+
+    final ImmutableMap<Object, Object> map = (ImmutableMap<Object, Object>) 
response.getEntity();
+    Assert.assertEquals(1, map.size());
+    Assert.assertEquals("Test exception", 
map.get(CustomExceptionMapper.ERROR_KEY));
+  }
+
+  @Test
+  public void testResponseWithLongMessage()
+  {
+    final JsonMappingException exception = 
JsonMappingException.from(jsonParser, "Test exception\nStack trace\nMisc 
details");
+    final Response response = customExceptionMapper.toResponse(exception);
+
+    Assert.assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), 
response.getStatus());
+    Assert.assertTrue(response.getEntity() instanceof ImmutableMap);
+
+    final ImmutableMap<Object, Object> map = (ImmutableMap<Object, Object>) 
response.getEntity();
+    Assert.assertEquals(1, map.size());
+    Assert.assertEquals("Test exception", 
map.get(CustomExceptionMapper.ERROR_KEY));
+  }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to