sbaudoin/yamllint

Enhance the yaml syntax error with more information

Closed this issue · 1 comments

Thanks for providing your library. I have integrated it into a spring boot application and lint the configuration files before the startup of the application.

What I have found out is that syntax errors are reported, but SnakeYML is able to provide more error information.

/**
   * Parses the passed YAML string to detect syntax errors. If an error is met, a problem is return.
   *
   * @param buffer a YAML string
   * @return a problem or <code>null</code> if there is no syntax error
   */
  public static LintProblem getSyntaxError(String buffer) {
      try {
          // Need to use loadAll in the event there are multiple documents in the same stream
          new Yaml().loadAll(new StringReader(buffer)).forEach(o -> { /* Do nothing on purpose, required to have the parser to process each document */ });
      } catch (MarkedYAMLException e) {
          LintProblem problem = new LintProblem(e.getProblemMark().getLine() + 1,
                  e.getProblemMark().getColumn() + 1,
                  "syntax error: " + e.getProblem());
          problem.setLevel(ERROR_LEVEL);
          return problem;
      }
      return null;
  }

could be changed to:

    /**
     * Parses the passed YAML string to detect syntax errors. If an error is met, a problem is return.
     *
     * @param buffer a YAML string
     * @return a problem or <code>null</code> if there is no syntax error
     */
    public static LintProblem getSyntaxError(String buffer) {
        try {
            // Need to use loadAll in the event there are multiple documents in the same stream
            new Yaml().loadAll(new StringReader(buffer)).forEach(o -> { /* Do nothing on purpose, required to have the parser to process each document */ });
        } catch (MarkedYAMLException e) {
            LintProblem problem = new LintProblem(e.getProblemMark().getLine() + 1,
                    e.getProblemMark().getColumn() + 1,
                    "syntax error: " + e.getMessage);
            problem.setLevel(ERROR_LEVEL);
            return problem;
        }
        return null;
    }

I have changed e.getProblem to e.getMessage...

Error message before:
67:5:syntax error: expected <block end>, but found '<block mapping start>'

Error message with more information:

67:5:syntax error: while parsing a block mapping
 in 'reader', line 63, column 3:
      temp-directory: "/tmp/adfc ... 
      ^
expected <block end>, but found '<block mapping start>'
 in 'reader', line 67, column 5:
        cache-file-size-threshold: 100
        ^

LintProblem problem = new LintProblem(e.getProblemMark().getLine() + 1,

It is worth to create a merge request? Do you want to change that?

Thanks
Jens

Hello,

Thanks for your proposition. This lib is actually used behind the YAML SonarQube plugin, so I need to see what would be the behavior of the plugin with this code change before deciding anything. SQ can leverage the line and column info to highlight the bogus piece of code.