Skip to content

Commit 9e5a1b3

Browse files
eddumelendezwilkinsona
authored andcommitted
Provide informative reason when rejecting request with invalid level
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. Fixes gh-10588
1 parent 5acd6c4 commit 9e5a1b3

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/LoggersMvcEndpoint.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222
import org.springframework.boot.actuate.endpoint.LoggersEndpoint.LoggerLevels;
2323
import org.springframework.boot.context.properties.ConfigurationProperties;
2424
import org.springframework.boot.logging.LogLevel;
25+
import org.springframework.http.HttpStatus;
2526
import org.springframework.http.ResponseEntity;
2627
import org.springframework.web.bind.annotation.PathVariable;
2728
import org.springframework.web.bind.annotation.RequestBody;
2829
import org.springframework.web.bind.annotation.ResponseBody;
30+
import org.springframework.web.bind.annotation.ResponseStatus;
2931

3032
/**
3133
* Adapter to expose {@link LoggersEndpoint} as an {@link MvcEndpoint}.
@@ -74,7 +76,7 @@ public Object set(@PathVariable String name,
7476
return ResponseEntity.ok().build();
7577
}
7678
catch (IllegalArgumentException ex) {
77-
return ResponseEntity.badRequest().build();
79+
throw new InvalidLogLevelException("No such log level " + configuration.get("configuredLevel"));
7880
}
7981
}
8082

@@ -83,4 +85,17 @@ private LogLevel getLogLevel(Map<String, String> configuration) {
8385
return (level == null ? null : LogLevel.valueOf(level.toUpperCase()));
8486
}
8587

88+
/**
89+
* Exception thrown when the specified log level cannot be found.
90+
*/
91+
@SuppressWarnings("serial")
92+
@ResponseStatus(value = HttpStatus.BAD_REQUEST, reason = "No such log level")
93+
public static class InvalidLogLevelException extends RuntimeException {
94+
95+
public InvalidLogLevelException(String string) {
96+
super(string);
97+
}
98+
99+
}
100+
86101
}

spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/LoggersMvcEndpointTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import org.springframework.web.context.WebApplicationContext;
5050

5151
import static org.hamcrest.Matchers.equalTo;
52+
import static org.hamcrest.Matchers.is;
5253
import static org.mockito.BDDMockito.given;
5354
import static org.mockito.Mockito.mock;
5455
import static org.mockito.Mockito.verify;
@@ -174,7 +175,8 @@ public void setLoggerWhenDisabledShouldReturnNotFound() throws Exception {
174175
public void setLoggerWithWrongLogLevel() throws Exception {
175176
this.mvc.perform(post("/loggers/ROOT").contentType(MediaType.APPLICATION_JSON)
176177
.content("{\"configuredLevel\":\"other\"}"))
177-
.andExpect(status().is4xxClientError());
178+
.andExpect(status().is4xxClientError())
179+
.andExpect(status().reason(is("No such log level")));
178180
verifyZeroInteractions(this.loggingSystem);
179181
}
180182

0 commit comments

Comments
 (0)