3

I'm using Spring Boot (1.5.7) and ng2-file-upload (1.2.1) for uploading files to the server.

So I have the next trouble, when file uploading starts and interrupts (on closing browser tab for example) Spring Boot logs next error (two times):

2018-10-10 14:45:57.054 ERROR 11540 --- [http-nio-8282-exec-6] o.a.c.c.C.[.[.[.[dispatcherServlet]      : line[182] - Servlet.service() for servlet [dispatcherServlet] 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.io.IOException: org.apache.tomcat.util.http.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. Unexpected EOF read on the socket] with root cause
java.io.EOFException: Unexpected EOF read on the socket
    at org.apache.coyote.http11.InternalNioInputBuffer.fill(InternalNioInputBuffer.java:152) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.coyote.http11.InternalNioInputBuffer$SocketInputBuffer.doRead(InternalNioInputBuffer.java:177) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.coyote.http11.filters.IdentityInputFilter.doRead(IdentityInputFilter.java:110) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.coyote.http11.AbstractInputBuffer.doRead(AbstractInputBuffer.java:362) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.coyote.Request.doRead(Request.java:476) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.connector.InputBuffer.realReadBytes(InputBuffer.java:344) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.tomcat.util.buf.ByteChunk.substract(ByteChunk.java:395) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.connector.InputBuffer.read(InputBuffer.java:369) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.connector.CoyoteInputStream.read(CoyoteInputStream.java:190) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at java.io.FilterInputStream.read(FilterInputStream.java:133) ~[?:1.8.0_161]
    at org.apache.tomcat.util.http.fileupload.util.LimitedInputStream.read(LimitedInputStream.java:132) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.tomcat.util.http.fileupload.MultipartStream$ItemInputStream.makeAvailable(MultipartStream.java:977) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.tomcat.util.http.fileupload.MultipartStream$ItemInputStream.read(MultipartStream.java:881) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at java.io.FilterInputStream.read(FilterInputStream.java:133) ~[?:1.8.0_161]
    at org.apache.tomcat.util.http.fileupload.util.LimitedInputStream.read(LimitedInputStream.java:132) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at java.io.FilterInputStream.read(FilterInputStream.java:107) ~[?:1.8.0_161]
    at org.apache.tomcat.util.http.fileupload.util.Streams.copy(Streams.java:98) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.tomcat.util.http.fileupload.util.Streams.copy(Streams.java:68) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.tomcat.util.http.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:293) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.connector.Request.parseParts(Request.java:2803) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.connector.Request.parseParameters(Request.java:3178) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.connector.Request.getParameter(Request.java:1109) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.connector.RequestFacade.getParameter(RequestFacade.java:380) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:75) ~[spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) ~[spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.springframework.boot.actuate.autoconfigure.MetricsFilter.doFilterInternal(MetricsFilter.java:106) ~[spring-boot-actuator-1.5.7.RELEASE.jar:1.5.7.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:94) [tomcat-embed-core-8.0.46.jar:8.0.46]
...

When I added @ExceptionHandler to @ControllerAdvice:

@ExceptionHandler(MultipartException.class)
protected ResponseData handleFileUploadingError(Exception e) {
    log.warn("Failed to upload attachment", e);
    return ResponseData.with(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}

And next property to application.properties:

spring.mvc.throw-exception-if-no-handler-found=true

Now when file uploading interrupted @ExceptionHandler logs error as it should (next snippet) but time to time after that Spring Boot still logs error from dispatcherServlet (like in first snippet)

2018-10-10 14:52:32.655 WARN 14392 --- [http-nio-8282-exec-9] c.c.i.c.a.ExceptionHandlerAdvice         : line[76] - Failed to upload attachment
org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is java.io.IOException: org.apache.tomcat.util.http.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. Unexpected EOF read on the socket
    at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.parseRequest(StandardMultipartHttpServletRequest.java:112) ~[spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.<init>(StandardMultipartHttpServletRequest.java:86) ~[spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.springframework.web.multipart.support.StandardServletMultipartResolver.resolveMultipart(StandardServletMultipartResolver.java:79) ~[spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.checkMultipart(DispatcherServlet.java:1104) [spring-webmvc-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:936) [spring-webmvc-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) [spring-webmvc-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) [spring-webmvc-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872) [spring-webmvc-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:648) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) [spring-webmvc-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) [tomcat-embed-websocket-8.0.46.jar:8.0.46]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.springframework.boot.web.filter.ApplicationContextHeaderFilter.doFilterInternal(ApplicationContextHeaderFilter.java:55) [spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.springframework.boot.actuate.trace.WebRequestTraceFilter.doFilterInternal(WebRequestTraceFilter.java:110) [spring-boot-actuator-1.5.7.RELEASE.jar:1.5.7.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.springframework.web.filter.CorsFilter.doFilterInternal(CorsFilter.java:96) [spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:317) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:127) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:114) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:170) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationProcessingFilter.doFilter(OAuth2AuthenticationProcessingFilter.java:176) [spring-security-oauth2-2.1.1.RELEASE.jar:?]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:64) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:214) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:177) [spring-security-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346) [spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262) [spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) [spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:108) [spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81) [spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) [spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.springframework.boot.actuate.autoconfigure.MetricsFilter.doFilterInternal(MetricsFilter.java:106) [spring-boot-actuator-1.5.7.RELEASE.jar:1.5.7.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:94) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1132) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1539) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1495) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [?:1.8.0_161]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [?:1.8.0_161]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.0.46.jar:8.0.46]
    at java.lang.Thread.run(Thread.java:748) [?:1.8.0_161]
