Skip to content

Commit

Permalink
Fix #24: light better handling of lint conf errors
Browse files Browse the repository at this point in the history
  • Loading branch information
sbaudoin committed Nov 3, 2020
1 parent 7444f8f commit c17e33c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
10 changes: 6 additions & 4 deletions src/main/java/com/github/sbaudoin/yamllint/YamlLintConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,12 @@ protected void parse(String rawContent) throws YamlLintConfigException {
// Does this conf override another conf that we need to load?
if (conf.containsKey("extends")) {
try {
YamlLintConfig base = new YamlLintConfig(getExtendedConfigFile((String)conf.get("extends")));
YamlLintConfig base = new YamlLintConfig(getExtendedConfigFile((String) conf.get("extends")));
extend(base);
} catch (IllegalArgumentException e) {
throw new YamlLintConfigException("invalid extends config: " + e.getMessage(), e);
} catch (Exception e) {
throw new YamlLintConfigException("invalid config: " + e.getMessage(), e);
throw new YamlLintConfigException("invalid extends config (unknown error): " + e.getMessage(), e);
}
}

Expand Down Expand Up @@ -293,8 +295,8 @@ protected static Map<String, Object> validateRuleConf(Rule rule, Object conf) th
* @throws IllegalArgumentException if name is {@code null} or an error occurs handling the passed file name
*/
protected URL getExtendedConfigFile(String name) {
if (name == null) {
throw new IllegalArgumentException("Argument cannot be null: need to extend something");
if (name == null || "".equals(name.trim())) {
throw new IllegalArgumentException("need to extend something");
}

// Is it a standard conf shipped with yamllint...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,21 @@ public void testWrongExtend() throws IOException {
new YamlLintConfig("extends:");
fail("Invalid config not identified");
} catch (YamlLintConfigException e) {
assertEquals("invalid config: Argument cannot be null: need to extend something", e.getMessage());
assertEquals("invalid extends config: need to extend something", e.getMessage());
}

try {
new YamlLintConfig("extends:\n - foo");
fail("Invalid config not identified");
} catch (YamlLintConfigException e) {
assertEquals("invalid config: java.util.ArrayList cannot be cast to java.lang.String", e.getMessage());
assertTrue(e.getMessage().startsWith("invalid extends config (unknown error): "));
}

try {
new YamlLintConfig("extends: dummy");
fail("Unknown ruleset should not be extended");
} catch (YamlLintConfigException e) {
assertEquals("invalid config: Bundled configuration file \"dummy\" not found", e.getMessage());
assertEquals("invalid extends config: Bundled configuration file \"dummy\" not found", e.getMessage());
}

try {
Expand Down

0 comments on commit c17e33c

Please sign in to comment.