Skip to content

Commit

Permalink
#1569 PR review update: in case of json mapping exception being cause…
Browse files Browse the repository at this point in the history
…d by PropertyTypeValidationException, react with most specific errorMessage to the REST API client
  • Loading branch information
dk1844 committed Dec 10, 2020
1 parent e545f5f commit 4c0cce5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

package za.co.absa.enceladus.menas.controllers

import com.fasterxml.jackson.databind.JsonMappingException
import org.apache.oozie.client.OozieClientException
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Value
Expand All @@ -28,6 +29,7 @@ import za.co.absa.enceladus.menas.models.RestError
import za.co.absa.enceladus.menas.models.rest.RestResponse
import za.co.absa.enceladus.menas.models.rest.errors.{RemoteSchemaRetrievalError, RequestTimeoutExpiredError, SchemaFormatError, SchemaParsingError}
import za.co.absa.enceladus.menas.models.rest.exceptions.{RemoteSchemaRetrievalException, SchemaFormatException, SchemaParsingException}
import za.co.absa.enceladus.model.properties.propertyType.PropertyTypeValidationException
import za.co.absa.enceladus.model.{UsedIn, Validation}

@ControllerAdvice(annotations = Array(classOf[RestController]))
Expand Down Expand Up @@ -83,8 +85,18 @@ class RestExceptionHandler {
// when json <-> object mapping fails, respond with 400 instead of 500
@ExceptionHandler(value = Array(classOf[HttpMessageConversionException]))
def handleHttpMessageConversionException(exception: HttpMessageConversionException): ResponseEntity[Any] = {
logger.error(s"HttpMessageConversionException: ${exception.getMessage}", exception)
ResponseEntity.badRequest().body(exception.getMessage)

// logic: the cause may be our custom PropertyTypeValidationException or another general exception
val specificMessage = exception.getCause match {
case jme:JsonMappingException => jme.getCause match {
case ptve:PropertyTypeValidationException => ptve.getMessage
case _ => jme.getMessage
}
case _ => exception.getMessage
}

logger.error(s"HttpMessageConversionException: $specificMessage", exception)
ResponseEntity.badRequest().body(specificMessage)
}

@ExceptionHandler(value = Array(classOf[EntityInUseException]))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class PropertyDefinitionApiIntegrationSuite extends BaseRestApiTest with BeforeA
val response = sendPost[String, String](urlPattern, bodyOpt = Some(invalidPayload("somePd1")))
assertBadRequest(response)

response.getBody should include("The suggested value invalidOptionC cannot be used: Value 'invalidOptionC' is not one of the allowed values (a, b).")
response.getBody shouldBe "The suggested value invalidOptionC cannot be used: Value 'invalidOptionC' is not one of the allowed values (a, b)."
}
}
}
Expand Down

0 comments on commit 4c0cce5

Please sign in to comment.