Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

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.Response;
import javax.ws.rs.ext.ExceptionMapper;
Expand All @@ -30,13 +31,20 @@
@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]
Comment thread
vivek807 marked this conversation as resolved.
))
.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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));
}
}