Skip to content

Commit 68f84d8

Browse files
committed
RestClientException is not ClientDisconnectedException
Fix spring-projectsGH-34264 Signed-off-by: Yanming Zhou <zhouyanming@gmail.com>
1 parent 6873427 commit 68f84d8

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

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

+6-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,9 @@ else if (logger.isDebugEnabled()) {
8385
* </ul>
8486
*/
8587
public static boolean isClientDisconnectedException(Throwable ex) {
88+
if (ex instanceof RestClientException) {
89+
return false;
90+
}
8691
String message = NestedExceptionUtils.getMostSpecificCause(ex).getMessage();
8792
if (message != null) {
8893
String text = message.toLowerCase(Locale.ROOT);

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

+18-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,20 @@ 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+
251268
@Test
252269
void customModelAndView() {
253270
ModelAndView expected = new ModelAndView();

0 commit comments

Comments
 (0)