Skip to content

Commit ca04482

Browse files
committed
Convert DateTimeException to expected IllegalArgumentException
Closes gh-33545
1 parent 2ab0101 commit ca04482

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

spring-beans/src/main/java/org/springframework/beans/propertyeditors/ZoneIdEditor.java

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,16 +17,18 @@
1717
package org.springframework.beans.propertyeditors;
1818

1919
import java.beans.PropertyEditorSupport;
20+
import java.time.DateTimeException;
2021
import java.time.ZoneId;
2122

2223
import org.springframework.util.StringUtils;
2324

2425
/**
25-
* Editor for {@code java.time.ZoneId}, translating zone ID Strings into {@code ZoneId}
26-
* objects. Exposes the {@code TimeZone} ID as a text representation.
26+
* Editor for {@code java.time.ZoneId}, translating time zone Strings into {@code ZoneId}
27+
* objects. Exposes the time zone as a text representation.
2728
*
2829
* @author Nicholas Williams
2930
* @author Sam Brannen
31+
* @author Juergen Hoeller
3032
* @since 4.0
3133
* @see java.time.ZoneId
3234
* @see TimeZoneEditor
@@ -38,7 +40,12 @@ public void setAsText(String text) throws IllegalArgumentException {
3840
if (StringUtils.hasText(text)) {
3941
text = text.trim();
4042
}
41-
setValue(ZoneId.of(text));
43+
try {
44+
setValue(ZoneId.of(text));
45+
}
46+
catch (DateTimeException ex) {
47+
throw new IllegalArgumentException(ex.getMessage(), ex);
48+
}
4249
}
4350

4451
@Override

spring-beans/src/test/java/org/springframework/beans/propertyeditors/ZoneIdEditorTests.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,10 +23,12 @@
2323
import org.junit.jupiter.params.provider.ValueSource;
2424

2525
import static org.assertj.core.api.Assertions.assertThat;
26+
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
2627

2728
/**
2829
* @author Nicholas Williams
2930
* @author Sam Brannen
31+
* @author Juergen Hoeller
3032
*/
3133
class ZoneIdEditorTests {
3234

@@ -69,4 +71,9 @@ void getValueAsText() {
6971
assertThat(editor.getAsText()).as("The text version is not correct.").isEqualTo("America/New_York");
7072
}
7173

74+
@Test
75+
void correctExceptionForInvalid() {
76+
assertThatIllegalArgumentException().isThrownBy(() -> editor.setAsText("INVALID")).withMessageContaining("INVALID");
77+
}
78+
7279
}

0 commit comments

Comments
 (0)