Fix #665 - When JSON payload parsing error occurs while processing request response does not include ErrorMessage

This commit is contained in:
sureshms 2021-10-04 21:06:29 -07:00
parent ce3fbe17ec
commit 02b7ded510
2 changed files with 50 additions and 5 deletions

View File

@ -20,9 +20,11 @@ import com.google.inject.Guice;
import com.google.inject.Injector;
import io.dropwizard.health.conf.HealthConfiguration;
import io.dropwizard.health.core.HealthCheckBundle;
import io.dropwizard.jersey.jackson.JsonProcessingExceptionMapper;
import org.openmetadata.catalog.events.EventFilter;
import org.openmetadata.catalog.exception.CatalogGenericExceptionMapper;
import org.openmetadata.catalog.exception.ConstraintViolationExceptionMapper;
import org.openmetadata.catalog.exception.JsonMappingExceptionMapper;
import org.openmetadata.catalog.security.AuthenticationConfiguration;
import org.openmetadata.catalog.security.NoopFilter;
import org.openmetadata.catalog.security.AuthorizerConfiguration;
@ -39,7 +41,6 @@ import io.dropwizard.jdbi.DBIFactory;
import io.dropwizard.jdbi.OptionalContainerFactory;
import io.dropwizard.jersey.errors.EarlyEofExceptionMapper;
import io.dropwizard.jersey.errors.LoggingExceptionMapper;
import io.dropwizard.jersey.jackson.JsonProcessingExceptionMapper;
import io.dropwizard.lifecycle.Managed;
import io.dropwizard.server.DefaultServerFactory;
import io.dropwizard.setup.Bootstrap;
@ -86,8 +87,7 @@ public class CatalogApplication extends Application<CatalogApplicationConfig> {
// Unregister dropwizard default exception mappers
((DefaultServerFactory) catalogConfig.getServerFactory()).setRegisterDefaultExceptionMappers(false);
environment.jersey()
.property(ServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR, true);
environment.jersey().property(ServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR, true);
environment.jersey().register(MultiPartFeature.class);
environment.jersey().register(CatalogGenericExceptionMapper.class);
@ -95,10 +95,10 @@ public class CatalogApplication extends Application<CatalogApplicationConfig> {
environment.jersey().register(new ConstraintViolationExceptionMapper());
// Restore dropwizard default exception mappers
environment.jersey().register(new LoggingExceptionMapper<>() {
});
environment.jersey().register(new LoggingExceptionMapper<>() {});
environment.jersey().register(new JsonProcessingExceptionMapper(true));
environment.jersey().register(new EarlyEofExceptionMapper());
environment.jersey().register(JsonMappingExceptionMapper.class);
environment.healthChecks().register("UserDatabaseCheck", new CatalogHealthCheck(catalogConfig, jdbi));
registerResources(catalogConfig, environment, jdbi);

View File

@ -0,0 +1,45 @@
/*
* 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.openmetadata.catalog.exception;
import com.fasterxml.jackson.databind.JsonMappingException;
import io.dropwizard.jersey.errors.ErrorMessage;
import javax.annotation.Priority;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
/**
* Dropwizard by default maps the JSON payload format issues (invalid URI etc.) violations to 400 Response
* with no error message. This exception mapper overrides that behavior by including ErrorMessage json in the
* response along with the error code.
*/
@Provider
@Priority(1) // Override the default JsonMappingExceptionMapper by setting the priority higher
public class JsonMappingExceptionMapper implements ExceptionMapper<JsonMappingException> {
@Override
public Response toResponse(JsonMappingException exception) {
final Response response = BadRequestException.of().getResponse();
return Response.fromResponse(response)
.type(MediaType.APPLICATION_JSON_TYPE)
.entity(new ErrorMessage(response.getStatus(), exception.getLocalizedMessage()))
.build();
}
}