mirror of
https://github.com/apache/druid.git
synced 2025-02-09 11:34:54 +00:00
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.
This commit is contained in:
parent
2726c6f388
commit
bb44f85bb6
@ -22,6 +22,7 @@ package org.apache.druid.server.initialization.jetty;
|
|||||||
|
|
||||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||||
import com.google.common.collect.ImmutableMap;
|
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.MediaType;
|
||||||
import javax.ws.rs.core.Response;
|
import javax.ws.rs.core.Response;
|
||||||
@ -31,13 +32,20 @@ import javax.ws.rs.ext.Provider;
|
|||||||
@Provider
|
@Provider
|
||||||
public class CustomExceptionMapper implements ExceptionMapper<JsonMappingException>
|
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
|
@Override
|
||||||
public Response toResponse(JsonMappingException exception)
|
public Response toResponse(JsonMappingException exception)
|
||||||
{
|
{
|
||||||
|
log.warn(exception.getMessage() == null ? UNABLE_TO_PROCESS_ERROR : exception.getMessage());
|
||||||
return Response.status(Response.Status.BAD_REQUEST)
|
return Response.status(Response.Status.BAD_REQUEST)
|
||||||
.entity(ImmutableMap.of(
|
.entity(ImmutableMap.of(
|
||||||
"error",
|
ERROR_KEY,
|
||||||
exception.getMessage() == null ? "unknown json mapping exception" : exception.getMessage()
|
exception.getMessage() == null
|
||||||
|
? UNABLE_TO_PROCESS_ERROR
|
||||||
|
: exception.getMessage().split(System.lineSeparator())[0]
|
||||||
))
|
))
|
||||||
.type(MediaType.APPLICATION_JSON)
|
.type(MediaType.APPLICATION_JSON)
|
||||||
.build();
|
.build();
|
||||||
|
@ -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));
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user