Caused by: java.io.IOException: org.apache.tomcat.util.http.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. Unexpected EOF read on the socket
    at org.apache.catalina.connector.Request.parseParts(Request.java:2876) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.connector.Request.parseParameters(Request.java:3178) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.connector.Request.getParameter(Request.java:1109) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.connector.RequestFacade.getParameter(RequestFacade.java:380) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:75) ~[spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    ... 27 more
Caused by: org.apache.tomcat.util.http.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. Unexpected EOF read on the socket
    at org.apache.tomcat.util.http.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:297) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.connector.Request.parseParts(Request.java:2803) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.connector.Request.parseParameters(Request.java:3178) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.connector.Request.getParameter(Request.java:1109) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.connector.RequestFacade.getParameter(RequestFacade.java:380) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:75) ~[spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    ... 27 more
Caused by: java.io.EOFException: Unexpected EOF read on the socket
    at org.apache.coyote.http11.InternalNioInputBuffer.fill(InternalNioInputBuffer.java:152) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.coyote.http11.InternalNioInputBuffer$SocketInputBuffer.doRead(InternalNioInputBuffer.java:177) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.coyote.http11.filters.IdentityInputFilter.doRead(IdentityInputFilter.java:110) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.coyote.http11.AbstractInputBuffer.doRead(AbstractInputBuffer.java:362) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.coyote.Request.doRead(Request.java:476) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.connector.InputBuffer.realReadBytes(InputBuffer.java:344) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.tomcat.util.buf.ByteChunk.substract(ByteChunk.java:395) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.connector.InputBuffer.read(InputBuffer.java:369) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.connector.CoyoteInputStream.read(CoyoteInputStream.java:190) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at java.io.FilterInputStream.read(FilterInputStream.java:133) ~[?:1.8.0_161]
    at org.apache.tomcat.util.http.fileupload.util.LimitedInputStream.read(LimitedInputStream.java:132) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.tomcat.util.http.fileupload.MultipartStream$ItemInputStream.makeAvailable(MultipartStream.java:977) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.tomcat.util.http.fileupload.MultipartStream$ItemInputStream.read(MultipartStream.java:881) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at java.io.FilterInputStream.read(FilterInputStream.java:133) ~[?:1.8.0_161]
    at org.apache.tomcat.util.http.fileupload.util.LimitedInputStream.read(LimitedInputStream.java:132) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at java.io.FilterInputStream.read(FilterInputStream.java:107) ~[?:1.8.0_161]
    at org.apache.tomcat.util.http.fileupload.util.Streams.copy(Streams.java:98) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.tomcat.util.http.fileupload.util.Streams.copy(Streams.java:68) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.tomcat.util.http.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:293) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.connector.Request.parseParts(Request.java:2803) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.connector.Request.parseParameters(Request.java:3178) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.connector.Request.getParameter(Request.java:1109) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.apache.catalina.connector.RequestFacade.getParameter(RequestFacade.java:380) ~[tomcat-embed-core-8.0.46.jar:8.0.46]
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:75) ~[spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    ... 27 more

Here is part of code from frontend app which responding for file uploading:

this.uploader.onAfterAddingFile = (fileItem) => {
            this.setAuthTokenHeader();
            this.uploader.uploadItem(fileItem);
            this.uploadedFiles.push({
                id: null,
                originalName: fileItem.file.name,
                attachmentName: null
            });
            this.changedCallback(this.uploadedFiles);
            this.scrollToFilesUX();
        };

I have no idea what can be wrong. Thanks in advance.

PS: Also I noticed, if @ExceptionHandler method type is void and it do nothing but logging error everything works as it should

UPDATE

I thought it's doesn't matter, because controller even not called, as I know it happens because file uploads to the server at first (and exception throws on this step) and then controller executes. But here is my controller method (class annotated as @RestController):

@PostMapping(value = "/attachments")
    public ResponseData put(@RequestParam(value = "storeFile", required = false) MultipartFile multipartFile,
                            @RequestParam(required = false) List<DocumentType> documentTypes) throws IOException {
        try {
            return ResponseData.with(attachmentService.uploadTempFile(multipartFile, documentTypes));
        } catch (AttachmentValidationException e) {
            return ResponseData.with(e.getTempFileName(), e.getMessages(), HttpStatus.NOT_ACCEPTABLE);
        }
    }

UPDATE 2

Here is chaned @ExceptionHandler method. In that case Spring Boot doesn't log any error (that's good). But now, exception logs as WARN two times. If early it was my WARN log + Spring Boot duplicate ERROR log, now it's my WARN log which duplicates (with different time)

