lib-recur
lib-recur copied to clipboard
Change InvalidRecurrenceRuleException to be a RuntimeException
It would be great if InvalidRecurrenceRuleException could be changed to be a RuntimeException so it does not have to be handled explicitly every time a rule String is parsed. I use validators before processing them, so an invalid rule is an application error in my case.
To clarify, code like this:
public Optional<RecurrenceRule> getParsedRule() {
if (StringUtil.isNullOrEmpty(rule)) {
return Optional.empty();
} else {
try {
return Optional.of(new RecurrenceRule(rule));
} catch (InvalidRecurrenceRuleException ex) {
throw new RuntimeException(ex);
}
}
}
could become just this:
public Optional<RecurrenceRule> getParsedRule() {
if (StringUtil.isNullOrEmpty(rule)) {
return Optional.empty();
} else {
return Optional.of(new RecurrenceRule(rule));
}
}
Actually, I think the way you do it now is the better (I'm tempted to say the "correct") one.
For you it's a runtime error if you have an invalid RRULE at this point. For others who don't validate the RRULE in advance (like we do in our projects) a RuntimeException (or any subtype) would be inappropriate.
To be more specific, I want the Exception to be checked, so users of this class are aware that the constructor will throw if the rule is invalid.
We will, however, consider your use case when we revisit the overall design of this library.
Btw, if you validate the rule in advance you probably should explicitly specify which RfcMode you want to use, to make sure it matches your own validation.
I think checked Exceptions are an anti pattern but I know that there are also good arguments for the contrary and I understand your reasoning. Thanks for considering this for the redesign. Maybe there is a way for this without using any kind of throwable.
Also thanks for the advice with the RfcMode.