Skip to content

Commit d22b587

Browse files
authored
Avoid throwing exceptions and error-level logging (#676)
1 parent 54b5e82 commit d22b587

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

src/main/java/com/networknt/schema/DateTimeValidator.java

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ public DateTimeValidator(String schemaPath, JsonNode schemaNode, JsonSchema pare
3939
this.formatName = formatName;
4040
this.validationContext = validationContext;
4141
parseErrorCode(getValidatorType().getErrorCodeKey());
42+
43+
if (!formatName.equals(DATE) && !formatName.equals(DATETIME)) {
44+
throw new IllegalArgumentException(String.format("formatName must be one of the following: [%s, %s]", DATE, DATETIME));
45+
}
4246
}
4347

4448
public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) {
@@ -57,29 +61,22 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
5761
}
5862

5963
private boolean isLegalDateTime(String string) {
60-
if(formatName.equals(DATE)) {
61-
return tryParse(() -> LocalDate.parse(string));
62-
} else if(formatName.equals(DATETIME)) {
63-
return tryParse(() -> {
64+
try {
65+
if (formatName.equals(DATE)) {
66+
LocalDate.parse(string);
67+
} else if (formatName.equals(DATETIME)) {
6468
try {
6569
ITU.parseDateTime(string);
6670
} catch (LeapSecondException ex) {
67-
if(!ex.isVerifiedValidLeapYearMonth()) {
68-
throw ex;
71+
if (!ex.isVerifiedValidLeapYearMonth()) {
72+
return false;
6973
}
7074
}
71-
});
72-
} else {
73-
throw new IllegalStateException("Unknown format: " + formatName);
74-
}
75-
}
75+
}
7676

77-
private boolean tryParse(Runnable parser) {
78-
try {
79-
parser.run();
8077
return true;
8178
} catch (Exception ex) {
82-
logger.error("Invalid {}: {}", formatName, ex.getMessage());
79+
logger.debug("Invalid {}: {}", formatName, ex.getMessage());
8380
return false;
8481
}
8582
}

0 commit comments

Comments
 (0)