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:
@@ -87,10 +87,6 @@ public enum SpServerError {
|
|||||||
*/
|
*/
|
||||||
SP_DS_CREATION_FAILED_MISSING_MODULE("hawkbit.server.error.distributionset.creationFailed.missingModule", "Creation if Distribution Set failed as module is missing that is configured as mandatory."),
|
SP_DS_CREATION_FAILED_MISSING_MODULE("hawkbit.server.error.distributionset.creationFailed.missingModule", "Creation if Distribution Set failed as module is missing that is configured as mandatory."),
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
SP_ARTIFACT_UPLOAD_FILE_LIMIT_EXCEEDED("hawkbit.server.error.artifact.uploadFailed.sizelimitexceeded", "Upload of artifact failed as the file exceeds its maximum permitted size"),
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -68,28 +68,24 @@ public class MgmtSoftwareModuleResource implements MgmtSoftwareModuleRestApi {
|
|||||||
@RequestParam(value = "md5sum", required = false) final String md5Sum,
|
@RequestParam(value = "md5sum", required = false) final String md5Sum,
|
||||||
@RequestParam(value = "sha1sum", required = false) final String sha1Sum) {
|
@RequestParam(value = "sha1sum", required = false) final String sha1Sum) {
|
||||||
|
|
||||||
Artifact result;
|
if (file.isEmpty()) {
|
||||||
if (!file.isEmpty()) {
|
|
||||||
String fileName = optionalFileName;
|
|
||||||
|
|
||||||
if (null == fileName) {
|
|
||||||
fileName = file.getOriginalFilename();
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
result = artifactManagement.createLocalArtifact(file.getInputStream(), softwareModuleId, fileName,
|
|
||||||
md5Sum == null ? null : md5Sum.toLowerCase(), sha1Sum == null ? null : sha1Sum.toLowerCase(),
|
|
||||||
false, file.getContentType());
|
|
||||||
} catch (final IOException e) {
|
|
||||||
LOG.error("Failed to store artifact", e);
|
|
||||||
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
||||||
}
|
}
|
||||||
|
String fileName = optionalFileName;
|
||||||
|
|
||||||
return new ResponseEntity<>(MgmtSoftwareModuleMapper.toResponse(result), HttpStatus.CREATED);
|
if (fileName == null) {
|
||||||
|
fileName = file.getOriginalFilename();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
final Artifact result = artifactManagement.createLocalArtifact(file.getInputStream(), softwareModuleId,
|
||||||
|
fileName, md5Sum == null ? null : md5Sum.toLowerCase(),
|
||||||
|
sha1Sum == null ? null : sha1Sum.toLowerCase(), false, file.getContentType());
|
||||||
|
return new ResponseEntity<>(MgmtSoftwareModuleMapper.toResponse(result), HttpStatus.CREATED);
|
||||||
|
} catch (final IOException e) {
|
||||||
|
LOG.error("Failed to store artifact", e);
|
||||||
|
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -0,0 +1,30 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2015 Bosch Software Innovations GmbH and others.
|
||||||
|
*
|
||||||
|
* All rights reserved. This program and the accompanying materials
|
||||||
|
* are made available under the terms of the Eclipse Public License v1.0
|
||||||
|
* which accompanies this distribution, and is available at
|
||||||
|
* http://www.eclipse.org/legal/epl-v10.html
|
||||||
|
*/
|
||||||
|
package org.eclipse.hawkbit.repository.exception;
|
||||||
|
|
||||||
|
import org.eclipse.hawkbit.exception.SpServerError;
|
||||||
|
import org.eclipse.hawkbit.exception.SpServerRtException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Thrown if a multi part exception occurred.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public final class MultiPartFileUploadException extends SpServerRtException {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param cause
|
||||||
|
* for the exception
|
||||||
|
*/
|
||||||
|
public MultiPartFileUploadException(final Throwable cause) {
|
||||||
|
super(cause.getMessage(), SpServerError.SP_ARTIFACT_UPLOAD_FAILED, cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -13,9 +13,10 @@ import java.util.Map;
|
|||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
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.SpServerError;
|
||||||
import org.eclipse.hawkbit.exception.SpServerRtException;
|
import org.eclipse.hawkbit.exception.SpServerRtException;
|
||||||
|
import org.eclipse.hawkbit.repository.exception.MultiPartFileUploadException;
|
||||||
import org.eclipse.hawkbit.rest.json.model.ExceptionInfo;
|
import org.eclipse.hawkbit.rest.json.model.ExceptionInfo;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@@ -28,10 +29,6 @@ import org.springframework.web.multipart.MultipartException;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* General controller advice for exception handling.
|
* General controller advice for exception handling.
|
||||||
*
|
|
||||||
*
|
|
||||||
*
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
@ControllerAdvice
|
@ControllerAdvice
|
||||||
public class ResponseExceptionHandler {
|
public class ResponseExceptionHandler {
|
||||||
@@ -89,14 +86,11 @@ public class ResponseExceptionHandler {
|
|||||||
@ExceptionHandler(SpServerRtException.class)
|
@ExceptionHandler(SpServerRtException.class)
|
||||||
public ResponseEntity<ExceptionInfo> handleSpServerRtExceptions(final HttpServletRequest request,
|
public ResponseEntity<ExceptionInfo> handleSpServerRtExceptions(final HttpServletRequest request,
|
||||||
final Exception ex) {
|
final Exception ex) {
|
||||||
LOG.debug("Handling exception of request {}", request.getRequestURL());
|
logRequest(request, ex);
|
||||||
final ExceptionInfo response = new ExceptionInfo();
|
final ExceptionInfo response = createExceptionInfo(ex);
|
||||||
final HttpStatus responseStatus;
|
final HttpStatus responseStatus;
|
||||||
response.setMessage(ex.getMessage());
|
|
||||||
response.setExceptionClass(ex.getClass().getName());
|
|
||||||
if (ex instanceof SpServerRtException) {
|
if (ex instanceof SpServerRtException) {
|
||||||
responseStatus = getStatusOrDefault(((SpServerRtException) ex).getError());
|
responseStatus = getStatusOrDefault(((SpServerRtException) ex).getError());
|
||||||
response.setErrorCode(((SpServerRtException) ex).getError().getKey());
|
|
||||||
} else {
|
} else {
|
||||||
responseStatus = DEFAULT_RESPONSE_STATUS;
|
responseStatus = DEFAULT_RESPONSE_STATUS;
|
||||||
}
|
}
|
||||||
@@ -118,11 +112,8 @@ public class ResponseExceptionHandler {
|
|||||||
@ExceptionHandler(HttpMessageNotReadableException.class)
|
@ExceptionHandler(HttpMessageNotReadableException.class)
|
||||||
public ResponseEntity<ExceptionInfo> handleHttpMessageNotReadableException(final HttpServletRequest request,
|
public ResponseEntity<ExceptionInfo> handleHttpMessageNotReadableException(final HttpServletRequest request,
|
||||||
final Exception ex) {
|
final Exception ex) {
|
||||||
LOG.debug("Handling exception {} of request {}", ex.getClass().getName(), request.getRequestURL());
|
logRequest(request, ex);
|
||||||
final ExceptionInfo response = new ExceptionInfo();
|
final ExceptionInfo response = createExceptionInfo(new MessageNotReadableException());
|
||||||
response.setErrorCode(SpServerError.SP_REST_BODY_NOT_READABLE.getKey());
|
|
||||||
response.setMessage(SpServerError.SP_REST_BODY_NOT_READABLE.getMessage());
|
|
||||||
response.setExceptionClass(MessageNotReadableException.class.getName());
|
|
||||||
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
|
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -139,35 +130,48 @@ public class ResponseExceptionHandler {
|
|||||||
* as entity.
|
* as entity.
|
||||||
*/
|
*/
|
||||||
@ExceptionHandler(MultipartException.class)
|
@ExceptionHandler(MultipartException.class)
|
||||||
public ResponseEntity<ExceptionInfo> handleFileLimitExceededException(final HttpServletRequest request,
|
public ResponseEntity<ExceptionInfo> handleMultipartException(final HttpServletRequest request,
|
||||||
final Exception ex) {
|
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)) {
|
Throwable responseCause = ex;
|
||||||
response.setErrorCode(SpServerError.SP_ARTIFACT_UPLOAD_FILE_LIMIT_EXCEEDED.getKey());
|
|
||||||
response.setMessage(SpServerError.SP_ARTIFACT_UPLOAD_FILE_LIMIT_EXCEEDED.getMessage());
|
final Throwable searchForCause = searchForCause(ex, FileUploadException.class);
|
||||||
response.setExceptionClass(FileSizeLimitExceededException.class.getName());
|
if (searchForCause != null) {
|
||||||
} else {
|
responseCause = searchForCause;
|
||||||
response.setErrorCode(SpServerError.SP_ARTIFACT_UPLOAD_FAILED.getKey());
|
|
||||||
response.setMessage(SpServerError.SP_ARTIFACT_UPLOAD_FAILED.getMessage());
|
|
||||||
response.setExceptionClass(MultipartException.class.getName());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
final ExceptionInfo response = createExceptionInfo(new MultiPartFileUploadException(responseCause));
|
||||||
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
|
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean searchForCause(final Throwable t, final Class<?> lookFor) {
|
private void logRequest(final HttpServletRequest request, final Exception ex) {
|
||||||
if (t != null && t.getCause() != null) {
|
LOG.debug("Handling exception {} of request {}", ex.getClass().getName(), request.getRequestURL());
|
||||||
if (t.getCause().getClass().isAssignableFrom(lookFor)) {
|
}
|
||||||
return true;
|
|
||||||
} else {
|
private static Throwable searchForCause(final Throwable t, final Class<?> lookFor) {
|
||||||
return searchForCause(t.getCause(), 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