Skip to content

Validate likes string before parsing to Integer#59

Open
devanfibio wants to merge 1 commit intoInfinityLoop1308:mainfrom
devanfibio:main
Open

Validate likes string before parsing to Integer#59
devanfibio wants to merge 1 commit intoInfinityLoop1308:mainfrom
devanfibio:main

Conversation

@devanfibio
Copy link
Copy Markdown

@devanfibio devanfibio commented Apr 25, 2026

Description

Add validation to the likes string parser to handle edge cases where no digits are found in the input.

The parser now checks if the processed string is null or empty before attempting to parse it with Integer.parseInt(). If validation fails, a descriptive ParsingException is thrown instead of allowing a NumberFormatException to propagate.

This provides clearer error messages and prevents unexpected exceptions during parsing.

Changes

  • Extract the result of Utils.removeNonDigitCharacters() into a variable
  • Validate that the result is not null and not empty
  • Throw a descriptive ParsingException if validation fails
  • Parse the validated string with Integer.parseInt()

Before

return Integer.parseInt(Utils.removeNonDigitCharacters(likesString));

After

final String digits = Utils.removeNonDigitCharacters(likesString);
if (digits == null || digits.isEmpty()) {
    throw new ParsingException("Could not parse \"" + likesString + "\": no digits found");
}
return Integer.parseInt(digits);

Benefits

  • Clear error messages that help users identify parsing issues
  • Improved code readability with extracted variable

fix: improve likes string parser error handling with validation

Add null and empty string validation before parsing likes string. If no digits
are found in the processed string, throw a descriptive ParsingException instead
of allowing Integer.parseInt() to fail with a cryptic NumberFormatException.

This provides clearer error messages that help users understand why parsing
failed and improves overall error handling consistency.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant