From fd5fbfff79acf10bdfabac34757cad3bff9d963b Mon Sep 17 00:00:00 2001 From: Michel Davit Date: Mon, 11 Nov 2024 09:12:34 +0100 Subject: [PATCH] Improve error logging (#213) --- .../github/sbt/avro/AvscFilesCompiler.java | 31 +++++-------------- .../scala/com/github/sbt/avro/SbtAvro.scala | 30 ++++++++++-------- 2 files changed, 25 insertions(+), 36 deletions(-) diff --git a/bridge/src/main/java/com/github/sbt/avro/AvscFilesCompiler.java b/bridge/src/main/java/com/github/sbt/avro/AvscFilesCompiler.java index 7abcbc5..dce30b9 100644 --- a/bridge/src/main/java/com/github/sbt/avro/AvscFilesCompiler.java +++ b/bridge/src/main/java/com/github/sbt/avro/AvscFilesCompiler.java @@ -12,6 +12,7 @@ import java.util.*; import java.util.function.Supplier; import java.util.stream.Collectors; +import java.util.stream.Stream; public class AvscFilesCompiler { @@ -55,18 +56,10 @@ public void compileFiles(Set files, File outputDirectory) { if (!uncompiledFiles.isEmpty()) { String failedFiles = uncompiledFiles.stream() - .map(AvroFileRef::toString) - .collect(Collectors.joining(", ")); - SchemaGenerationException ex = new SchemaGenerationException( - String.format("Can not compile schema files: %s", failedFiles)); + .flatMap(f -> Stream.ofNullable(compileExceptions.get(f)).map(e -> f.getFile().getName() + ": " + e.getMessage())) + .collect(Collectors.joining(",\n")); - for (AvroFileRef file : uncompiledFiles) { - Exception e = compileExceptions.get(file); - if (e != null) { - ex.addSuppressed(e); - } - } - throw ex; + throw new SchemaGenerationException("Can not compile schema files:\n" + failedFiles); } } @@ -92,19 +85,11 @@ public void compileClasses(Set> classes, File ou } if (!uncompiledClasses.isEmpty()) { - String failedFiles = uncompiledClasses.stream() + String failedClasses = uncompiledClasses.stream() .map(Class::toString) - .collect(Collectors.joining(", ")); - SchemaGenerationException ex = new SchemaGenerationException( - String.format("Can not re-compile class: %s", failedFiles)); - - for (Class clazz : uncompiledClasses) { - Exception e = compileExceptions.get(clazz); - if (e != null) { - ex.addSuppressed(e); - } - } - throw ex; + .flatMap(c -> Stream.ofNullable(compileExceptions.get(c)).map(e -> c + ": " + e.getMessage())) + .collect(Collectors.joining(",\n")); + throw new SchemaGenerationException("Can not re-compile classes:\n" + failedClasses); } } diff --git a/plugin/src/main/scala/com/github/sbt/avro/SbtAvro.scala b/plugin/src/main/scala/com/github/sbt/avro/SbtAvro.scala index ce5b298..d655bea 100644 --- a/plugin/src/main/scala/com/github/sbt/avro/SbtAvro.scala +++ b/plugin/src/main/scala/com/github/sbt/avro/SbtAvro.scala @@ -245,26 +245,30 @@ object SbtAvro extends AutoPlugin { compiler.setCreateSetters(avroCreateSetters.value) compiler.setOptionalGetters(avroOptionalGetters.value) - try { - val recs = records.map(avroClassLoader.loadClass) - val avdls = srcDirs.flatMap(d => (d ** AvroAvdlFilter).get()) - val avscs = srcDirs.flatMap(d => - (d ** AvroAvscFilter) - .get() - .map(avsc => new AvroFileRef(d, avsc.relativeTo(d).get.toString)) - ) - val avprs = srcDirs.flatMap(d => (d ** AvroAvrpFilter).get()) - - out.log.info( - s"Avro compiler ${avroVersion.value} using stringType=${avroStringType.value}" - ) + val recs = records.map(avroClassLoader.loadClass) + val avdls = srcDirs.flatMap(d => (d ** AvroAvdlFilter).get()) + val avscs = srcDirs.flatMap(d => + (d ** AvroAvscFilter) + .get() + .map(avsc => new AvroFileRef(d, avsc.relativeTo(d).get.toString)) + ) + val avprs = srcDirs.flatMap(d => (d ** AvroAvrpFilter).get()) + out.log.info( + s"Avro compiler ${avroVersion.value} using stringType=${avroStringType.value}" + ) + try { compiler.recompile(recs.toArray, outDir) compiler.compileIdls(avdls.toArray, outDir) compiler.compileAvscs(avscs.toArray, outDir) compiler.compileAvprs(avprs.toArray, outDir) (outDir ** SbtAvro.JavaFileFilter).get().toSet + } catch { + case e: Exception => + out.log.err(e.getMessage) + // avoid stacktrace in sbt + throw new AlreadyHandledException(e) } finally { avroClassLoader.close() }