Skip to content

Commit

Permalink
Add a throwExceptionOnFail option
Browse files Browse the repository at this point in the history
  • Loading branch information
Guy Davenport committed Oct 17, 2024
1 parent 31c329b commit bfd4f18
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.brapi.schematools.cli;

import lombok.Getter;
import org.brapi.schematools.core.response.Response;

import java.util.Collection;

@Getter
public class BrAPICommandException extends RuntimeException {
Collection<Response.Error> allErrors ;

public BrAPICommandException(String message, Collection<Response.Error> allErrors) {
super(message) ;

this.allErrors = allErrors ;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public class GenerateSubCommand implements Runnable {
@CommandLine.Option(names = {"-r", "--overwrite"}, description = "Overwrite the output file(s) if it already exists. True by default, if set to False the output wll not be over writen.")
private boolean overwrite = true;

@CommandLine.Option(names = {"-x", "--throwExceptionOnFail"}, description = "Throw an exception on failure. False by default, if set to True if an exception is thrown when validation or generation fails.")
private boolean throwExceptionOnFail = false;

@Override
public void run() {
try {
Expand Down Expand Up @@ -141,13 +144,18 @@ private void outputIntrospectionSchema(GraphQLSchema schema) {
}

private void printGraphQLSchemaErrors(Response<GraphQLSchema> response) {
String message ;
if (response.getAllErrors().size() == 1) {
err.printf("There was 1 error generating the GraphQL Schema:%n");
err.println(message = "There was 1 error generating the GraphQL Schema");
} else {
err.printf("There were %d errors generating the GraphQL Schema:%n", response.getAllErrors().size());
err.println(message = String.format("There were %d errors generating the GraphQL Schema", response.getAllErrors().size()));
}

response.getAllErrors().forEach(this::printError);

if (throwExceptionOnFail) {
throw new BrAPICommandException(message, response.getAllErrors()) ;
}
}

private void generateOpenAPISpecification(OpenAPIGeneratorOptions options) {
Expand Down Expand Up @@ -186,13 +194,18 @@ private void outputOpenAPISpecification(OpenAPI specification, Path outputPath)
}

private void printOpenAPISpecificationErrors(Response<List<OpenAPI>> response) {
String message ;
if (response.getAllErrors().size() == 1) {
System.err.printf("There was 1 error generating the OpenAPI Specification:%n");
System.err.println(message = "There was 1 error generating the OpenAPI Specification");
} else {
System.err.printf("There were %d errors generating the OpenAPI Specification:%n", response.getAllErrors().size());
System.err.println(message = String.format("There were %d errors generating the OpenAPI Specification", response.getAllErrors().size()));
}

response.getAllErrors().forEach(this::printError);

if (throwExceptionOnFail) {
throw new BrAPICommandException(message, response.getAllErrors()) ;
}
}

private void generateOntModel(OntModelGeneratorOptions options, OntModelGeneratorMetadata metadata) {
Expand All @@ -216,13 +229,18 @@ private void outputOntModel(OntModel model) {
}

private void printOntModelErrors(Response<OntModel> response) {
String message ;
if (response.getAllErrors().size() == 1) {
err.printf("There was 1 error generating the RDF Graph:%n");
err.println(message = "There was 1 error generating the RDF Graph");
} else {
err.printf("There were %d errors generating the RDF Graph:%n", response.getAllErrors().size());
err.println(message = String.format("There were %d errors generating the RDF Graph", response.getAllErrors().size()));
}

response.getAllErrors().forEach(this::printError);

if (throwExceptionOnFail) {
throw new BrAPICommandException(message, response.getAllErrors()) ;
}
}

private boolean openWriter(Path outputPathFile) throws IOException {
Expand Down Expand Up @@ -268,22 +286,27 @@ private void outputMarkdownPaths(List<Path> paths) {
if (paths.isEmpty()) {
System.out.println("Did not generate any markdown files");
} else if (paths.size() == 1) {
System.out.println(String.format("Generated '1' markdown file:"));
System.out.println("Generated '1' markdown file:");
System.out.println(paths.get(0).toString());
} else {
System.out.println(String.format("Generated '%s' markdown files:", paths.size()));
System.out.printf("Generated '%s' markdown files:%n", paths.size());
paths.forEach(path -> System.out.println(path.toString()));
}
}

private void printMarkdownErrors(Response<List<Path>> response) {
String message ;
if (response.getAllErrors().size() == 1) {
err.printf("There was 1 error generating the Markdown:%n");
err.println(message = "There was 1 error generating the Markdown");
} else {
err.printf("There were %d errors generating the Markdown:%n", response.getAllErrors().size());
err.println(message = String.format("There were %d errors generating the Markdown", response.getAllErrors().size())); ;
}

response.getAllErrors().forEach(this::printError);

if (throwExceptionOnFail) {
throw new BrAPICommandException(message, response.getAllErrors()) ;
}
}

private void generateExcel() {
Expand Down Expand Up @@ -323,13 +346,18 @@ private void outputExcelPaths(List<Path> paths) {
}

private void printExcelErrors(Response<List<Path>> response) {
String message ;
if (response.getAllErrors().size() == 1) {
err.printf("There was 1 error generating the Excel:%n");
err.println(message = "There was 1 error generating the Excel");
} else {
err.printf("There were %d errors generating the Excel:%n", response.getAllErrors().size());
err.println(message = String.format("There were %d errors generating the Excel", response.getAllErrors().size()));
}

response.getAllErrors().forEach(this::printError);

if (throwExceptionOnFail) {
throw new BrAPICommandException(message, response.getAllErrors()) ;
}
}

private void printError(Response.Error error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public class ValidateSubCommand implements Runnable {
@CommandLine.Parameters(index = "0", description = "The directory containing the BrAPI JSON schema")
private Path schemaDirectory;

@CommandLine.Option(names = {"-x", "--throwExceptionOnFail"}, description = "Throw an exception on failure. False by default, if set to True if an exception is thrown when validation or generation fails.")
private boolean throwExceptionOnFail = false ;

@Override
public void run() {
BrAPISchemaReader schemaReader = new BrAPISchemaReader() ;
Expand All @@ -31,13 +34,18 @@ public void run() {
}

private void printErrors(Response<List<BrAPIClass>> response) {
String message ;
if (response.getAllErrors().size() == 1) {
System.err.printf("There was 1 error validating the JSON Schema:%n");
System.err.println(message = "There was 1 error validating the JSON Schema");
} else {
System.err.printf("There were %d errors validating the JSON Schema:%n", response.getAllErrors().size());
System.err.println(message = String.format("There were %d errors validating the JSON Schema", response.getAllErrors().size()));
}

response.getAllErrors().forEach(this::printError);

if (throwExceptionOnFail) {
throw new BrAPICommandException(message, response.getAllErrors()) ;
}
}

private void printError(Response.Error error) {
Expand Down

0 comments on commit bfd4f18

Please sign in to comment.