Skip to content

Commit 2eb0c3b

Browse files
committed
Merge pull request #10588 from Eddú Meléndez
* gh-10588: Polish "Provide informative reason when rejecting request with invalid level" Provide informative reason when rejecting request with invalid level
2 parents 5acd6c4 + 43aa7db commit 2eb0c3b

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

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

Lines changed: 22 additions & 7 deletions
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}.
@@ -68,19 +70,32 @@ public Object set(@PathVariable String name,
6870
// disabled
6971
return getDisabledResponse();
7072
}
73+
LogLevel logLevel = getLogLevel(configuration);
74+
this.delegate.setLogLevel(name, logLevel);
75+
return ResponseEntity.ok().build();
76+
}
77+
78+
private LogLevel getLogLevel(Map<String, String> configuration) {
79+
String level = configuration.get("configuredLevel");
7180
try {
72-
LogLevel logLevel = getLogLevel(configuration);
73-
this.delegate.setLogLevel(name, logLevel);
74-
return ResponseEntity.ok().build();
81+
return (level == null ? null : LogLevel.valueOf(level.toUpperCase()));
7582
}
7683
catch (IllegalArgumentException ex) {
77-
return ResponseEntity.badRequest().build();
84+
throw new InvalidLogLevelException(level);
7885
}
7986
}
8087

81-
private LogLevel getLogLevel(Map<String, String> configuration) {
82-
String level = configuration.get("configuredLevel");
83-
return (level == null ? null : LogLevel.valueOf(level.toUpperCase()));
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 level) {
96+
super("Log level '" + level + "' is invalid");
97+
}
98+
8499
}
85100

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)