Skip to content

Commit

Permalink
Fix unexpected NumberFormatException in Durations.parse() by replacin…
Browse files Browse the repository at this point in the history
…g with documented thrown ParseException.

This fixes parsing of invalid second value long to throw the correct exception. Most users should already be handling ParseExceptions e.g. for invalid nanos

PiperOrigin-RevId: 686281774
  • Loading branch information
zhangskz authored and copybara-github committed Oct 15, 2024
1 parent 3b2a608 commit ee5aa49
Showing 1 changed file with 6 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,12 @@ public static Duration parse(String value) throws ParseException {
nanoValue = secondValue.substring(pointPosition + 1);
secondValue = secondValue.substring(0, pointPosition);
}
long seconds = Long.parseLong(secondValue);
long seconds;
try {
seconds = Long.parseLong(secondValue);
} catch (NumberFormatException e) {
throw new ParseException("Invalid duration string: " + value, 0);
}
int nanos = nanoValue.isEmpty() ? 0 : Timestamps.parseNanos(nanoValue);
if (seconds < 0) {
throw new ParseException("Invalid duration string: " + value, 0);
Expand Down

0 comments on commit ee5aa49

Please sign in to comment.