tomasbjerre / violations-lib

Java library for parsing report files from static code analysis.
Apache License 2.0
148 stars 39 forks source link

Show better error message for unknown parsers #169

Open CarstenGrohmann opened 1 year ago

CarstenGrohmann commented 1 year ago

Hello Tomas,

please show an informative error message for unknown parsers. Currently the generic ISSUES parser is not supported. This triggers a very noise Java exception like:

[Pipeline] stage
[Pipeline] { (Test ISSUES parser)
[Pipeline] sh
+ echo '{"fileName":"basic.yml","severity":"ERROR","lineStart":10,"lineEnd":11,"columnStart":110,"columnEnd":120,"message":"some message","description":"some description"}'
+ cat example-out.json
{"fileName":"basic.yml","severity":"ERROR","lineStart":10,"lineEnd":11,"columnStart":110,"columnEnd":120,"message":"some message","description":"some description"}
+ ls -l example-out.json
-rw-r--r-- 1 jenkins jenkins 164 Jan 16 08:21 example-out.json
[Pipeline] }
[Pipeline] // stage
[Pipeline] step
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
java.lang.IllegalArgumentException: No enum constant se.bjurr.violations.lib.reports.Parser.ISSUES
    at java.base/java.lang.Enum.valueOf(Enum.java:240)
    at org.jenkinsci.plugins.structs.describable.DescribableModel.coerce(DescribableModel.java:476)
    at org.jenkinsci.plugins.structs.describable.DescribableModel.buildArguments(DescribableModel.java:409)
    at org.jenkinsci.plugins.structs.describable.DescribableModel.instantiate(DescribableModel.java:329)
Caused: java.lang.IllegalArgumentException: Could not instantiate {parser=ISSUES, pattern=.*example-out.json$, reporter=Custom message for ISSUES parser} for org.jenkinsci.plugins.jvctgl.config.ViolationConfig
    at org.jenkinsci.plugins.structs.describable.DescribableModel.instantiate(DescribableModel.java:334)
    at org.jenkinsci.plugins.structs.describable.DescribableModel.coerce(DescribableModel.java:474)
    at org.jenkinsci.plugins.structs.describable.DescribableModel.coerceList(DescribableModel.java:585)
    at org.jenkinsci.plugins.structs.describable.DescribableModel.coerce(DescribableModel.java:458)
    at org.jenkinsci.plugins.structs.describable.DescribableModel.injectSetters(DescribableModel.java:429)
    at org.jenkinsci.plugins.structs.describable.DescribableModel.instantiate(DescribableModel.java:331)
<snip>

People who are not (Java) programmers, it is hard to guess the reason of this exception. Therefore I ask you to catch this exception and replace it with an informative error message (example):

ERROR: Unknown parser "...". Please check violationConfigs and configure a known/supported parser.

Steps to reproduce:

$ cat Jenkinsfile
#!/usr/bin/env groovy
node {
  checkout scm
  try {
    stage('Test ISSUES parser') {
      // see https://github.com/jenkinsci/warnings-ng-plugin/blob/master/doc/Documentation.md#creating-support-for-a-custom-tool
      sh '''
            echo '{"fileName":"basic.yml","severity":"ERROR","lineStart":10,"lineEnd":11,"columnStart":110,"columnEnd":120,"message":"some message","description":"some description"}' > example-out.json
            cat example-out.json
            ls -l example-out.json
      '''
    }
  }
  catch (err) {
    echo "ERROR: General Error: ${err}"
  }
  finally {
    if (!env.BRANCH_IS_PRIMARY) {
      ViolationsToGitLab(
        [
          apiTokenCredentialsId: '42',
          apiTokenPrivate: true,
          authMethodHeader: true,
          commentOnlyChangedContentContext: 0,
          commentOnlyChangedFiles: true,
          createSingleFileComments: true,
          enableLogging: true,
          gitLabUrl: 'https://gitlab.com',
          keepOldComments: false,
          maxNumberOfViolations: 99999,
          mergeRequestIid: "${env.CHANGE_ID.toInteger()}",
          projectId: "${env.GITLAB_PROJECT_PATH_NAMESPACE}",
          violationConfigs: [
            // [ parser: 'FLAKE8', pattern: '.*example-out.json$', reporter: 'FLAKE8 parser' ]
            [ parser: 'ISSUES', pattern: '.*example-out.json$', reporter: 'Custom message for ISSUES parser' ]
          ]
        ]
      )
    }
  }
}

Thank you, Carsten

tomasbjerre commented 1 year ago

I totally agree with this. But cannot find a place to catch it and handle it nicer.