Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce friendlier NumberFormatException messages #426

Merged
merged 1 commit into from
Oct 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,16 @@ interface ConfigMessages {

@Message(id = 28, value = "Could not find a mapping for %s with prefix %s")
NoSuchElementException mappingPrefixNotFound(String className, String prefix);

@Message(id = 29, value = "Expected an integer value, got \"%s\"")
NumberFormatException integerExpected(String value);

@Message(id = 30, value = "Expected a long value, got \"%s\"")
NumberFormatException longExpected(String value);

@Message(id = 31, value = "Expected a double value, got \"%s\"")
NumberFormatException doubleExpected(String value);

@Message(id = 32, value = "Expected a float value, got \"%s\"")
NumberFormatException floatExpected(String value);
}
34 changes: 29 additions & 5 deletions implementation/src/main/java/io/smallrye/config/Converters.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,40 @@ private Converters() {
|| "OUI".equalsIgnoreCase(value)))));

static final Converter<Double> DOUBLE_CONVERTER = BuiltInConverter.of(2,
newTrimmingConverter(newEmptyValueConverter(Double::valueOf)));
newTrimmingConverter(newEmptyValueConverter(value -> {
try {
return Double.valueOf(value);
} catch (NumberFormatException nfe) {
throw ConfigMessages.msg.doubleExpected(value);
}
})));

static final Converter<Float> FLOAT_CONVERTER = BuiltInConverter.of(3,
newTrimmingConverter(newEmptyValueConverter(Float::valueOf)));
newTrimmingConverter(newEmptyValueConverter(value -> {
try {
return Float.valueOf(value);
} catch (NumberFormatException nfe) {
throw ConfigMessages.msg.floatExpected(value);
}
})));

static final Converter<Long> LONG_CONVERTER = BuiltInConverter.of(4,
newTrimmingConverter(newEmptyValueConverter(Long::valueOf)));
newTrimmingConverter(newEmptyValueConverter(value -> {
try {
return Long.valueOf(value);
} catch (NumberFormatException nfe) {
throw ConfigMessages.msg.longExpected(value);
}
})));

static final Converter<Integer> INTEGER_CONVERTER = BuiltInConverter.of(5,
newTrimmingConverter(newEmptyValueConverter(Integer::valueOf)));
newTrimmingConverter(newEmptyValueConverter(value -> {
try {
return Integer.valueOf(value);
} catch (NumberFormatException nfe) {
throw ConfigMessages.msg.integerExpected(value);
}
})));

static final Converter<Class<?>> CLASS_CONVERTER = BuiltInConverter.of(6,
newTrimmingConverter(newEmptyValueConverter(value -> {
Expand Down Expand Up @@ -108,7 +132,7 @@ private Converters() {

static final Converter<Character> CHARACTER_CONVERTER = BuiltInConverter.of(11, newEmptyValueConverter(value -> {
if (value.length() == 1) {
return Character.valueOf(value.charAt(0));
return value.charAt(0);
}
throw ConfigMessages.msg.failedCharacterConversion(value);
}));
Expand Down