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