Skip to content

Commit 883c3a6

Browse files
committed
Revised IllegalArgumentException handling for Formatter parse calls
Issue: SPR-14661 (cherry picked from commit 73bbe08)
1 parent 68332bf commit 883c3a6

File tree

4 files changed

+12
-10
lines changed

4 files changed

+12
-10
lines changed

spring-beans/src/main/java/org/springframework/beans/AbstractNestablePropertyAccessor.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ private Object convertIfNecessary(String propertyName, Object oldValue, Object n
590590
new PropertyChangeEvent(this.rootObject, this.nestedPath + propertyName, oldValue, newValue);
591591
throw new ConversionNotSupportedException(pce, requiredType, ex);
592592
}
593-
catch (Throwable ex) {
593+
catch (IllegalArgumentException ex) {
594594
PropertyChangeEvent pce =
595595
new PropertyChangeEvent(this.rootObject, this.nestedPath + propertyName, oldValue, newValue);
596596
throw new TypeMismatchException(pce, requiredType, ex);
@@ -914,9 +914,7 @@ else if (Map.class.isAssignableFrom(type)) {
914914
return BeanUtils.instantiate(type);
915915
}
916916
}
917-
catch (Exception ex) {
918-
// TODO: Root cause exception context is lost here; just exception message preserved.
919-
// Should we throw another exception type that preserves context instead?
917+
catch (Throwable ex) {
920918
throw new NullValueInNestedPathException(getRootClass(), this.nestedPath + name,
921919
"Could not instantiate property type [" + type.getName() + "] to auto-grow nested property path: " + ex);
922920
}

spring-beans/src/main/java/org/springframework/beans/TypeConverterSupport.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ private <T> T doConvert(Object value, Class<T> requiredType, MethodParameter met
7373
catch (IllegalStateException ex) {
7474
throw new ConversionNotSupportedException(value, requiredType, ex);
7575
}
76-
catch (Throwable ex) {
76+
catch (IllegalArgumentException ex) {
7777
throw new TypeMismatchException(value, requiredType, ex);
7878
}
7979
}

spring-context/src/main/java/org/springframework/format/support/FormatterPropertyEditorAdapter.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.beans.PropertyEditor;
2020
import java.beans.PropertyEditorSupport;
21-
import java.text.ParseException;
2221

2322
import org.springframework.context.i18n.LocaleContextHolder;
2423
import org.springframework.format.Formatter;
@@ -65,7 +64,10 @@ public void setAsText(String text) throws IllegalArgumentException {
6564
try {
6665
setValue(this.formatter.parse(text, LocaleContextHolder.getLocale()));
6766
}
68-
catch (ParseException ex) {
67+
catch (IllegalArgumentException ex) {
68+
throw ex;
69+
}
70+
catch (Throwable ex) {
6971
throw new IllegalArgumentException("Parse attempt failed for value [" + text + "]", ex);
7072
}
7173
}

spring-context/src/main/java/org/springframework/format/support/FormattingConversionService.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.springframework.format.support;
1818

1919
import java.lang.annotation.Annotation;
20-
import java.text.ParseException;
2120
import java.util.Collections;
2221
import java.util.Map;
2322
import java.util.Set;
@@ -193,11 +192,14 @@ public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor t
193192
try {
194193
result = this.parser.parse(text, LocaleContextHolder.getLocale());
195194
}
196-
catch (ParseException ex) {
195+
catch (IllegalArgumentException ex) {
196+
throw ex;
197+
}
198+
catch (Throwable ex) {
197199
throw new IllegalArgumentException("Parse attempt failed for value [" + text + "]", ex);
198200
}
199201
if (result == null) {
200-
throw new IllegalStateException("Parsers are not allowed to return null");
202+
throw new IllegalStateException("Parsers are not allowed to return null: " + this.parser);
201203
}
202204
TypeDescriptor resultType = TypeDescriptor.valueOf(result.getClass());
203205
if (!resultType.isAssignableTo(targetType)) {

0 commit comments

Comments
 (0)