Adapt to change in Framework's disconnected client detection

See gh-38666
This commit is contained in:
Andy Wilkinson 2023-12-06 14:18:40 +00:00
parent ad5b844e1f
commit 6dff3c5978
2 changed files with 2 additions and 33 deletions

View File

@ -18,10 +18,8 @@ package org.springframework.boot.autoconfigure.web.reactive.error;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.logging.Log;
import reactor.core.publisher.Mono;
@ -33,7 +31,6 @@ import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.boot.web.reactive.error.ErrorAttributes;
import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;
import org.springframework.context.ApplicationContext;
import org.springframework.core.NestedExceptionUtils;
import org.springframework.core.io.Resource;
import org.springframework.core.log.LogMessage;
import org.springframework.http.HttpLogging;
@ -49,6 +46,7 @@ import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.reactive.result.view.ViewResolver;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.util.DisconnectedClientHelper;
import org.springframework.web.util.HtmlUtils;
/**
@ -61,19 +59,6 @@ import org.springframework.web.util.HtmlUtils;
*/
public abstract class AbstractErrorWebExceptionHandler implements ErrorWebExceptionHandler, InitializingBean {
/**
* Currently duplicated from Spring Web's DisconnectedClientHelper.
*/
private static final Set<String> DISCONNECTED_CLIENT_EXCEPTIONS;
static {
Set<String> exceptions = new HashSet<>();
exceptions.add("ClientAbortException");
exceptions.add("EOFException");
exceptions.add("EofException");
DISCONNECTED_CLIENT_EXCEPTIONS = Collections.unmodifiableSet(exceptions);
}
private static final Log logger = HttpLogging.forLogName(AbstractErrorWebExceptionHandler.class);
private final ApplicationContext applicationContext;
@ -305,13 +290,7 @@ public abstract class AbstractErrorWebExceptionHandler implements ErrorWebExcept
}
private boolean isDisconnectedClientError(Throwable ex) {
return DISCONNECTED_CLIENT_EXCEPTIONS.contains(ex.getClass().getSimpleName())
|| isDisconnectedClientErrorMessage(NestedExceptionUtils.getMostSpecificCause(ex).getMessage());
}
private boolean isDisconnectedClientErrorMessage(String message) {
message = (message != null) ? message.toLowerCase() : "";
return (message.contains("broken pipe") || message.contains("connection reset by peer"));
return DisconnectedClientHelper.isClientDisconnectedException(ex);
}
/**

View File

@ -33,12 +33,10 @@ import org.springframework.http.codec.HttpMessageReader;
import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
import org.springframework.mock.web.server.MockServerWebExchange;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.result.view.View;
import org.springframework.web.reactive.result.view.ViewResolver;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.util.DisconnectedClientHelper;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
@ -54,14 +52,6 @@ import static org.mockito.Mockito.mock;
*/
class DefaultErrorWebExceptionHandlerTests {
@Test
void disconnectedClientExceptionsMatchesFramework() {
Object errorHandlers = ReflectionTestUtils.getField(AbstractErrorWebExceptionHandler.class,
"DISCONNECTED_CLIENT_EXCEPTIONS");
Object webHandlers = ReflectionTestUtils.getField(DisconnectedClientHelper.class, "EXCEPTION_TYPE_NAMES");
assertThat(errorHandlers).isNotNull().isEqualTo(webHandlers);
}
@Test
void nonStandardErrorStatusCodeShouldNotFail() {
ErrorAttributes errorAttributes = mock(ErrorAttributes.class);