diff --git a/avni-server-api/src/main/java/org/avni/server/web/ErrorInterceptors.java b/avni-server-api/src/main/java/org/avni/server/web/ErrorInterceptors.java index 3b8257325..8c4d3b41e 100644 --- a/avni-server-api/src/main/java/org/avni/server/web/ErrorInterceptors.java +++ b/avni-server-api/src/main/java/org/avni/server/web/ErrorInterceptors.java @@ -1,7 +1,7 @@ package org.avni.server.web; import com.fasterxml.jackson.databind.exc.InvalidFormatException; -import org.avni.server.domain.ValidationException; +import org.apache.tomcat.util.http.fileupload.impl.SizeLimitExceededException; import org.avni.server.domain.accessControl.AvniAccessException; import org.avni.server.domain.accessControl.AvniNoUserSessionException; import org.avni.server.framework.rest.RestControllerErrorResponse; @@ -9,6 +9,7 @@ import org.avni.server.util.BugsnagReporter; import org.avni.server.web.util.ErrorBodyBuilder; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.data.rest.webmvc.ResourceNotFoundException; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; @@ -18,6 +19,7 @@ import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; import org.springframework.web.context.request.WebRequest; +import org.springframework.web.multipart.MaxUploadSizeExceededException; import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; import java.util.HashMap; @@ -28,6 +30,8 @@ @RestControllerAdvice public class ErrorInterceptors extends ResponseEntityExceptionHandler { + + private final String maxFileSize; private final BugsnagReporter bugsnagReporter; private final ErrorBodyBuilder errorBodyBuilder; @@ -58,7 +62,8 @@ public void setMessage(String message) { } @Autowired - public ErrorInterceptors(BugsnagReporter bugsnagReporter, ErrorBodyBuilder errorBodyBuilder) { + public ErrorInterceptors(@Value("${spring.servlet.multipart.max-file-size}") String maxFileSize, BugsnagReporter bugsnagReporter, ErrorBodyBuilder errorBodyBuilder) { + this.maxFileSize = maxFileSize; this.bugsnagReporter = bugsnagReporter; this.errorBodyBuilder = errorBodyBuilder; } @@ -81,6 +86,11 @@ private ResponseEntity error(final Exception excep return new ResponseEntity(new RestControllerErrorResponse(errorBodyBuilder.getErrorBody(message)), httpStatus); } + @ExceptionHandler(value = {MaxUploadSizeExceededException.class, SizeLimitExceededException.class}) + public ResponseEntity fileUploadSizeLimitExceededError(Exception e) { + return ResponseEntity.badRequest().body(String.format("Maximum upload file size exceeded; ensure file size is less than %s.", maxFileSize)); + } + @ExceptionHandler(value = {Exception.class}) public ResponseEntity unknownException(Exception e) { if (e instanceof BadRequestError) {