Skip to content

Commit eab730d

Browse files
committed
RestClientException should not be considered as client disconnected exception
Fix spring-projectsGH-34264 Signed-off-by: Yanming Zhou <[email protected]>
1 parent 6873427 commit eab730d

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

spring-web/src/main/java/org/springframework/web/util/DisconnectedClientHelper.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@
2424

2525
import org.springframework.core.NestedExceptionUtils;
2626
import org.springframework.util.Assert;
27+
import org.springframework.web.client.RestClientException;
2728

2829
/**
2930
* Utility methods to assist with identifying and logging exceptions that indicate
@@ -32,6 +33,7 @@
3233
* and a full stacktrace at TRACE level.
3334
*
3435
* @author Rossen Stoyanchev
36+
* @author Yanming Zhou
3537
* @since 6.1
3638
*/
3739
public class DisconnectedClientHelper {
@@ -83,6 +85,13 @@ else if (logger.isDebugEnabled()) {
8385
* </ul>
8486
*/
8587
public static boolean isClientDisconnectedException(Throwable ex) {
88+
Throwable cause = ex;
89+
while (cause != null) {
90+
if (cause instanceof RestClientException) {
91+
return false;
92+
}
93+
cause = cause.getCause();
94+
}
8695
String message = NestedExceptionUtils.getMostSpecificCause(ex).getMessage();
8796
if (message != null) {
8897
String text = message.toLowerCase(Locale.ROOT);

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/support/DefaultHandlerExceptionResolverTests.java

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717
package org.springframework.web.servlet.mvc.support;
1818

1919
import java.lang.reflect.Method;
20+
import java.net.SocketException;
2021
import java.util.Arrays;
2122
import java.util.Collections;
2223

@@ -42,6 +43,7 @@
4243
import org.springframework.web.bind.MissingPathVariableException;
4344
import org.springframework.web.bind.MissingServletRequestParameterException;
4445
import org.springframework.web.bind.ServletRequestBindingException;
46+
import org.springframework.web.client.RestClientException;
4547
import org.springframework.web.context.request.async.AsyncRequestTimeoutException;
4648
import org.springframework.web.multipart.MaxUploadSizeExceededException;
4749
import org.springframework.web.multipart.support.MissingServletRequestPartException;
@@ -60,6 +62,7 @@
6062
*
6163
* @author Arjen Poutsma
6264
* @author Sebastien Deleuze
65+
* @author Yanming Zhou
6366
*/
6467
class DefaultHandlerExceptionResolverTests {
6568

@@ -248,6 +251,24 @@ void handleMaxUploadSizeExceededException() {
248251
assertThat(response.getErrorMessage()).isEqualTo("Maximum upload size exceeded");
249252
}
250253

254+
@Test
255+
void handleClientDisconnectedException() {
256+
SocketException ex = new SocketException("Connection reset");
257+
ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex);
258+
assertThat(mav).as("No ModelAndView returned").isNotNull();
259+
}
260+
261+
@Test
262+
void handleRestClientExceptionHasConnectionResetMessage() {
263+
RestClientException ex = new RestClientException("I/O error", new SocketException("Connection reset"));
264+
ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex);
265+
assertThat(mav).as("ModelAndView is returned").isNull();
266+
267+
Exception exception = new Exception(ex.getMessage(), ex);
268+
mav = exceptionResolver.resolveException(request, response, null, exception);
269+
assertThat(mav).as("ModelAndView is returned").isNull();
270+
}
271+
251272
@Test
252273
void customModelAndView() {
253274
ModelAndView expected = new ModelAndView();

0 commit comments

Comments
 (0)