Handle response of MethodArgumentNotValidException (#855)
* Handle response of MethodArgumentNotValidException * Readapt DdiDeploymentBaseTest after merge conflicts * Handle Exceptions HttpMessageNotReadable and MethodArgumentNotValid in one ExceptionHandler method Signed-off-by: Natalia Kislicyn <natalia.kislicyn@bosch-si.com>
This commit is contained in:
committed by
Stefan Behl
parent
379726a697
commit
0812937f3f
@@ -46,6 +46,7 @@ import org.eclipse.hawkbit.repository.model.Target;
|
|||||||
import org.eclipse.hawkbit.repository.model.TargetUpdateStatus;
|
import org.eclipse.hawkbit.repository.model.TargetUpdateStatus;
|
||||||
import org.eclipse.hawkbit.repository.test.matcher.Expect;
|
import org.eclipse.hawkbit.repository.test.matcher.Expect;
|
||||||
import org.eclipse.hawkbit.repository.test.matcher.ExpectEvents;
|
import org.eclipse.hawkbit.repository.test.matcher.ExpectEvents;
|
||||||
|
import org.eclipse.hawkbit.rest.exception.MessageNotReadableException;
|
||||||
import org.eclipse.hawkbit.rest.util.JsonBuilder;
|
import org.eclipse.hawkbit.rest.util.JsonBuilder;
|
||||||
import org.eclipse.hawkbit.rest.util.MockMvcResultPrinter;
|
import org.eclipse.hawkbit.rest.util.MockMvcResultPrinter;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@@ -773,7 +774,9 @@ public class DdiDeploymentBaseTest extends AbstractDDiApiIntegrationTest {
|
|||||||
final String missingResultInFeedback = JsonBuilder.missingResultInFeedback(action.getId().toString(), "closed",
|
final String missingResultInFeedback = JsonBuilder.missingResultInFeedback(action.getId().toString(), "closed",
|
||||||
"test");
|
"test");
|
||||||
postFeedback(MediaType.APPLICATION_JSON, "1080", action.getId(), missingResultInFeedback,
|
postFeedback(MediaType.APPLICATION_JSON, "1080", action.getId(), missingResultInFeedback,
|
||||||
status().isBadRequest());
|
status().isBadRequest())
|
||||||
|
.andExpect(jsonPath("$.*", hasSize(3)))
|
||||||
|
.andExpect(jsonPath("$.exceptionClass", equalTo(MessageNotReadableException.class.getCanonicalName())));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -794,7 +797,9 @@ public class DdiDeploymentBaseTest extends AbstractDDiApiIntegrationTest {
|
|||||||
final String missingFinishedResultInFeedback = JsonBuilder
|
final String missingFinishedResultInFeedback = JsonBuilder
|
||||||
.missingFinishedResultInFeedback(action.getId().toString(), "closed", "test");
|
.missingFinishedResultInFeedback(action.getId().toString(), "closed", "test");
|
||||||
postFeedback(MediaType.APPLICATION_JSON, "1080", action.getId(), missingFinishedResultInFeedback,
|
postFeedback(MediaType.APPLICATION_JSON, "1080", action.getId(), missingFinishedResultInFeedback,
|
||||||
status().isBadRequest());
|
status().isBadRequest())
|
||||||
|
.andExpect(jsonPath("$.*", hasSize(3)))
|
||||||
|
.andExpect(jsonPath("$.exceptionClass", equalTo(MessageNotReadableException.class.getCanonicalName())));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void assertActionStatusCount(final int actionStatusCount, final int minActionStatusCountInPage) {
|
private void assertActionStatusCount(final int actionStatusCount, final int minActionStatusCountInPage) {
|
||||||
@@ -829,14 +834,14 @@ public class DdiDeploymentBaseTest extends AbstractDDiApiIntegrationTest {
|
|||||||
.andExpect(content().contentTypeCompatibleWith(mediaType));
|
.andExpect(content().contentTypeCompatibleWith(mediaType));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void postFeedback(final MediaType mediaType, final String controllerId, final Long id, final String content,
|
private ResultActions postFeedback(final MediaType mediaType, final String controllerId, final Long id, final String content,
|
||||||
final ResultMatcher statusMatcher) throws Exception {
|
final ResultMatcher statusMatcher) throws Exception {
|
||||||
postFeedback(mediaType, controllerId, id, content.getBytes(), statusMatcher);
|
return postFeedback(mediaType, controllerId, id, content.getBytes(), statusMatcher);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void postFeedback(final MediaType mediaType, final String controllerId, final Long id, final byte[] content,
|
private ResultActions postFeedback(final MediaType mediaType, final String controllerId, final Long id, final byte[] content,
|
||||||
final ResultMatcher statusMatcher) throws Exception {
|
final ResultMatcher statusMatcher) throws Exception {
|
||||||
mvc.perform(post(DEPLOYMENT_BASE + id + "/feedback", tenantAware.getCurrentTenant(), controllerId)
|
return mvc.perform(post(DEPLOYMENT_BASE + id + "/feedback", tenantAware.getCurrentTenant(), controllerId)
|
||||||
.content(content).contentType(mediaType).accept(mediaType)).andDo(MockMvcResultPrinter.print())
|
.content(content).contentType(mediaType).accept(mediaType)).andDo(MockMvcResultPrinter.print())
|
||||||
.andExpect(statusMatcher);
|
.andExpect(statusMatcher);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ import org.slf4j.LoggerFactory;
|
|||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.http.converter.HttpMessageNotReadableException;
|
import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||||
|
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
import org.springframework.web.multipart.MultipartException;
|
import org.springframework.web.multipart.MultipartException;
|
||||||
@@ -112,9 +113,10 @@ public class ResponseExceptionHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method for handling exception of type HttpMessageNotReadableException
|
* Method for handling exception of type HttpMessageNotReadableException and
|
||||||
* which is thrown in case the request body is not well formed and cannot be
|
* MethodArgumentNotValidException which are thrown in case the request body is
|
||||||
* deserialized. Called by the Spring-Framework for exception handling.
|
* not well formed (e.g. syntax failures, missing/invalid parameters) and cannot
|
||||||
|
* be deserialized. Called by the Spring-Framework for exception handling.
|
||||||
*
|
*
|
||||||
* @param request
|
* @param request
|
||||||
* the Http request
|
* the Http request
|
||||||
@@ -123,8 +125,8 @@ public class ResponseExceptionHandler {
|
|||||||
* @return the entity to be responded containing the exception information
|
* @return the entity to be responded containing the exception information
|
||||||
* as entity.
|
* as entity.
|
||||||
*/
|
*/
|
||||||
@ExceptionHandler(HttpMessageNotReadableException.class)
|
@ExceptionHandler({HttpMessageNotReadableException.class, MethodArgumentNotValidException.class})
|
||||||
public ResponseEntity<ExceptionInfo> handleHttpMessageNotReadableException(final HttpServletRequest request,
|
public ResponseEntity<ExceptionInfo> handleExceptionCausedByIncorrectRequestBody(final HttpServletRequest request,
|
||||||
final Exception ex) {
|
final Exception ex) {
|
||||||
logRequest(request, ex);
|
logRequest(request, ex);
|
||||||
final ExceptionInfo response = createExceptionInfo(new MessageNotReadableException());
|
final ExceptionInfo response = createExceptionInfo(new MessageNotReadableException());
|
||||||
|
|||||||
Reference in New Issue
Block a user