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 gh-596
This commit is contained in:
parent
d13827c45c
commit
71c2c69c92
|
|
@ -30,6 +30,7 @@ import org.apache.commons.logging.LogFactory;
|
|||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.context.embedded.AbstractEmbeddedServletContainerFactory;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
|
@ -63,17 +64,26 @@ public class BasicErrorController implements ErrorController {
|
|||
|
||||
@RequestMapping(value = "${error.path:/error}", produces = "text/html")
|
||||
public ModelAndView errorHtml(HttpServletRequest request) {
|
||||
Map<String, Object> map = error(request);
|
||||
Map<String, Object> map = extract(new ServletRequestAttributes(request), false,
|
||||
false);
|
||||
return new ModelAndView(ERROR_KEY, map);
|
||||
}
|
||||
|
||||
@RequestMapping(value = "${error.path:/error}")
|
||||
@ResponseBody
|
||||
public Map<String, Object> error(HttpServletRequest request) {
|
||||
public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
|
||||
ServletRequestAttributes attributes = new ServletRequestAttributes(request);
|
||||
String trace = request.getParameter("trace");
|
||||
return extract(attributes, trace != null && !"false".equals(trace.toLowerCase()),
|
||||
true);
|
||||
Map<String, Object> extracted = extract(attributes,
|
||||
trace != null && !"false".equals(trace.toLowerCase()), true);
|
||||
HttpStatus statusCode;
|
||||
try {
|
||||
statusCode = HttpStatus.valueOf((Integer) extracted.get("status"));
|
||||
}
|
||||
catch (Exception e) {
|
||||
statusCode = HttpStatus.INTERNAL_SERVER_ERROR;
|
||||
}
|
||||
return new ResponseEntity<Map<String, Object>>(extracted, statusCode);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -69,7 +69,7 @@ public class BasicErrorControllerIntegrationTests {
|
|||
@Test
|
||||
public void testErrorForMachineClient() throws Exception {
|
||||
MvcResult response = this.mockMvc.perform(get("/error"))
|
||||
.andExpect(status().isOk()).andReturn();
|
||||
.andExpect(status().is5xxServerError()).andReturn();
|
||||
String content = response.getResponse().getContentAsString();
|
||||
assertTrue("Wrong content: " + content, content.contains("999"));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -50,7 +50,7 @@ public class EndpointsPropertiesSampleActuatorApplicationTests {
|
|||
@SuppressWarnings("rawtypes")
|
||||
ResponseEntity<Map> entity = RestTemplates.get("user", "password").getForEntity(
|
||||
"http://localhost:8080/oops", Map.class);
|
||||
assertEquals(HttpStatus.OK, entity.getStatusCode());
|
||||
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, entity.getStatusCode());
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> body = entity.getBody();
|
||||
assertEquals("None", body.get("error"));
|
||||
|
|
|
|||
|
|
@ -158,7 +158,7 @@ public class SampleActuatorApplicationTests {
|
|||
@SuppressWarnings("rawtypes")
|
||||
ResponseEntity<Map> entity = RestTemplates.get().getForEntity(
|
||||
"http://localhost:8080/error", Map.class);
|
||||
assertEquals(HttpStatus.OK, entity.getStatusCode());
|
||||
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, entity.getStatusCode());
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> body = entity.getBody();
|
||||
assertEquals("None", body.get("error"));
|
||||
|
|
|
|||
Loading…
Reference in New Issue