Skip to content

Commit 71c2c69

Browse files
author
Dave Syer
committed
Return actual status code not 200 to machine client
Machine clients are much more fussy than browsers and we should take care to preserve the HTTP status for them. Fixes spring-projectsgh-596
1 parent d13827c commit 71c2c69

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
lines changed

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/web/BasicErrorController.java

+14-4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.springframework.beans.factory.annotation.Value;
3131
import org.springframework.boot.context.embedded.AbstractEmbeddedServletContainerFactory;
3232
import org.springframework.http.HttpStatus;
33+
import org.springframework.http.ResponseEntity;
3334
import org.springframework.stereotype.Controller;
3435
import org.springframework.web.bind.annotation.RequestMapping;
3536
import org.springframework.web.bind.annotation.ResponseBody;
@@ -63,17 +64,26 @@ public String getErrorPath() {
6364

6465
@RequestMapping(value = "${error.path:/error}", produces = "text/html")
6566
public ModelAndView errorHtml(HttpServletRequest request) {
66-
Map<String, Object> map = error(request);
67+
Map<String, Object> map = extract(new ServletRequestAttributes(request), false,
68+
false);
6769
return new ModelAndView(ERROR_KEY, map);
6870
}
6971

7072
@RequestMapping(value = "${error.path:/error}")
7173
@ResponseBody
72-
public Map<String, Object> error(HttpServletRequest request) {
74+
public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
7375
ServletRequestAttributes attributes = new ServletRequestAttributes(request);
7476
String trace = request.getParameter("trace");
75-
return extract(attributes, trace != null && !"false".equals(trace.toLowerCase()),
76-
true);
77+
Map<String, Object> extracted = extract(attributes,
78+
trace != null && !"false".equals(trace.toLowerCase()), true);
79+
HttpStatus statusCode;
80+
try {
81+
statusCode = HttpStatus.valueOf((Integer) extracted.get("status"));
82+
}
83+
catch (Exception e) {
84+
statusCode = HttpStatus.INTERNAL_SERVER_ERROR;
85+
}
86+
return new ResponseEntity<Map<String, Object>>(extracted, statusCode);
7787
}
7888

7989
@Override

spring-boot-actuator/src/test/java/org/springframework/boot/actuate/web/BasicErrorControllerIntegrationTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public void setup() {
6969
@Test
7070
public void testErrorForMachineClient() throws Exception {
7171
MvcResult response = this.mockMvc.perform(get("/error"))
72-
.andExpect(status().isOk()).andReturn();
72+
.andExpect(status().is5xxServerError()).andReturn();
7373
String content = response.getResponse().getContentAsString();
7474
assertTrue("Wrong content: " + content, content.contains("999"));
7575
}

spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/EndpointsPropertiesSampleActuatorApplicationTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public void testCustomErrorPath() throws Exception {
5050
@SuppressWarnings("rawtypes")
5151
ResponseEntity<Map> entity = RestTemplates.get("user", "password").getForEntity(
5252
"http://localhost:8080/oops", Map.class);
53-
assertEquals(HttpStatus.OK, entity.getStatusCode());
53+
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, entity.getStatusCode());
5454
@SuppressWarnings("unchecked")
5555
Map<String, Object> body = entity.getBody();
5656
assertEquals("None", body.get("error"));

spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/SampleActuatorApplicationTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public void testErrorPageDirectAccess() throws Exception {
158158
@SuppressWarnings("rawtypes")
159159
ResponseEntity<Map> entity = RestTemplates.get().getForEntity(
160160
"http://localhost:8080/error", Map.class);
161-
assertEquals(HttpStatus.OK, entity.getStatusCode());
161+
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, entity.getStatusCode());
162162
@SuppressWarnings("unchecked")
163163
Map<String, Object> body = entity.getBody();
164164
assertEquals("None", body.get("error"));

0 commit comments

Comments
 (0)