Skip to content

Commit

Permalink
avniproject/avni-webapp#1133 | Show user-friendly error message when …
Browse files Browse the repository at this point in the history
…upload file size is exceeded
  • Loading branch information
himeshr committed May 28, 2024
1 parent 994ec0a commit 5f2ca33
Showing 1 changed file with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
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;
import org.avni.server.util.BadRequestError;
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;
Expand All @@ -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;
Expand All @@ -28,6 +30,8 @@

@RestControllerAdvice
public class ErrorInterceptors extends ResponseEntityExceptionHandler {

private final String maxFileSize;
private final BugsnagReporter bugsnagReporter;
private final ErrorBodyBuilder errorBodyBuilder;

Expand Down Expand Up @@ -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;
}
Expand All @@ -81,6 +86,11 @@ private ResponseEntity <RestControllerErrorResponse> 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) {
Expand Down

0 comments on commit 5f2ca33

Please sign in to comment.