How to catch “org.springframework.web.multipart.MultipartException” in Grails 3

大兔子大兔子 提交于 2020-06-29 09:14:10

问题


I'm having an awfully hard time catching the error: org.springframework.web.multipart.MultipartException

I have the following code:

final class UploadTextCommand  {
   MultipartFile contents
   static constraints = {
   }
}

Additionally to handle the post of the file I have this action:

def upload = { UploadTextCommand -> 
   ...code...
}

I've got these settings in the "application.yml" file to force the error:

controllers:
    upload:
        maxFileSize: 100
        maxRequestSize: 100

So I can reliably recreate the exception, but I can't catch it before the browser shows a horrible view:

HTTP Status 500 - Request processing failed; 
nested exception is org.springframework.web.multipart.MultipartException

Please tell me how to catch this error in Grails. Just using a try/catch in the upload action fails miserably.

Added stack trace on request of commenter:

javax.servlet.ServletException: org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (318635) exceeds the configured maximum (100000)
grails.plugin.cache.web.filter.AbstractFilter.logThrowable(AbstractFilter.java:116)
grails.plugin.cache.web.filter.AbstractFilter.doFilter(AbstractFilter.java:70)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:101)
org.grails.web.servlet.mvc.GrailsWebRequestFilter.doFilterInternal(GrailsWebRequestFilter.java:73)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:101)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:101)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:101)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:101)
org.springframework.boot.context.web.ErrorPageFilter.doFilter(ErrorPageFilter.java:113)
org.springframework.boot.context.web.ErrorPageFilter.forwardToErrorPage(ErrorPageFilter.java:188)
org.springframework.boot.context.web.ErrorPageFilter.handleException(ErrorPageFilter.java:171)
org.springframework.boot.context.web.ErrorPageFilter.doFilter(ErrorPageFilter.java:135)
org.springframework.boot.context.web.ErrorPageFilter.access$000(ErrorPageFilter.java:61)
org.springframework.boot.context.web.ErrorPageFilter$1.doFilterInternal(ErrorPageFilter.java:95)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
org.springframework.boot.context.web.ErrorPageFilter.doFilter(ErrorPageFilter.java:113)

回答1:


You can find in a stacktrace:

the request was rejected because its size (318635) exceeds the configured maximum (100000)

and in your code:

controllers:
    upload:
        maxFileSize: 100
        maxRequestSize: 100

you should exceed maxFileSize, but if you want to handle exception check this solution. IMHO cleaner solution would be implementation of your own before filter or interceptor.



来源:https://stackoverflow.com/questions/36098826/how-to-catch-org-springframework-web-multipart-multipartexception-in-grails-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!