From b0f5fb51fc0a5b80ac5f3cfa10747f5b2147d3bb Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 14 Jun 2022 16:38:39 +0200 Subject: [PATCH] Adapt to deprecation of NestedServletException --- .../actuate/metrics/web/servlet/WebMvcMetricsFilter.java | 7 +++---- .../servlet/LongTaskTimingHandlerInterceptorTests.java | 7 +++---- .../metrics/web/servlet/WebMvcMetricsFilterTests.java | 9 ++++----- .../web/servlet/WebMvcMetricsIntegrationTests.java | 6 +++--- .../WebMvcTestAllControllersIntegrationTests.java | 5 ++--- .../boot/web/servlet/support/ErrorPageFilter.java | 5 ++--- .../boot/web/servlet/support/ErrorPageFilterTests.java | 7 +++---- 7 files changed, 20 insertions(+), 26 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/servlet/WebMvcMetricsFilter.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/servlet/WebMvcMetricsFilter.java index fae5365bd63..7cbd7c8183e 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/servlet/WebMvcMetricsFilter.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/servlet/WebMvcMetricsFilter.java @@ -40,7 +40,6 @@ import org.springframework.web.filter.OncePerRequestFilter; import org.springframework.web.method.HandlerMethod; import org.springframework.web.servlet.DispatcherServlet; import org.springframework.web.servlet.HandlerMapping; -import org.springframework.web.util.NestedServletException; /** * Intercepts incoming HTTP requests handled by Spring MVC handlers and records metrics @@ -104,13 +103,13 @@ public class WebMvcMetricsFilter extends OncePerRequestFilter { } catch (Exception ex) { response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value()); - record(timingContext, request, response, unwrapNestedServletException(ex)); + record(timingContext, request, response, unwrapServletException(ex)); throw ex; } } - private Throwable unwrapNestedServletException(Throwable ex) { - return (ex instanceof NestedServletException) ? ex.getCause() : ex; + private Throwable unwrapServletException(Throwable ex) { + return (ex instanceof ServletException) ? ex.getCause() : ex; } private TimingContext startAndAttachTimingContext(HttpServletRequest request) { diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/servlet/LongTaskTimingHandlerInterceptorTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/servlet/LongTaskTimingHandlerInterceptorTests.java index 68420af8b95..b479f310e84 100644 --- a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/servlet/LongTaskTimingHandlerInterceptorTests.java +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/servlet/LongTaskTimingHandlerInterceptorTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2021 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,6 +28,7 @@ import io.micrometer.core.instrument.MeterRegistry; import io.micrometer.core.instrument.MockClock; import io.micrometer.core.instrument.simple.SimpleConfig; import io.micrometer.core.instrument.simple.SimpleMeterRegistry; +import jakarta.servlet.ServletException; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -49,7 +50,6 @@ import org.springframework.web.context.WebApplicationContext; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; -import org.springframework.web.util.NestedServletException; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; @@ -92,8 +92,7 @@ class LongTaskTimingHandlerInterceptorTests { MvcResult result = this.mvc.perform(get("/api/c1/completableFutureException")) .andExpect(request().asyncStarted()).andReturn(); assertThat(this.registry.get("my.long.request.exception").longTaskTimer().activeTasks()).isEqualTo(1); - assertThatExceptionOfType(NestedServletException.class) - .isThrownBy(() -> this.mvc.perform(asyncDispatch(result))) + assertThatExceptionOfType(ServletException.class).isThrownBy(() -> this.mvc.perform(asyncDispatch(result))) .withRootCauseInstanceOf(RuntimeException.class); assertThat(this.registry.get("my.long.request.exception").longTaskTimer().activeTasks()).isEqualTo(0); } diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/servlet/WebMvcMetricsFilterTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/servlet/WebMvcMetricsFilterTests.java index 5d523a2f6bb..76e230c9909 100644 --- a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/servlet/WebMvcMetricsFilterTests.java +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/servlet/WebMvcMetricsFilterTests.java @@ -78,7 +78,6 @@ import org.springframework.web.filter.OncePerRequestFilter; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter; -import org.springframework.web.util.NestedServletException; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatCode; @@ -191,7 +190,8 @@ class WebMvcMetricsFilterTests { assertThatCode(() -> this.mvc .perform(get("/api/filterError").header(CustomBehaviorFilter.TEST_SERVLET_EXCEPTION_HEADER, "throw"))) .isInstanceOf(ServletException.class); - Id meterId = this.registry.get("http.server.requests").tags("exception", "ServletException").timer().getId(); + Id meterId = this.registry.get("http.server.requests").tags("exception", "IllegalStateException").timer() + .getId(); assertThat(meterId.getTag("status")).isEqualTo("500"); } @@ -253,8 +253,7 @@ class WebMvcMetricsFilterTests { void asyncRequestThatThrowsUncheckedException() throws Exception { MvcResult result = this.mvc.perform(get("/api/c1/completableFutureException")) .andExpect(request().asyncStarted()).andReturn(); - assertThatExceptionOfType(NestedServletException.class) - .isThrownBy(() -> this.mvc.perform(asyncDispatch(result))) + assertThatExceptionOfType(ServletException.class).isThrownBy(() -> this.mvc.perform(asyncDispatch(result))) .withRootCauseInstanceOf(RuntimeException.class); assertThat(this.registry.get("http.server.requests").tags("uri", "/api/c1/completableFutureException").timer() .count()).isEqualTo(1); @@ -553,7 +552,7 @@ class WebMvcMetricsFilterTests { return; } if (request.getHeader(TEST_SERVLET_EXCEPTION_HEADER) != null) { - throw new ServletException(); + throw new ServletException(new IllegalStateException()); } filterChain.doFilter(request, response); } diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/servlet/WebMvcMetricsIntegrationTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/servlet/WebMvcMetricsIntegrationTests.java index e7d4d383b53..b4cb6b969a0 100644 --- a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/servlet/WebMvcMetricsIntegrationTests.java +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/servlet/WebMvcMetricsIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2021 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,6 +22,7 @@ import io.micrometer.core.instrument.MeterRegistry; import io.micrometer.core.instrument.MockClock; import io.micrometer.core.instrument.simple.SimpleConfig; import io.micrometer.core.instrument.simple.SimpleMeterRegistry; +import jakarta.servlet.ServletException; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -44,7 +45,6 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.servlet.config.annotation.EnableWebMvc; -import org.springframework.web.util.NestedServletException; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; @@ -86,7 +86,7 @@ class WebMvcMetricsIntegrationTests { @Test void rethrownExceptionIsRecordedInMetricTag() { - assertThatExceptionOfType(NestedServletException.class) + assertThatExceptionOfType(ServletException.class) .isThrownBy(() -> this.mvc.perform(get("/api/rethrownError")).andReturn()); assertThat(this.registry.get("http.server.requests").tags("exception", "Exception2", "status", "500").timer() .count()).isEqualTo(1L); diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/mockmvc/WebMvcTestAllControllersIntegrationTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/mockmvc/WebMvcTestAllControllersIntegrationTests.java index 0c9238c0a19..93ea749a7c9 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/mockmvc/WebMvcTestAllControllersIntegrationTests.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/mockmvc/WebMvcTestAllControllersIntegrationTests.java @@ -16,6 +16,7 @@ package org.springframework.boot.test.autoconfigure.web.servlet.mockmvc; +import jakarta.servlet.ServletException; import jakarta.validation.ConstraintViolationException; import org.junit.jupiter.api.Test; @@ -24,7 +25,6 @@ import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.web.servlet.error.ErrorAttributes; import org.springframework.security.test.context.support.WithMockUser; import org.springframework.test.web.servlet.MockMvc; -import org.springframework.web.util.NestedServletException; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; @@ -70,8 +70,7 @@ class WebMvcTestAllControllersIntegrationTests { @Test void shouldRunValidationFailure() { - assertThatExceptionOfType(NestedServletException.class) - .isThrownBy(() -> this.mvc.perform(get("/three/invalid"))) + assertThatExceptionOfType(ServletException.class).isThrownBy(() -> this.mvc.perform(get("/three/invalid"))) .withCauseInstanceOf(ConstraintViolationException.class); } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/support/ErrorPageFilter.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/support/ErrorPageFilter.java index d645ad0b338..48c38315f69 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/support/ErrorPageFilter.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/support/ErrorPageFilter.java @@ -44,7 +44,6 @@ import org.springframework.boot.web.server.ErrorPageRegistry; import org.springframework.core.Ordered; import org.springframework.util.ClassUtils; import org.springframework.web.filter.OncePerRequestFilter; -import org.springframework.web.util.NestedServletException; /** * A Servlet {@link Filter} that provides an {@link ErrorPageRegistry} for non-embedded @@ -133,8 +132,8 @@ public class ErrorPageFilter implements Filter, ErrorPageRegistry, Ordered { } catch (Throwable ex) { Throwable exceptionToHandle = ex; - if (ex instanceof NestedServletException) { - Throwable rootCause = ((NestedServletException) ex).getRootCause(); + if (ex instanceof ServletException) { + Throwable rootCause = ((ServletException) ex).getRootCause(); if (rootCause != null) { exceptionToHandle = rootCause; } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/support/ErrorPageFilterTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/support/ErrorPageFilterTests.java index 68994eb51b9..d27d5715a16 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/support/ErrorPageFilterTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/support/ErrorPageFilterTests.java @@ -47,7 +47,6 @@ import org.springframework.web.context.request.async.DeferredResult; import org.springframework.web.context.request.async.StandardServletAsyncWebRequest; import org.springframework.web.context.request.async.WebAsyncManager; import org.springframework.web.context.request.async.WebAsyncUtils; -import org.springframework.web.util.NestedServletException; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.BDDMockito.given; @@ -367,11 +366,11 @@ class ErrorPageFilterTests { } @Test - void nestedServletExceptionIsUnwrapped() throws Exception { + void servletExceptionIsUnwrapped() throws Exception { this.filter.addErrorPages(new ErrorPage(RuntimeException.class, "/500")); this.chain = new TestFilterChain((request, response, chain) -> { chain.call(); - throw new NestedServletException("Wrapper", new RuntimeException("BAD")); + throw new ServletException("Wrapper", new RuntimeException("BAD")); }); this.filter.doFilter(this.request, this.response, this.chain); assertThat(((HttpServletResponseWrapper) this.chain.getResponse()).getStatus()).isEqualTo(500); @@ -388,7 +387,7 @@ class ErrorPageFilterTests { } @Test - void nestedServletExceptionWithNoCause() throws Exception { + void servletExceptionWithNoCause() throws Exception { this.filter.addErrorPages(new ErrorPage(MissingServletRequestParameterException.class, "/500")); this.chain = new TestFilterChain((request, response, chain) -> { chain.call();