diff --git a/spring-beans/src/main/java/org/springframework/beans/propertyeditors/ZoneIdEditor.java b/spring-beans/src/main/java/org/springframework/beans/propertyeditors/ZoneIdEditor.java index 4992e33aebd0..60baf80864b7 100644 --- a/spring-beans/src/main/java/org/springframework/beans/propertyeditors/ZoneIdEditor.java +++ b/spring-beans/src/main/java/org/springframework/beans/propertyeditors/ZoneIdEditor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,6 +27,7 @@ * * @author Nicholas Williams * @author Sam Brannen + * @author Ngoc Nhan * @since 4.0 * @see java.time.ZoneId * @see TimeZoneEditor @@ -38,7 +39,13 @@ public void setAsText(String text) throws IllegalArgumentException { if (StringUtils.hasText(text)) { text = text.trim(); } - setValue(ZoneId.of(text)); + try { + + setValue(ZoneId.of(text)); + } + catch (Exception ex) { + throw new IllegalArgumentException("Failed to convert ZoneId for " + text, ex); + } } @Override diff --git a/spring-beans/src/test/java/org/springframework/beans/propertyeditors/ZoneIdEditorTests.java b/spring-beans/src/test/java/org/springframework/beans/propertyeditors/ZoneIdEditorTests.java index 535f0ed3ad87..b00e83cdce9c 100644 --- a/spring-beans/src/test/java/org/springframework/beans/propertyeditors/ZoneIdEditorTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/propertyeditors/ZoneIdEditorTests.java @@ -23,10 +23,12 @@ import org.junit.jupiter.params.provider.ValueSource; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; /** * @author Nicholas Williams * @author Sam Brannen + * @author Ngoc Nhan */ class ZoneIdEditorTests { @@ -69,4 +71,9 @@ void getValueAsText() { assertThat(editor.getAsText()).as("The text version is not correct.").isEqualTo("America/New_York"); } + @Test + void throwIllegalArgumentException() { + assertThatIllegalArgumentException().isThrownBy(() -> editor.setAsText("Hello, World!")); + } + }