Skip to content

Commit

Permalink
0003973: Putting in non-numeric string for numeric parameters causes
Browse files Browse the repository at this point in the history
error
  • Loading branch information
jaredfrees committed May 20, 2019
1 parent 846b135 commit c659520
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
Expand Up @@ -73,7 +73,11 @@ public AbstractParameterService() {
public BigDecimal getDecimal(String key, BigDecimal defaultVal) {
String val = getString(key);
if (val != null) {
return new BigDecimal(val);
try {
return new BigDecimal(val);
} catch (NumberFormatException ex) {
TypedProperties.logPropertiesException(log, key, val);
}
}
return defaultVal;
}
Expand Down Expand Up @@ -106,8 +110,12 @@ public int getInt(String key) {

public int getInt(String key, int defaultVal) {
String val = getString(key);
if (StringUtils.isNotBlank(val)) {
return Integer.parseInt(val.trim());
if (val != null) {
try {
return Integer.parseInt(val.trim());
} catch (NumberFormatException ex) {
TypedProperties.logPropertiesException(log, key, val);
}
}
return defaultVal;
}
Expand All @@ -119,7 +127,11 @@ public long getLong(String key) {
public long getLong(String key, long defaultVal) {
String val = getString(key);
if (val != null) {
return Long.parseLong(val);
try {
return Long.parseLong(val);
} catch (NumberFormatException ex) {
TypedProperties.logPropertiesException(log, key, val);
}
}
return defaultVal;
}
Expand Down
Expand Up @@ -30,6 +30,7 @@
import java.util.Set;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.jumpmind.exception.IoException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -101,6 +102,7 @@ public long getLong(String key, long defaultValue) {
try {
returnValue = Long.parseLong(value);
} catch (NumberFormatException ex) {
logPropertiesException(log, key, value);
}
}
return returnValue;
Expand All @@ -117,6 +119,7 @@ public int getInt(String key, int defaultValue) {
try {
returnValue = Integer.parseInt(value);
} catch (NumberFormatException ex) {
logPropertiesException(log, key, value);
}
}
return returnValue;
Expand Down Expand Up @@ -195,5 +198,11 @@ public void merge(Properties properties) {
public TypedProperties copy() {
return new TypedProperties(this);
}

public static void logPropertiesException(Logger logger, String key, String val) {
if (StringUtils.isNotBlank(val)) {
logger.error("Could not parse integer from parameter \"" + key + "\"=\"" + val + "\"");
}
}

}

0 comments on commit c659520

Please sign in to comment.