I have an app that allows users to upload files, I can't control the size of the files they are uploading, and maxFileSize will always be finite.
If the user uploads a file that is bigger than maxFileSize, there is an internal exception I need to grab to present the error to the user, but the exception seems to be thrown from the Servlet.service() before reaching the contoller (even the exception is a Tomcat thing).
How can I catch that exception in a controller to be able to show the user an error using Grails 3?
FYI, this is the trace I get:
2020-06-26 14:35:28.158 ERROR --- [nio-8095-exec-7] o.g.web.errors.GrailsExceptionResolver : SizeLimitExceededException occurred when processing request: [POST] /operationalTemplate/upload
the request was rejected because its size (6530988) exceeds the configured maximum (5242880). Stacktrace follows:
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 (6530988) exceeds the configured maximum (5242880)
at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.parseRequest(StandardMultipartHttpServletRequest.java:116)
at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.<init>(StandardMultipartHttpServletRequest.java:90)
at org.springframework.web.multipart.support.StandardServletMultipartResolver.resolveMultipart(StandardServletMultipartResolver.java:81)
at org.springframework.web.servlet.DispatcherServlet.checkMultipart(DispatcherServlet.java:1104)
at org.grails.web.servlet.mvc.GrailsDispatcherServlet.checkMultipart(GrailsDispatcherServlet.groovy:75)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:936)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at org.springframework.boot.web.filter.ApplicationContextHeaderFilter.doFilterInternal(ApplicationContextHeaderFilter.java:55)
at org.grails.web.servlet.mvc.GrailsWebRequestFilter.doFilterInternal(GrailsWebRequestFilter.java:77)
at org.grails.web.filters.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:67)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (6530988) exceeds the configured maximum (5242880)
at org.grails.web.filters.HiddenHttpMethodFilter.getHttpMethodOverride(HiddenHttpMethodFilter.java:71)
at org.grails.web.filters.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:60)
... 3 common frames omitted
Caused by: org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (6530988) exceeds the configured maximum (5242880)
... 5 common frames omitted
2020-06-26 14:35:28.171 ERROR --- [nio-8095-exec-7] .a.c.c.C.[.[.[.[grailsDispatcherServlet] : Servlet.service() for servlet [grailsDispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is 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 (6530988) exceeds the configured maximum (5242880)] with root cause
org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (6530988) exceeds the configured maximum (5242880)
at org.grails.web.filters.HiddenHttpMethodFilter.getHttpMethodOverride(HiddenHttpMethodFilter.java:71)
at org.grails.web.filters.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:60)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)