Merge pull request #229 from bsinno/Fix_wrong_multipart_exception_message_in_response
Return last throwable in response
This commit is contained in:
@@ -9,11 +9,12 @@
|
|||||||
package org.eclipse.hawkbit.rest.exception;
|
package org.eclipse.hawkbit.rest.exception;
|
||||||
|
|
||||||
import java.util.EnumMap;
|
import java.util.EnumMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
import org.apache.tomcat.util.http.fileupload.FileUploadException;
|
import org.apache.commons.lang3.exception.ExceptionUtils;
|
||||||
import org.eclipse.hawkbit.exception.SpServerError;
|
import org.eclipse.hawkbit.exception.SpServerError;
|
||||||
import org.eclipse.hawkbit.exception.SpServerRtException;
|
import org.eclipse.hawkbit.exception.SpServerRtException;
|
||||||
import org.eclipse.hawkbit.repository.exception.MultiPartFileUploadException;
|
import org.eclipse.hawkbit.repository.exception.MultiPartFileUploadException;
|
||||||
@@ -27,6 +28,8 @@ 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;
|
||||||
|
|
||||||
|
import com.google.common.collect.Iterables;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* General controller advice for exception handling.
|
* General controller advice for exception handling.
|
||||||
*/
|
*/
|
||||||
@@ -135,13 +138,8 @@ public class ResponseExceptionHandler {
|
|||||||
|
|
||||||
logRequest(request, ex);
|
logRequest(request, ex);
|
||||||
|
|
||||||
Throwable responseCause = ex;
|
final List<Throwable> throwables = ExceptionUtils.getThrowableList(ex);
|
||||||
|
final Throwable responseCause = Iterables.getLast(throwables);
|
||||||
final Throwable searchForCause = searchForCause(ex, FileUploadException.class);
|
|
||||||
if (searchForCause != null) {
|
|
||||||
responseCause = searchForCause;
|
|
||||||
}
|
|
||||||
|
|
||||||
final ExceptionInfo response = createExceptionInfo(new MultiPartFileUploadException(responseCause));
|
final ExceptionInfo response = createExceptionInfo(new MultiPartFileUploadException(responseCause));
|
||||||
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
|
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
|
||||||
}
|
}
|
||||||
@@ -150,19 +148,6 @@ public class ResponseExceptionHandler {
|
|||||||
LOG.debug("Handling exception {} of request {}", ex.getClass().getName(), request.getRequestURL());
|
LOG.debug("Handling exception {} of request {}", ex.getClass().getName(), request.getRequestURL());
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Throwable searchForCause(final Throwable t, final Class<?> lookFor) {
|
|
||||||
if (t == null || t.getCause() == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
final Throwable cause = t.getCause();
|
|
||||||
|
|
||||||
if (cause.getClass().equals(lookFor)) {
|
|
||||||
return cause;
|
|
||||||
}
|
|
||||||
return searchForCause(cause, lookFor);
|
|
||||||
}
|
|
||||||
|
|
||||||
private ExceptionInfo createExceptionInfo(final Exception ex) {
|
private ExceptionInfo createExceptionInfo(final Exception ex) {
|
||||||
final ExceptionInfo response = new ExceptionInfo();
|
final ExceptionInfo response = new ExceptionInfo();
|
||||||
response.setMessage(ex.getMessage());
|
response.setMessage(ex.getMessage());
|
||||||
|
|||||||
Reference in New Issue
Block a user