Mercurial > hg > thermostat-ng
changeset 32:9af3b8b15804
Update SwaggerCombine to throw an exception on I/O error condition
This patch allows exceptions to escape SwaggerCombineMain.main() if "--throw" is set, which will halt a build if SwaggerCombine is misconfigured. Errors in data do not cause an exception to be thrown.
Reviewed-by: ebaron
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-October/025563.html
author | Simon Tooke <stooke@redhat.com> |
---|---|
date | Fri, 27 Oct 2017 13:23:07 -0400 |
parents | 1a3ece19e2fd |
children | 7f94e7f977bb |
files | common/src/main/java/com/redhat/thermostat/common/swaggercombine/SwaggerCombineContext.java common/src/main/java/com/redhat/thermostat/common/swaggercombine/SwaggerCombineContextBuilder.java common/src/main/java/com/redhat/thermostat/common/swaggercombine/SwaggerCombineMain.java |
diffstat | 3 files changed, 36 insertions(+), 10 deletions(-) [+] |
line wrap: on
line diff
--- a/common/src/main/java/com/redhat/thermostat/common/swaggercombine/SwaggerCombineContext.java Wed Oct 25 10:07:39 2017 -0400 +++ b/common/src/main/java/com/redhat/thermostat/common/swaggercombine/SwaggerCombineContext.java Fri Oct 27 13:23:07 2017 -0400 @@ -50,6 +50,7 @@ public enum OutputFormat { NONE, YAML, JSON }; private OutputFormat fmt = OutputFormat.NONE; + private boolean throwExceptions = false; private boolean pretty = false; private boolean quiet = false; private boolean useTemplate = false; @@ -177,9 +178,8 @@ * add an input file (representing a microAPI defined by Swagger) * @param fin input micro API definitions * @return this - * @throws IOException if an I/O error occurred */ - public SwaggerCombineContext addMicroAPI(File fin) throws IOException { + public SwaggerCombineContext addMicroAPI(File fin) { final MicroAPI api = MicroAPI.createFromFile(fin); this.getAPIList().add(api); return this; @@ -190,9 +190,8 @@ * @param spec input micro API definitions * @param isYaml true if format of input file is YAML, false if gormat is JSON * @return this - * @throws IOException if an I/O error occurred */ - public SwaggerCombineContext addMicroAPI(String spec, boolean isYaml) throws IOException { + public SwaggerCombineContext addMicroAPI(String spec, boolean isYaml) { final MicroAPI api = isYaml ? MicroAPI.createFromYaml(spec) : MicroAPI.createFromJson(spec); this.getAPIList().add(api); @@ -241,4 +240,12 @@ return apiList; } + public boolean isThrowExceptions() { + return throwExceptions; + } + + public void setThrowExceptions(boolean throwExceptions) { + this.throwExceptions = throwExceptions; + } + }
--- a/common/src/main/java/com/redhat/thermostat/common/swaggercombine/SwaggerCombineContextBuilder.java Wed Oct 25 10:07:39 2017 -0400 +++ b/common/src/main/java/com/redhat/thermostat/common/swaggercombine/SwaggerCombineContextBuilder.java Fri Oct 27 13:23:07 2017 -0400 @@ -70,6 +70,8 @@ ctx.produce(SwaggerCombineContext.OutputFormat.JSON); } else if ("--yaml".equals(arg)) { ctx.produce(SwaggerCombineContext.OutputFormat.YAML); + } else if ("--throw".equals(arg)) { + ctx.setThrowExceptions(true); } else if (arg.startsWith("--output=")) { final String outFn = arg.substring(arg.indexOf('=') + 1); if ("-".equals(outFn)) { @@ -82,7 +84,7 @@ ctx.produce(SwaggerCombineContext.OutputFormat.YAML); } else if (outFn.endsWith(".json")) { ctx.produce(SwaggerCombineContext.OutputFormat.JSON); - } else { + } else if (!outFn.isEmpty()) { System.err.println("unable to determine output format from output extension - assuming YAML"); ctx.produce(SwaggerCombineContext.OutputFormat.YAML); }
--- a/common/src/main/java/com/redhat/thermostat/common/swaggercombine/SwaggerCombineMain.java Wed Oct 25 10:07:39 2017 -0400 +++ b/common/src/main/java/com/redhat/thermostat/common/swaggercombine/SwaggerCombineMain.java Fri Oct 27 13:23:07 2017 -0400 @@ -48,24 +48,41 @@ * command line Main method * @param args command line arguments * [--yaml] [--json [--pretty]] [--lint] [--use-template] [--quiet] [--help] infile1 [infile2] ... + * @throws IOException if "--throws" set and there was an error */ - public static void main(String[] args) { + public static void main(String[] args) throws IOException { + SwaggerCombineContext ctx = null; try { final SwaggerCombine combine = new SwaggerCombine(); - final SwaggerCombineContext ctx = new SwaggerCombineContextBuilder().buildContext(args); + ctx = new SwaggerCombineContextBuilder().buildContext(args); if (ctx.printUsage()) { - System.err.println("SwaggerCombine [--yaml] [--json [--pretty]] [--lint] [--use-template] [--quiet] [--help] [@argfile] infile1 [infile2] ..."); + System.err.println("SwaggerCombine [--yaml] [--json [--pretty]] [--lint] [--output=filename|-] [--use-template] [--quiet] [--help] [--throw] [@argfile] infile1 [infile2] ..."); } else if (ctx.getOutputFile() == null) { final Writer out = new BufferedWriter(new PrintWriter(System.out)); combine.run(ctx, out); + } else if (ctx.getOutputFile().isEmpty()) { + combine.run(ctx); } else { final Writer out = new BufferedWriter(new FileWriter(ctx.getOutputFile())); combine.run(ctx, out); out.close(); } - } catch (IOException e) { - e.printStackTrace(); + } catch (Exception e) { + // there's a chance the exception was thrown while parsing arguments, + // but before parsing any '--throw'. Run a quick check + boolean rethrow = ctx != null && ctx.isThrowExceptions(); + for (final String arg : args) { + if ("--throw".equals(arg)) { + rethrow = true; + break; + } + } + if (rethrow) { + throw e; + } else { + e.printStackTrace(); + } } } }