我已经展示了here的场景。但是,我将后端(接收文件并保存的那一面)配置为接受最大10 MB的文件。
我的问题是,我不知道如何处理发送大于10 MB的文件时出现的错误。
这是前端出现的错误:
2018-11-19 16:20:51.317 INFO 4868 --- [o-10082-exec-10] o.apache.http.impl.execchain.RetryExec : I/O exception (java.net.SocketException) caught when processing request to {}->http://localhost:9082: Software caused connection abort: socket write error
2018-11-19 16:20:51.317 WARN 4868 --- [o-10082-exec-10] o.s.c.n.z.filters.post.SendErrorFilter : Error during filtering
com.netflix.zuul.exception.ZuulException: null
at org.springframework.cloud.netflix.zuul.util.ZuulRuntimeException.<init>(ZuulRuntimeException.java:33) ~[spring-cloud-netflix-core-1.4.4.RELEASE.jar:1.4.4.RELEASE]
at org.springframework.cloud.netflix.zuul.filters.route.SimpleHostRoutingFilter.run(SimpleHostRoutingFilter.java:207) ~[spring-cloud-netflix-core-1.4.4.RELEASE.jar:1.4.4.RELEASE]
at com.netflix.zuul.ZuulFilter.runFilter(ZuulFilter.java:112) ~[zuul-core-1.3.0.jar:1.3.0]
at com.netflix.zuul.FilterProcessor.processZuulFilter(FilterProcessor.java:193) ~[zuul-core-1.3.0.jar:1.3.0]
[...]
Caused by: org.apache.http.client.ClientProtocolException: null
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:187) ~[httpclient-4.5.3.jar:4.5.3]
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:118) ~[httpclient-4.5.3.jar:4.5.3]
at org.springframework.cloud.netflix.zuul.filters.route.SimpleHostRoutingFilter.forwardRequest(SimpleHostRoutingFilter.java:332) ~[spring-cloud-netflix-core-1.4.4.RELEASE.jar:1.4.4.RELEASE]
at org.springframework.cloud.netflix.zuul.filters.route.SimpleHostRoutingFilter.forward(SimpleHostRoutingFilter.java:251) ~[spring-cloud-netflix-core-1.4.4.RELEASE.jar:1.4.4.RELEASE]
[...]
Caused by: org.apache.http.client.NonRepeatableRequestException: Cannot retry request with a non-repeatable request entity
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:108) ~[httpclient-4.5.3.jar:4.5.3]
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185) ~[httpclient-4.5.3.jar:4.5.3]
[...]
Caused by: java.net.SocketException: Software caused connection abort: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method) ~[na:1.8.0_92]
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109) ~[na:1.8.0_92]
at java.net.SocketOutputStream.write(SocketOutputStream.java:153) ~[na:1.8.0_92]
at org.apache.http.impl.io.SessionOutputBufferImpl.streamWrite(SessionOutputBufferImpl.java:124) ~[httpcore-4.4.6.jar:4.4.6]
[...]
可以很好地处理后端错误,如下所示:
16:20:39.764 WARN [http-nio-9082-exec-1] o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Resolved exception caused by Handler execution: org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size of 20971520 bytes exceeded; nested exception is org.apache.commons.fileupload.FileUploadBase$FileSizeLimitExceededException: The field file exceeds its maximum permitted size of 20971520 bytes.
有什么主意我应该如何处理后端错误?
当然,我也会在Frontend上验证文件大小,但是我需要能够优雅地处理该异常。
谢谢!
答案 0 :(得分:0)
您可以根据需要处理的异常创建自定义错误处理程序。
@ControllerAdvice
public class EntityExceptionHandler
extends ResponseEntityExceptionHandler {
@ExceptionHandler(value = { ClientProtocolException.class })
protected ResponseEntity<Object> handleClientProtocol(ClientProtocolException ex, WebRequest request) {
return handleExceptionInternal(ex, "Here you need to put the error message",
new HttpHeaders(), HttpStatus.BAD_REQUEST, request);
}
}
您可以在error handlers
中查看处理异常的方法