Adapt to deprecation of NestedServletException

This commit is contained in:
Stephane Nicoll
2022-06-14 16:38:39 +02:00
parent 6894f561ce
commit b0f5fb51fc
7 changed files with 20 additions and 26 deletions

View File

@@ -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) {

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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);