- DefaultGenerator now communicates failures by throwing exceptions

rather than setting a status flag in a public field
- DefaultGenerator now decorates exceptions to record where a failure
occured (which api, model or operation?)
- CodeGenMojo now propagates this exception to maven to abort the build
This commit is contained in:
Adrian Moos 2015-09-22 18:01:29 +02:00
parent 98ff2d231f
commit d4a94fbf52
3 changed files with 136 additions and 128 deletions

View File

@ -162,7 +162,16 @@ public class CodeGenMojo extends AbstractMojo {
ClientOptInput input = new ClientOptInput().opts(new ClientOpts()).swagger(swagger); ClientOptInput input = new ClientOptInput().opts(new ClientOpts()).swagger(swagger);
input.setConfig(config); input.setConfig(config);
try {
new DefaultGenerator().opts(input).generate(); new DefaultGenerator().opts(input).generate();
} catch (Exception e) {
// Maven logs exceptions thrown by plugins only if invoked with -e
// I find it annoying to jump through hoops to get basic diagnostic information,
// so let's log it in any case:
getLog().error(e);
throw new MojoExecutionException("Code generation failed. See above for the full exception.");
}
if (addCompileSourceRoot) { if (addCompileSourceRoot) {
project.addCompileSourceRoot(output.toString()); project.addCompileSourceRoot(output.toString());

View File

@ -5,7 +5,6 @@ import static org.apache.commons.lang3.StringUtils.isNotEmpty;
import com.samskivert.mustache.Mustache; import com.samskivert.mustache.Mustache;
import com.samskivert.mustache.Template; import com.samskivert.mustache.Template;
import io.swagger.codegen.languages.CodeGenStatus;
import io.swagger.models.ComposedModel; import io.swagger.models.ComposedModel;
import io.swagger.models.Contact; import io.swagger.models.Contact;
import io.swagger.models.Info; import io.swagger.models.Info;
@ -47,8 +46,6 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
protected ClientOptInput opts = null; protected ClientOptInput opts = null;
protected Swagger swagger = null; protected Swagger swagger = null;
public CodeGenStatus status = CodeGenStatus.UNRUN;
@Override @Override
public Generator opts(ClientOptInput opts) { public Generator opts(ClientOptInput opts) {
this.opts = opts; this.opts = opts;
@ -69,7 +66,6 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
Json.prettyPrint(swagger); Json.prettyPrint(swagger);
} }
List<File> files = new ArrayList<File>(); List<File> files = new ArrayList<File>();
try {
config.processOpts(); config.processOpts();
config.preprocessSwagger(swagger); config.preprocessSwagger(swagger);
@ -139,6 +135,7 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
List<String> sortedModelKeys = sortModelsByInheritance(definitions); List<String> sortedModelKeys = sortModelsByInheritance(definitions);
for (String name : sortedModelKeys) { for (String name : sortedModelKeys) {
try {
//dont generate models that have an import mapping //dont generate models that have an import mapping
if(config.importMapping().containsKey(name)) { if(config.importMapping().containsKey(name)) {
@ -173,6 +170,9 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
writeToFile(filename, tmpl.execute(models)); writeToFile(filename, tmpl.execute(models));
files.add(new File(filename)); files.add(new File(filename));
} }
} catch (Exception e) {
throw new RuntimeException("Could not generate model '" + name + "'", e);
}
} }
} }
if (System.getProperty("debugModels") != null) { if (System.getProperty("debugModels") != null) {
@ -183,6 +183,7 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
// apis // apis
Map<String, List<CodegenOperation>> paths = processPaths(swagger.getPaths()); Map<String, List<CodegenOperation>> paths = processPaths(swagger.getPaths());
for (String tag : paths.keySet()) { for (String tag : paths.keySet()) {
try {
List<CodegenOperation> ops = paths.get(tag); List<CodegenOperation> ops = paths.get(tag);
Map<String, Object> operation = processOperations(config, tag, ops); Map<String, Object> operation = processOperations(config, tag, ops);
@ -207,7 +208,6 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
} }
for (String templateName : config.apiTemplateFiles().keySet()) { for (String templateName : config.apiTemplateFiles().keySet()) {
String filename = config.apiFilename(templateName, tag); String filename = config.apiFilename(templateName, tag);
if (!config.shouldOverwrite(filename) && new File(filename).exists()) { if (!config.shouldOverwrite(filename) && new File(filename).exists()) {
continue; continue;
@ -228,6 +228,9 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
writeToFile(filename, tmpl.execute(operation)); writeToFile(filename, tmpl.execute(operation));
files.add(new File(filename)); files.add(new File(filename));
} }
} catch (Exception e) {
throw new RuntimeException("Could not generate api file for '" + tag + "'", e);
}
} }
if (System.getProperty("debugOperations") != null) { if (System.getProperty("debugOperations") != null) {
System.out.println("############ Operation info ############"); System.out.println("############ Operation info ############");
@ -273,6 +276,7 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
} }
for (SupportingFile support : config.supportingFiles()) { for (SupportingFile support : config.supportingFiles()) {
try {
String outputFolder = config.outputFolder(); String outputFolder = config.outputFolder();
if (isNotEmpty(support.folder)) { if (isNotEmpty(support.folder)) {
outputFolder += File.separator + support.folder; outputFolder += File.separator + support.folder;
@ -329,14 +333,13 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
files.add(outputFile); files.add(outputFile);
} }
} catch (Exception e) {
throw new RuntimeException("Could not generate supporting file '" + support + "'", e);
}
} }
config.processSwagger(swagger); config.processSwagger(swagger);
status = CodeGenStatus.SUCCESSFUL;
} catch (Exception e) {
status = CodeGenStatus.FAILED;
}
return files; return files;
} }
@ -508,11 +511,12 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
} }
} }
catch (Exception ex) { catch (Exception ex) {
LOGGER.error("Error while trying to get Config from Operation for tag(" + tag + ")\n" // String msg = "Could not process operation:\n" //
+ "\tResource: " + httpMethod + " " + resourcePath + "\n"// + " Tag: " + tag + "\n"//
+ "\tOperation:" + operation + "\n" // + " Operation: " + operation.getOperationId() + "\n" //
+ "\tDefinitions: " + swagger.getDefinitions() + "\n"); + " Resource: " + httpMethod + " " + resourcePath + "\n"//
ex.printStackTrace(); + " Definitions: " + swagger.getDefinitions();
throw new RuntimeException(msg, ex);
} }
} }
} }

View File

@ -1,5 +0,0 @@
package io.swagger.codegen.languages;
public enum CodeGenStatus {
UNRUN, SUCCESSFUL, FAILED
}