@ExceptionHandler(value = MultipartException.class)
    public void handleFileUploadingError(HttpServletResponse response, Exception exception) {
        log.warn("Failed to upload attachment", exception);
        try {
            response.sendError(Response.SC_INTERNAL_SERVER_ERROR, exception.getMessage());
        } catch (IOException e) {
            log.error("Failed to send error response", e);
        }
    }
Dmitry Stoyanov
  • 29
  • 1
  • 1
  • 5
  • Could you please share your controller mapping and function definition?. Also great if you share us network tab request from browser – Barath Oct 10 '18 at 13:00
  • Can you make it public "public ResponseData handleFileUploadingError" – kj007 Oct 10 '18 at 16:09
  • @kj007 unfortunately it's doesn't help. Spring Boot still log (and duplicate) that error sometimes – Dmitry Stoyanov Oct 11 '18 at 05:49
  • @DmitryStoyanov please catch @ExceptionHandler({MultipartException.class, IOFileUploadException}) should help you to log only on controller advice as might be IOFileUploadException called first before MultipartException in order.. – kj007 Oct 11 '18 at 06:07
  • @kj007 unfortunately it's doesn't help too. Look at my Update 2, maybe you can find hints there – Dmitry Stoyanov Oct 11 '18 at 07:30
  • Can you check In your method Exception is null or something or just use MultipartException ex in your method instead of Exception – kj007 Oct 11 '18 at 07:52
  • Seems sendError method throws error so it’s going in cache to log again warn message – kj007 Oct 11 '18 at 07:54

5 Answers5

0

I think this behaviour is correct, since you're still logging the exception here (the e param):

log.warn("Failed to upload attachment", e);

Notice that your exception changed from ERROR to WARN, as you did log as a warn in yout exception handler.

inafalcao
  • 1,415
  • 1
  • 11
  • 25
  • Maybe I didn't understand you, but my trouble is that after my WARN log, Spring Boot sometime also logged ERROR and it seems like I didn't catch that error by my @ExceptionHandler. Resolution for me will be ExceptionHandler error log only – Dmitry Stoyanov Oct 10 '18 at 14:31
  • @DmitryStoyanov I get it now. – inafalcao Oct 10 '18 at 14:47
0

I have debugged your controller advice and found method is called twice so its logging warning twice so seems sendError again send response back to handle exception..(not sure)..

Here are two solutions I can see and works for me as checked..

Approach 1 return ResponseEntity

@ExceptionHandler(MultipartException.class)
    public ResponseEntity handleFileUploadingError(MultipartException exception) {
        log.warn("Failed to upload attachment", exception);
        return new ResponseEntity<>(exception.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);

    }

Approach 2 which similar but return String

    @ExceptionHandler(value = MultipartException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    public String handleFileUploadingError(MultipartException exception) {
        logger.warn("Failed to upload attachment", exception);
        return exception.getMessage();
    }
kj007
  • 6,073
  • 4
  • 29
  • 47
  • Both of this solutions ain't resolve my trouble. Spring Boot **sometime** still log error. It looks like exception throws at twice but catches only one time, in my UPDATE 2 exception catches two times. But I have the next question, why this exception throws two times, maybe some troubles in responding? Because if @ExceptionHandler is void and implement no logic instead "log.warn(...);" all works as it should (error logged at once with WARN) – Dmitry Stoyanov Oct 11 '18 at 11:18
0

Disable csrf in your WebSecurityConfigurerAdapter I am not sure what is this but one of my expert friend said disable it for sending multipart resources.

public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
    http.csrf().disable();
}
Amir
  • 1,638
  • 19
  • 26
0

Check if your form has method="post" enctype="multipart/form-data". Also, check you have provided these settings in the ajax parameters.

type:'post',
processData : false,
contentType : false,
mimeType: 'multipart/form-data', 
mumbasa
  • 632
  • 7
  • 11
-1

After many tries, after many tears. I randomly found how to fix this error. All troubles was in response from server side. If response do not contains body - everything works fine, else everything works as in header of this topic. There is my ExceptionHandler which resolve topic case:

@ExceptionHandler(value = MultipartException.class)
public ResponseData handleFileUploadingError(Exception exception) {
    log.warn("Failed to upload attachment", exception);
    return ResponseData.with(HttpStatus.BAD_REQUEST);
}
Dmitry Stoyanov
  • 29
  • 1
  • 1
  • 5