Skip to content

Commit f4c2714

Browse files
Support server.error config in management context
Prior to this commit, the ManagementErrorEndpoint used to handle error responses for the management servlet excluded stacktrace and exception message details from the response unconditionally. With this commit, the endpoint honors the `server.error.include-stacktrace` and `server.error.include-details` properties to conditionally include error details for consistency with non-management error handling. Fixes gh-20989
1 parent 3c388cf commit f4c2714

File tree

4 files changed

+178
-14
lines changed

4 files changed

+178
-14
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/servlet/ManagementErrorEndpoint.java

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.util.Map;
2020

21+
import org.springframework.boot.autoconfigure.web.ErrorProperties;
2122
import org.springframework.boot.web.servlet.error.ErrorAttributes;
2223
import org.springframework.boot.web.servlet.error.ErrorController;
2324
import org.springframework.stereotype.Controller;
@@ -32,22 +33,57 @@
3233
* but because of the way the handler mappings are set up it will not be detected.
3334
*
3435
* @author Dave Syer
36+
* @author Scott Frederick
3537
* @since 2.0.0
3638
*/
3739
@Controller
3840
public class ManagementErrorEndpoint {
3941

4042
private final ErrorAttributes errorAttributes;
4143

42-
public ManagementErrorEndpoint(ErrorAttributes errorAttributes) {
44+
private final ErrorProperties errorProperties;
45+
46+
public ManagementErrorEndpoint(ErrorAttributes errorAttributes, ErrorProperties errorProperties) {
4347
Assert.notNull(errorAttributes, "ErrorAttributes must not be null");
48+
Assert.notNull(errorProperties, "ErrorProperties must not be null");
4449
this.errorAttributes = errorAttributes;
50+
this.errorProperties = errorProperties;
4551
}
4652

4753
@RequestMapping("${server.error.path:${error.path:/error}}")
4854
@ResponseBody
4955
public Map<String, Object> invoke(ServletWebRequest request) {
50-
return this.errorAttributes.getErrorAttributes(request, false, false);
56+
return this.errorAttributes.getErrorAttributes(request, includeStackTrace(request), includeDetails(request));
57+
}
58+
59+
private boolean includeStackTrace(ServletWebRequest request) {
60+
ErrorProperties.IncludeStacktrace include = this.errorProperties.getIncludeStacktrace();
61+
if (include == ErrorProperties.IncludeStacktrace.ALWAYS) {
62+
return true;
63+
}
64+
if (include == ErrorProperties.IncludeStacktrace.ON_TRACE_PARAM) {
65+
return getBooleanParameter(request, "trace");
66+
}
67+
return false;
68+
}
69+
70+
private boolean includeDetails(ServletWebRequest request) {
71+
ErrorProperties.IncludeDetails include = this.errorProperties.getIncludeDetails();
72+
if (include == ErrorProperties.IncludeDetails.ALWAYS) {
73+
return true;
74+
}
75+
if (include == ErrorProperties.IncludeDetails.ON_DETAILS_PARAM) {
76+
return getBooleanParameter(request, "details");
77+
}
78+
return false;
79+
}
80+
81+
protected boolean getBooleanParameter(ServletWebRequest request, String parameterName) {
82+
String parameter = request.getParameter(parameterName);
83+
if (parameter == null) {
84+
return false;
85+
}
86+
return !"false".equalsIgnoreCase(parameter);
5187
}
5288

5389
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/servlet/WebMvcEndpointChildContextConfiguration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ class WebMvcEndpointChildContextConfiguration {
6161
*/
6262
@Bean
6363
@ConditionalOnBean(ErrorAttributes.class)
64-
ManagementErrorEndpoint errorEndpoint(ErrorAttributes errorAttributes) {
65-
return new ManagementErrorEndpoint(errorAttributes);
64+
ManagementErrorEndpoint errorEndpoint(ErrorAttributes errorAttributes, ServerProperties serverProperties) {
65+
return new ManagementErrorEndpoint(errorAttributes, serverProperties.getError());
6666
}
6767

6868
@Bean
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright 2012-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.autoconfigure.web.servlet;
18+
19+
import java.util.Map;
20+
21+
import org.junit.jupiter.api.BeforeEach;
22+
import org.junit.jupiter.api.Test;
23+
24+
import org.springframework.boot.autoconfigure.web.ErrorProperties;
25+
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
26+
import org.springframework.boot.web.servlet.error.ErrorAttributes;
27+
import org.springframework.mock.web.MockHttpServletRequest;
28+
import org.springframework.web.context.request.ServletWebRequest;
29+
30+
import static org.assertj.core.api.Assertions.assertThat;
31+
32+
/**
33+
* Tests for {@link ManagementErrorEndpoint}.
34+
*
35+
* @author Scott Frederick
36+
*/
37+
class ManagementErrorEndpointTests {
38+
39+
private final ErrorAttributes errorAttributes = new DefaultErrorAttributes();
40+
41+
private final ErrorProperties errorProperties = new ErrorProperties();
42+
43+
private final MockHttpServletRequest request = new MockHttpServletRequest();
44+
45+
@BeforeEach
46+
void setUp() {
47+
this.request.setAttribute("javax.servlet.error.exception", new RuntimeException("test exception"));
48+
}
49+
50+
@Test
51+
void errorResponseNeverDetails() {
52+
ManagementErrorEndpoint endpoint = new ManagementErrorEndpoint(this.errorAttributes, this.errorProperties);
53+
Map<String, Object> response = endpoint.invoke(new ServletWebRequest(new MockHttpServletRequest()));
54+
assertThat(response).containsEntry("message", "An error occurred while processing the request");
55+
assertThat(response).doesNotContainKey("trace");
56+
}
57+
58+
@Test
59+
void errorResponseAlwaysDetails() {
60+
this.errorProperties.setIncludeStacktrace(ErrorProperties.IncludeStacktrace.ALWAYS);
61+
this.errorProperties.setIncludeDetails(ErrorProperties.IncludeDetails.ALWAYS);
62+
this.request.addParameter("trace", "false");
63+
this.request.addParameter("details", "false");
64+
ManagementErrorEndpoint endpoint = new ManagementErrorEndpoint(this.errorAttributes, this.errorProperties);
65+
Map<String, Object> response = endpoint.invoke(new ServletWebRequest(this.request));
66+
assertThat(response).containsEntry("message", "test exception");
67+
assertThat(response).hasEntrySatisfying("trace",
68+
(value) -> assertThat(value).asString().startsWith("java.lang.RuntimeException: test exception"));
69+
}
70+
71+
@Test
72+
void errorResponseParamsAbsent() {
73+
this.errorProperties.setIncludeStacktrace(ErrorProperties.IncludeStacktrace.ON_TRACE_PARAM);
74+
this.errorProperties.setIncludeDetails(ErrorProperties.IncludeDetails.ON_DETAILS_PARAM);
75+
ManagementErrorEndpoint endpoint = new ManagementErrorEndpoint(this.errorAttributes, this.errorProperties);
76+
Map<String, Object> response = endpoint.invoke(new ServletWebRequest(this.request));
77+
assertThat(response).containsEntry("message", "An error occurred while processing the request");
78+
assertThat(response).doesNotContainKey("trace");
79+
}
80+
81+
@Test
82+
void errorResponseParamsTrue() {
83+
this.errorProperties.setIncludeStacktrace(ErrorProperties.IncludeStacktrace.ON_TRACE_PARAM);
84+
this.errorProperties.setIncludeDetails(ErrorProperties.IncludeDetails.ON_DETAILS_PARAM);
85+
this.request.addParameter("trace", "true");
86+
this.request.addParameter("details", "true");
87+
ManagementErrorEndpoint endpoint = new ManagementErrorEndpoint(this.errorAttributes, this.errorProperties);
88+
Map<String, Object> response = endpoint.invoke(new ServletWebRequest(this.request));
89+
assertThat(response).containsEntry("message", "test exception");
90+
assertThat(response).hasEntrySatisfying("trace",
91+
(value) -> assertThat(value).asString().startsWith("java.lang.RuntimeException: test exception"));
92+
}
93+
94+
@Test
95+
void errorResponseParamsFalse() {
96+
this.errorProperties.setIncludeStacktrace(ErrorProperties.IncludeStacktrace.ON_TRACE_PARAM);
97+
this.errorProperties.setIncludeDetails(ErrorProperties.IncludeDetails.ON_DETAILS_PARAM);
98+
this.request.addParameter("trace", "false");
99+
this.request.addParameter("details", "false");
100+
ManagementErrorEndpoint endpoint = new ManagementErrorEndpoint(this.errorAttributes, this.errorProperties);
101+
Map<String, Object> response = endpoint.invoke(new ServletWebRequest(this.request));
102+
assertThat(response).containsEntry("message", "An error occurred while processing the request");
103+
assertThat(response).doesNotContainKey("trace");
104+
}
105+
106+
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/servlet/WebMvcEndpointChildContextConfigurationIntegrationTests.java

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure.web.servlet;
1818

19+
import java.util.Map;
20+
1921
import org.junit.jupiter.api.Test;
2022

2123
import org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration;
@@ -45,23 +47,43 @@
4547
*/
4648
class WebMvcEndpointChildContextConfigurationIntegrationTests {
4749

50+
private final WebApplicationContextRunner runner = new WebApplicationContextRunner(
51+
AnnotationConfigServletWebServerApplicationContext::new)
52+
.withConfiguration(AutoConfigurations.of(ManagementContextAutoConfiguration.class,
53+
ServletWebServerFactoryAutoConfiguration.class,
54+
ServletManagementContextAutoConfiguration.class, WebEndpointAutoConfiguration.class,
55+
EndpointAutoConfiguration.class, DispatcherServletAutoConfiguration.class,
56+
ErrorMvcAutoConfiguration.class))
57+
.withUserConfiguration(FailingEndpoint.class)
58+
.withInitializer(new ServerPortInfoApplicationContextInitializer()).withPropertyValues(
59+
"server.port=0", "management.server.port=0", "management.endpoints.web.exposure.include=*");
60+
4861
@Test // gh-17938
62+
@SuppressWarnings("unchecked")
4963
void errorPageAndErrorControllerAreUsed() {
50-
new WebApplicationContextRunner(AnnotationConfigServletWebServerApplicationContext::new)
51-
.withConfiguration(AutoConfigurations.of(ManagementContextAutoConfiguration.class,
52-
ServletWebServerFactoryAutoConfiguration.class, ServletManagementContextAutoConfiguration.class,
53-
WebEndpointAutoConfiguration.class, EndpointAutoConfiguration.class,
54-
DispatcherServletAutoConfiguration.class, ErrorMvcAutoConfiguration.class))
55-
.withUserConfiguration(FailingEndpoint.class)
56-
.withInitializer(new ServerPortInfoApplicationContextInitializer()).withPropertyValues("server.port=0",
57-
"management.server.port=0", "management.endpoints.web.exposure.include=*")
64+
this.runner.run((context) -> {
65+
String port = context.getEnvironment().getProperty("local.management.port");
66+
WebClient client = WebClient.create("http://localhost:" + port);
67+
ClientResponse response = client.get().uri("actuator/fail").accept(MediaType.APPLICATION_JSON).exchange()
68+
.block();
69+
Map<Object, Object> body = response.bodyToMono(Map.class).block();
70+
assertThat(body).containsEntry("message", "An error occurred while processing the request");
71+
assertThat(body).doesNotContainKey("trace");
72+
});
73+
}
74+
75+
@Test
76+
void errorPageAndErrorControllerIncludeDetails() {
77+
this.runner.withPropertyValues("server.error.include-stacktrace=always", "server.error.include-details=always")
5878
.run((context) -> {
5979
String port = context.getEnvironment().getProperty("local.management.port");
6080
WebClient client = WebClient.create("http://localhost:" + port);
6181
ClientResponse response = client.get().uri("actuator/fail").accept(MediaType.APPLICATION_JSON)
6282
.exchange().block();
63-
assertThat(response.bodyToMono(String.class).block())
64-
.contains("message\":\"An error occurred while processing the request");
83+
Map<Object, Object> body = response.bodyToMono(Map.class).block();
84+
assertThat(body).containsEntry("message", "Epic Fail");
85+
assertThat(body).hasEntrySatisfying("trace", (value) -> assertThat(value).asString()
86+
.contains("java.lang.IllegalStateException: Epic Fail"));
6587
});
6688
}
6789

0 commit comments

Comments
 (0)