zafarkhaja/jsemver

ParseException message is null

jsimomaa opened this issue · 1 comments

In some cases the detailMessage field is null for ParseException (and inheriting exceptions) resulting in non-descriptive logging with SLF4J and Logback.

I have the following code sample:

    private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);

    public static void main(String[] args) {
        try {
            Version.valueOf("v0.0.1");
        } catch (Exception e) {
            LOGGER.error("Could not parse version", e);
        }
    }

I know that the leading v is the cause for this exception. However, the code above produces the following output:

15:59:12.485 [main] ERROR Main - Could not parse version
com.github.zafarkhaja.semver.UnexpectedCharacterException: null
    at com.github.zafarkhaja.semver.VersionParser.consumeNextCharacter(VersionParser.java:516)
    at com.github.zafarkhaja.semver.VersionParser.digits(VersionParser.java:448)
    at com.github.zafarkhaja.semver.VersionParser.numericIdentifier(VersionParser.java:408)
    at com.github.zafarkhaja.semver.VersionParser.parseVersionCore(VersionParser.java:285)
    at com.github.zafarkhaja.semver.VersionParser.parseValidSemVer(VersionParser.java:255)
    at com.github.zafarkhaja.semver.VersionParser.parseValidSemVer(VersionParser.java:195)
    at com.github.zafarkhaja.semver.Version.valueOf(Version.java:265)
    at Main.main(Main.java:10)

which is not very descriptive.

I think this could be fixed quite simply with the following addition to ParseException:

    @Override
    public String getMessage() {
        if (super.getMessage() != null)
            return super.getMessage();
        return toString();
    }

Hello Jani! Sorry for the delay and thank you for your contribution!