diff options
author | corysmith <corysmith@google.com> | 2017-11-30 08:55:49 -0800 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2017-11-30 08:58:15 -0800 |
commit | 39c2a36bf8f502d55addd58e9e97bf2ebb46e4b0 (patch) | |
tree | d03277d06ea07cac689e88502b81949c5b6630c7 /src/tools/android/java/com/google/devtools/build/android/aapt2 | |
parent | 38bd6d7e205df20bc94c8c7359cee81e500cdd16 (diff) |
Improve the error messaging by suppressing stack traces for expected errors.
RELNOTES: None
PiperOrigin-RevId: 177460834
Diffstat (limited to 'src/tools/android/java/com/google/devtools/build/android/aapt2')
3 files changed, 67 insertions, 5 deletions
diff --git a/src/tools/android/java/com/google/devtools/build/android/aapt2/Aapt2Exception.java b/src/tools/android/java/com/google/devtools/build/android/aapt2/Aapt2Exception.java new file mode 100644 index 0000000000..4781698e8c --- /dev/null +++ b/src/tools/android/java/com/google/devtools/build/android/aapt2/Aapt2Exception.java @@ -0,0 +1,27 @@ +// Copyright 2017 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// Copyright 2017 The Bazel Authors. All rights reserved. + +package com.google.devtools.build.android.aapt2; + +/** Represents an error found during an aapt2 execution pass. */ +public abstract class Aapt2Exception extends RuntimeException { + protected Aapt2Exception(Throwable e) { + super(e); + } + + public Aapt2Exception() { + super(); + } +} diff --git a/src/tools/android/java/com/google/devtools/build/android/aapt2/ResourceCompiler.java b/src/tools/android/java/com/google/devtools/build/android/aapt2/ResourceCompiler.java index 5f8b842859..e51192ce1e 100644 --- a/src/tools/android/java/com/google/devtools/build/android/aapt2/ResourceCompiler.java +++ b/src/tools/android/java/com/google/devtools/build/android/aapt2/ResourceCompiler.java @@ -17,7 +17,8 @@ package com.google.devtools.build.android.aapt2; import com.android.builder.core.VariantType; import com.android.repository.Revision; import com.google.common.base.Preconditions; -import com.google.common.util.concurrent.Futures; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableList.Builder; import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.ListeningExecutorService; import com.google.devtools.build.android.AaptCommandBuilder; @@ -29,12 +30,30 @@ import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; import java.util.ArrayList; import java.util.List; +import java.util.Optional; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.logging.Logger; /** Invokes aapt2 to compile resources. */ public class ResourceCompiler { + static class CompileError extends Aapt2Exception { + + protected CompileError(Throwable e) { + super(e); + } + + private CompileError() { + super(); + } + + public static CompileError of(List<Throwable> compilationErrors) { + final CompileError compileError = new CompileError(); + compilationErrors.forEach(compileError::addSuppressed); + return compileError; + } + } + private static final Logger logger = Logger.getLogger(ResourceCompiler.class.getName()); private final CompilingVisitor compilingVisitor; @@ -131,7 +150,19 @@ public class ResourceCompiler { } List<Path> getCompiledArtifacts() throws InterruptedException, ExecutionException { - return Futures.allAsList(tasks).get(); + Builder<Path> builder = ImmutableList.builder(); + List<Throwable> compilationErrors = new ArrayList<>(); + for (ListenableFuture<Path> task : tasks) { + try { + builder.add(task.get()); + } catch (InterruptedException | ExecutionException e) { + compilationErrors.add(Optional.ofNullable(e.getCause()).orElse(e)); + } + } + if (compilationErrors.isEmpty()) { + return builder.build(); + } + throw CompileError.of(compilationErrors); } } diff --git a/src/tools/android/java/com/google/devtools/build/android/aapt2/ResourceLinker.java b/src/tools/android/java/com/google/devtools/build/android/aapt2/ResourceLinker.java index 2787722c36..99af38766b 100644 --- a/src/tools/android/java/com/google/devtools/build/android/aapt2/ResourceLinker.java +++ b/src/tools/android/java/com/google/devtools/build/android/aapt2/ResourceLinker.java @@ -39,11 +39,15 @@ import java.util.stream.Collectors; public class ResourceLinker { /** Represents errors thrown during linking. */ - public static class LinkError extends RuntimeException { + public static class LinkError extends Aapt2Exception { - public LinkError(Throwable e) { + private LinkError(Throwable e) { super(e); } + + public static LinkError of(IOException e) { + return new LinkError(e); + } } private static Logger logger = Logger.getLogger(ResourceLinker.class.getName()); @@ -192,7 +196,7 @@ public class ResourceLinker { profiler.recordEndOf("sourcejar"); return StaticLibrary.from(outPath, rTxt, ImmutableList.of(), sourceJar); } catch (IOException e) { - throw new LinkError(e); + throw LinkError.of(e); } } |