Fix wrong multi part exception message in response.
Hardening all exception message and classes in exception handler for reponses Signed-off-by: SirWayne <dennis.melzer@bosch-si.com>
This commit is contained in:
@@ -13,9 +13,10 @@ import java.util.Map;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.apache.tomcat.util.http.fileupload.FileUploadBase.FileSizeLimitExceededException;
|
||||
import org.apache.tomcat.util.http.fileupload.FileUploadException;
|
||||
import org.eclipse.hawkbit.exception.SpServerError;
|
||||
import org.eclipse.hawkbit.exception.SpServerRtException;
|
||||
import org.eclipse.hawkbit.repository.exception.MultiPartFileUploadException;
|
||||
import org.eclipse.hawkbit.rest.json.model.ExceptionInfo;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -28,10 +29,6 @@ import org.springframework.web.multipart.MultipartException;
|
||||
|
||||
/**
|
||||
* General controller advice for exception handling.
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*/
|
||||
@ControllerAdvice
|
||||
public class ResponseExceptionHandler {
|
||||
@@ -89,14 +86,11 @@ public class ResponseExceptionHandler {
|
||||
@ExceptionHandler(SpServerRtException.class)
|
||||
public ResponseEntity<ExceptionInfo> handleSpServerRtExceptions(final HttpServletRequest request,
|
||||
final Exception ex) {
|
||||
LOG.debug("Handling exception of request {}", request.getRequestURL());
|
||||
final ExceptionInfo response = new ExceptionInfo();
|
||||
logRequest(request, ex);
|
||||
final ExceptionInfo response = createExceptionInfo(ex);
|
||||
final HttpStatus responseStatus;
|
||||
response.setMessage(ex.getMessage());
|
||||
response.setExceptionClass(ex.getClass().getName());
|
||||
if (ex instanceof SpServerRtException) {
|
||||
responseStatus = getStatusOrDefault(((SpServerRtException) ex).getError());
|
||||
response.setErrorCode(((SpServerRtException) ex).getError().getKey());
|
||||
} else {
|
||||
responseStatus = DEFAULT_RESPONSE_STATUS;
|
||||
}
|
||||
@@ -118,11 +112,8 @@ public class ResponseExceptionHandler {
|
||||
@ExceptionHandler(HttpMessageNotReadableException.class)
|
||||
public ResponseEntity<ExceptionInfo> handleHttpMessageNotReadableException(final HttpServletRequest request,
|
||||
final Exception ex) {
|
||||
LOG.debug("Handling exception {} of request {}", ex.getClass().getName(), request.getRequestURL());
|
||||
final ExceptionInfo response = new ExceptionInfo();
|
||||
response.setErrorCode(SpServerError.SP_REST_BODY_NOT_READABLE.getKey());
|
||||
response.setMessage(SpServerError.SP_REST_BODY_NOT_READABLE.getMessage());
|
||||
response.setExceptionClass(MessageNotReadableException.class.getName());
|
||||
logRequest(request, ex);
|
||||
final ExceptionInfo response = createExceptionInfo(new MessageNotReadableException());
|
||||
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
|
||||
@@ -139,35 +130,48 @@ public class ResponseExceptionHandler {
|
||||
* as entity.
|
||||
*/
|
||||
@ExceptionHandler(MultipartException.class)
|
||||
public ResponseEntity<ExceptionInfo> handleFileLimitExceededException(final HttpServletRequest request,
|
||||
public ResponseEntity<ExceptionInfo> handleMultipartException(final HttpServletRequest request,
|
||||
final Exception ex) {
|
||||
LOG.debug("Handling exception {} of request {}", ex.getClass().getName(), request.getRequestURL());
|
||||
|
||||
final ExceptionInfo response = new ExceptionInfo();
|
||||
logRequest(request, ex);
|
||||
|
||||
if (searchForCause(ex, FileSizeLimitExceededException.class)) {
|
||||
response.setErrorCode(SpServerError.SP_ARTIFACT_UPLOAD_FILE_LIMIT_EXCEEDED.getKey());
|
||||
response.setMessage(SpServerError.SP_ARTIFACT_UPLOAD_FILE_LIMIT_EXCEEDED.getMessage());
|
||||
response.setExceptionClass(FileSizeLimitExceededException.class.getName());
|
||||
} else {
|
||||
response.setErrorCode(SpServerError.SP_ARTIFACT_UPLOAD_FAILED.getKey());
|
||||
response.setMessage(SpServerError.SP_ARTIFACT_UPLOAD_FAILED.getMessage());
|
||||
response.setExceptionClass(MultipartException.class.getName());
|
||||
Throwable responseCause = ex;
|
||||
|
||||
final Throwable searchForCause = searchForCause(ex, FileUploadException.class);
|
||||
if (searchForCause != null) {
|
||||
responseCause = searchForCause;
|
||||
}
|
||||
|
||||
final ExceptionInfo response = createExceptionInfo(new MultiPartFileUploadException(responseCause));
|
||||
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
|
||||
private static boolean searchForCause(final Throwable t, final Class<?> lookFor) {
|
||||
if (t != null && t.getCause() != null) {
|
||||
if (t.getCause().getClass().isAssignableFrom(lookFor)) {
|
||||
return true;
|
||||
} else {
|
||||
return searchForCause(t.getCause(), lookFor);
|
||||
}
|
||||
private void logRequest(final HttpServletRequest request, final Exception ex) {
|
||||
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;
|
||||
}
|
||||
|
||||
return false;
|
||||
final Throwable cause = t.getCause();
|
||||
|
||||
if (cause.getClass().equals(lookFor)) {
|
||||
return cause;
|
||||
}
|
||||
return searchForCause(cause, lookFor);
|
||||
}
|
||||
|
||||
private ExceptionInfo createExceptionInfo(final Exception ex) {
|
||||
final ExceptionInfo response = new ExceptionInfo();
|
||||
response.setMessage(ex.getMessage());
|
||||
response.setExceptionClass(ex.getClass().getName());
|
||||
if (ex instanceof SpServerRtException) {
|
||||
response.setErrorCode(((SpServerRtException) ex).getError().getKey());
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user