From 4b60ef08232408b40208ce39cc4492b0d6b96495 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Tue, 10 Oct 2017 18:52:00 -0500 Subject: [PATCH 1/2] Add proper message when wrong log level is sent Previously, bad request with no reason was included in the response. This commit introduces the reason when invalid log level is sent in the request. See gh-10568 --- .../endpoint/mvc/LoggersMvcEndpoint.java | 17 ++++++++++++++++- .../endpoint/mvc/LoggersMvcEndpointTests.java | 5 ++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/LoggersMvcEndpoint.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/LoggersMvcEndpoint.java index 0ede3e749f1f..77b1d400db1e 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/LoggersMvcEndpoint.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/LoggersMvcEndpoint.java @@ -22,10 +22,12 @@ import org.springframework.boot.actuate.endpoint.LoggersEndpoint.LoggerLevels; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.logging.LogLevel; +import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.ResponseStatus; /** * Adapter to expose {@link LoggersEndpoint} as an {@link MvcEndpoint}. @@ -74,7 +76,7 @@ public Object set(@PathVariable String name, return ResponseEntity.ok().build(); } catch (IllegalArgumentException ex) { - return ResponseEntity.badRequest().build(); + throw new InvalidLogLevelException("No such log level " + configuration.get("configuredLevel")); } } @@ -83,4 +85,17 @@ private LogLevel getLogLevel(Map configuration) { return (level == null ? null : LogLevel.valueOf(level.toUpperCase())); } + /** + * Exception thrown when the specified log level cannot be found. + */ + @SuppressWarnings("serial") + @ResponseStatus(value = HttpStatus.BAD_REQUEST, reason = "No such log level") + public static class InvalidLogLevelException extends RuntimeException { + + public InvalidLogLevelException(String string) { + super(string); + } + + } + } diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/LoggersMvcEndpointTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/LoggersMvcEndpointTests.java index 499062e2062c..1f4aaed9d051 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/LoggersMvcEndpointTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/LoggersMvcEndpointTests.java @@ -49,6 +49,7 @@ import org.springframework.web.context.WebApplicationContext; import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; @@ -57,6 +58,7 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; /** @@ -174,7 +176,8 @@ public void setLoggerWhenDisabledShouldReturnNotFound() throws Exception { public void setLoggerWithWrongLogLevel() throws Exception { this.mvc.perform(post("/loggers/ROOT").contentType(MediaType.APPLICATION_JSON) .content("{\"configuredLevel\":\"other\"}")) - .andExpect(status().is4xxClientError()); + .andExpect(status().is4xxClientError()) + .andExpect(status().reason(is("No such log level"))); verifyZeroInteractions(this.loggingSystem); } From 6456d864ba7b121dab0ef70d7c32745d5aac4940 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Tue, 10 Oct 2017 19:16:02 -0500 Subject: [PATCH 2/2] Remove unused import --- .../boot/actuate/endpoint/mvc/LoggersMvcEndpointTests.java | 1 - 1 file changed, 1 deletion(-) diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/LoggersMvcEndpointTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/LoggersMvcEndpointTests.java index 1f4aaed9d051..a6234dddc74b 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/LoggersMvcEndpointTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/LoggersMvcEndpointTests.java @@ -58,7 +58,6 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; /**