Search in sources :

Example 1 with PatternAwareOutputParser

use of com.android.ide.common.blame.parser.PatternAwareOutputParser in project android by JetBrains.

the class AndroidGradleTargetBuilder method handleBuildException.

/**
   * Something went wrong while invoking Gradle. Since we cannot distinguish an execution error from compilation errors easily, we first try
   * to show, in the "Problems" view, compilation errors by parsing the error output. If no errors are found, we show the stack trace in the
   * "Problems" view. The idea is that we need to somehow inform the user that something went wrong.
   */
private static void handleBuildException(BuildException e, CompileContext context, String stdErr) throws ProjectBuildException {
    Iterable<PatternAwareOutputParser> parsers = JpsServiceManager.getInstance().getExtensions(PatternAwareOutputParser.class);
    Collection<Message> compilerMessages = new BuildOutputParser(parsers).parseGradleOutput(stdErr);
    if (!compilerMessages.isEmpty()) {
        boolean hasError = false;
        for (Message message : compilerMessages) {
            if (message.getKind() == Message.Kind.ERROR) {
                hasError = true;
            }
            for (CompilerMessage compilerMessage : AndroidGradleJps.createCompilerMessages(message)) {
                context.processMessage(compilerMessage);
            }
        }
        if (hasError) {
            return;
        }
    }
    // There are no error messages to present. Show some feedback indicating that something went wrong.
    if (!stdErr.isEmpty()) {
        // Show the contents of stderr as a compiler error.
        context.processMessage(createCompilerErrorMessage(stdErr));
    } else {
        // Since we have nothing else to show, just print the stack trace of the caught exception.
        ByteArrayOutputStream out = new ByteArrayOutputStream(BUFFER_SIZE);
        try {
            //noinspection IOResourceOpenedButNotSafelyClosed
            e.printStackTrace(new PrintStream(out));
            String message = "Internal error:" + SystemProperties.getLineSeparator() + out.toString();
            context.processMessage(createCompilerErrorMessage(message));
        } finally {
            try {
                Closeables.close(out, true);
            } catch (IOException e1) {
                LOG.debug(e1);
            }
        }
    }
    throw new ProjectBuildException(e.getMessage());
}
Also used : ProjectBuildException(org.jetbrains.jps.incremental.ProjectBuildException) BuildOutputParser(com.android.tools.idea.gradle.output.parser.BuildOutputParser) PrintStream(java.io.PrintStream) CompilerMessage(org.jetbrains.jps.incremental.messages.CompilerMessage) Message(com.android.ide.common.blame.Message) CompilerMessage(org.jetbrains.jps.incremental.messages.CompilerMessage) ProgressMessage(org.jetbrains.jps.incremental.messages.ProgressMessage) BuildMessage(org.jetbrains.jps.incremental.messages.BuildMessage) PatternAwareOutputParser(com.android.ide.common.blame.parser.PatternAwareOutputParser) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Aggregations

Message (com.android.ide.common.blame.Message)1 PatternAwareOutputParser (com.android.ide.common.blame.parser.PatternAwareOutputParser)1 BuildOutputParser (com.android.tools.idea.gradle.output.parser.BuildOutputParser)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 PrintStream (java.io.PrintStream)1 ProjectBuildException (org.jetbrains.jps.incremental.ProjectBuildException)1 BuildMessage (org.jetbrains.jps.incremental.messages.BuildMessage)1 CompilerMessage (org.jetbrains.jps.incremental.messages.CompilerMessage)1 ProgressMessage (org.jetbrains.jps.incremental.messages.ProgressMessage)1