provide better error messages for malformed yaml

This commit is contained in:
Jonathan Shook 2021-04-08 15:03:54 -05:00
parent 5355edf759
commit 8143a2c275
6 changed files with 59 additions and 43 deletions

View File

@ -45,11 +45,11 @@ public class RawStmtsLoader {
try {
Optional<Content<?>> oyaml = NBIO.all().prefix(searchPaths).name(path).extension(YAML_EXTENSIONS).first();
data = oyaml.map(Content::asString).orElseThrow(() -> new BasicError("Unable to load " + path));
return loadString(logger, data);
} catch (Exception e) {
throw new RuntimeException("error while reading file " + path, e);
}
return loadString(logger, data);
}
private RawStmtsDocList parseYaml(Logger logger, String data) {

View File

@ -267,15 +267,17 @@ public class NBCLIScenarioParser {
for (Path yamlPath : yamlPathList) {
String referenced = yamlPath.toString();
try {
if (referenced.startsWith("/")) {
if (yamlPath.getFileSystem() == FileSystems.getDefault()) {
Path relative = Paths.get(System.getProperty("user.dir")).toAbsolutePath().relativize(yamlPath);
if (!relative.toString().contains("..")) {
referenced = relative.toString();
String referenced = yamlPath.toString();
if (referenced.startsWith("/")) {
if (yamlPath.getFileSystem() == FileSystems.getDefault()) {
Path relative = Paths.get(System.getProperty("user.dir")).toAbsolutePath().relativize(yamlPath);
if (!relative.toString().contains("..")) {
referenced = relative.toString();
}
}
}
// String alternate = referenced.startsWith("/") ? referenced.substring(1) : referenced;
// Optional<Content<?>> checkLoad = NBIO.all().prefix(SEARCH_IN)
// .name(alternate).extension("yaml")
@ -283,46 +285,50 @@ public class NBCLIScenarioParser {
// if (checkLoad.isPresent()) {
// referenced = alternate;
// }
}
Content<?> content = NBIO.all().prefix(SEARCH_IN)
.name(referenced).extension("yaml")
.one();
StmtsDocList stmts = StatementsLoader.loadContent(logger, content);
if (stmts.getStmtDocs().size() == 0) {
logger.warn("Encountered yaml with no docs in '" + referenced + "'");
continue;
}
Map<String, String> templates = new LinkedHashMap<>();
try {
List<String> lines = Files.readAllLines(yamlPath);
for (String line : lines) {
templates = matchTemplates(line, templates);
}
} catch (IOException e) {
e.printStackTrace();
}
Content<?> content = NBIO.all().prefix(SEARCH_IN)
.name(referenced).extension("yaml")
.one();
StmtsDocList stmts = StatementsLoader.loadContent(logger, content);
if (stmts.getStmtDocs().size() == 0) {
logger.warn("Encountered yaml with no docs in '" + referenced + "'");
continue;
}
Map<String, String> templates = new LinkedHashMap<>();
try {
List<String> lines = Files.readAllLines(yamlPath);
for (String line : lines) {
templates = matchTemplates(line, templates);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
Scenarios scenarios = stmts.getDocScenarios();
Scenarios scenarios = stmts.getDocScenarios();
List<String> scenarioNames = scenarios.getScenarioNames();
List<String> scenarioNames = scenarios.getScenarioNames();
if (scenarioNames != null && scenarioNames.size() > 0) {
if (scenarioNames != null && scenarioNames.size() > 0) {
// String path = yamlPath.toString();
// path = path.startsWith(FileSystems.getDefault().getSeparator()) ? path.substring(1) : path;
LinkedHashMap<String, String> sortedTemplates = new LinkedHashMap<>();
ArrayList<String> keyNames = new ArrayList<>(templates.keySet());
Collections.sort(keyNames);
for (String keyName : keyNames) {
sortedTemplates.put(keyName, templates.get(keyName));
}
LinkedHashMap<String, String> sortedTemplates = new LinkedHashMap<>();
ArrayList<String> keyNames = new ArrayList<>(templates.keySet());
Collections.sort(keyNames);
for (String keyName : keyNames) {
sortedTemplates.put(keyName, templates.get(keyName));
}
String description = stmts.getDescription();
workloadDescriptions.add(new WorkloadDesc(referenced, scenarioNames, sortedTemplates, description, ""));
String description = stmts.getDescription();
workloadDescriptions.add(new WorkloadDesc(referenced, scenarioNames, sortedTemplates, description, ""));
}
} catch (Exception e) {
throw new RuntimeException("Error while scanning path '" + yamlPath.toString() + "':" + e.getMessage(), e);
}
}
Collections.sort(workloadDescriptions);

View File

@ -84,8 +84,12 @@ public class RawYamlStatementLoaderTest {
assertThat(blocks).hasSize(1);
RawStmtsBlock rawStmtsBlock = blocks.get(0);
assertThat(rawStmtsBlock.getName()).isEqualTo("block0");
}
@Test
public void testErrorMsg() {
RawStmtsLoader ysl = new RawStmtsLoader();
RawStmtsDocList erthing = ysl.loadPath(logger, "testdocs/badyamlfile.yaml");
}
}

View File

@ -0,0 +1,2 @@
# This is just a testing file too.
description: This is just a testing file too

View File

@ -8,9 +8,13 @@ import java.util.List;
public class NBCLIScenarios {
public static void printWorkloads(boolean includeScenarios,
String... includes) {
List<WorkloadDesc> workloads =
NBCLIScenarioParser.getWorkloadsWithScenarioScripts(true, includes);
List<WorkloadDesc> workloads = List.of();
try {
workloads= NBCLIScenarioParser.getWorkloadsWithScenarioScripts(true, includes);
} catch (Exception e) {
throw new RuntimeException("Error while getting workloads:" + e.getMessage(), e);
}
for (WorkloadDesc workload : workloads) {
System.out.println(workload.toMarkdown(includeScenarios));
}

View File

@ -41,7 +41,7 @@ public class NBCliIntegrationTests {
invoker.setLogDir("logs/test");
ProcessResult result = invoker.run(
"workload-test", 15, java, "-jar",
JARNAME, "--logs-dir", "logs/test", "--list-workloads"
JARNAME, "--logs-dir", "logs/test", "--list-workloads", "--show-stacktraces"
);
System.out.println(result.getStdoutData());
System.out.println(result.getStderrData());