Reject empty strings in DurationFormatterUtils

See gh-33669
This commit is contained in:
Seungrae
2024-10-09 00:56:59 +09:00
committed by Stéphane Nicoll
parent 7f7f65cfcb
commit 02c990ca82
2 changed files with 31 additions and 1 deletions

View File

@@ -35,6 +35,7 @@ import org.springframework.util.StringUtils;
* @author Phillip Webb
* @author Valentine Wu
* @author Simon Baslé
* @author Kim Seungrae
* @since 6.2
*/
public abstract class DurationFormatterUtils {
@@ -62,6 +63,7 @@ public abstract class DurationFormatterUtils {
* @return a duration
*/
public static Duration parse(String value, DurationFormat.Style style, @Nullable DurationFormat.Unit unit) {
Assert.hasText(value, () -> "Value must not be empty");
return switch (style) {
case ISO8601 -> parseIso8601(value);
case SIMPLE -> parseSimple(value, unit);
@@ -88,6 +90,7 @@ public abstract class DurationFormatterUtils {
* @return the printed result
*/
public static String print(Duration value, DurationFormat.Style style, @Nullable DurationFormat.Unit unit) {
Assert.notNull(value, "Value must not be null");
return switch (style) {
case ISO8601 -> value.toString();
case SIMPLE -> printSimple(value, unit);
@@ -149,7 +152,7 @@ public abstract class DurationFormatterUtils {
try {
return Duration.parse(value);
}
catch (Throwable ex) {
catch (Exception ex) {
throw new IllegalArgumentException("'" + value + "' is not a valid ISO-8601 duration", ex);
}
}