Search in sources :

Example 1 with ExceptionWithHumanReadableMessage

use of com.facebook.buck.util.ExceptionWithHumanReadableMessage in project buck by facebook.

the class Build method executeAndPrintFailuresToEventBus.

public int executeAndPrintFailuresToEventBus(Iterable<BuildTarget> targetsish, boolean isKeepGoing, BuckEventBus eventBus, Console console, Optional<Path> pathToBuildReport) throws InterruptedException {
    int exitCode;
    try {
        try {
            BuildExecutionResult buildExecutionResult = executeBuild(targetsish, isKeepGoing);
            SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(ruleResolver));
            BuildReport buildReport = new BuildReport(buildExecutionResult, pathResolver);
            if (isKeepGoing) {
                String buildReportText = buildReport.generateForConsole(console);
                buildReportText = buildReportText.isEmpty() ? "Failure report is empty." : // Remove trailing newline from build report.
                buildReportText.substring(0, buildReportText.length() - 1);
                eventBus.post(ConsoleEvent.info(buildReportText));
                exitCode = buildExecutionResult.getFailures().isEmpty() ? 0 : 1;
                if (exitCode != 0) {
                    eventBus.post(ConsoleEvent.severe("Not all rules succeeded."));
                }
            } else {
                exitCode = 0;
            }
            if (pathToBuildReport.isPresent()) {
                // Note that pathToBuildReport is an absolute path that may exist outside of the project
                // root, so it is not appropriate to use ProjectFilesystem to write the output.
                String jsonBuildReport = buildReport.generateJsonBuildReport();
                try {
                    Files.write(jsonBuildReport, pathToBuildReport.get().toFile(), Charsets.UTF_8);
                } catch (IOException e) {
                    eventBus.post(ThrowableConsoleEvent.create(e, "Failed writing report"));
                    exitCode = 1;
                }
            }
        } catch (ExecutionException | RuntimeException e) {
            // This is likely a checked exception that was caught while building a build rule.
            Throwable cause = e.getCause();
            if (cause == null) {
                Throwables.throwIfInstanceOf(e, RuntimeException.class);
                throw new RuntimeException(e);
            }
            Throwables.throwIfInstanceOf(cause, IOException.class);
            Throwables.throwIfInstanceOf(cause, StepFailedException.class);
            Throwables.throwIfInstanceOf(cause, InterruptedException.class);
            Throwables.throwIfInstanceOf(cause, ClosedByInterruptException.class);
            Throwables.throwIfInstanceOf(cause, HumanReadableException.class);
            if (cause instanceof ExceptionWithHumanReadableMessage) {
                throw new HumanReadableException((ExceptionWithHumanReadableMessage) cause);
            }
            LOG.debug(e, "Got an exception during the build.");
            throw new RuntimeException(e);
        }
    } catch (IOException e) {
        LOG.debug(e, "Got an exception during the build.");
        eventBus.post(ConsoleEvent.severe(getFailureMessageWithClassName(e)));
        exitCode = 1;
    } catch (StepFailedException e) {
        LOG.debug(e, "Got an exception during the build.");
        eventBus.post(ConsoleEvent.severe(getFailureMessage(e)));
        exitCode = 1;
    }
    return exitCode;
}
Also used : IOException(java.io.IOException) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) StepFailedException(com.facebook.buck.step.StepFailedException) HumanReadableException(com.facebook.buck.util.HumanReadableException) ExceptionWithHumanReadableMessage(com.facebook.buck.util.ExceptionWithHumanReadableMessage) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)1 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)1 StepFailedException (com.facebook.buck.step.StepFailedException)1 ExceptionWithHumanReadableMessage (com.facebook.buck.util.ExceptionWithHumanReadableMessage)1 HumanReadableException (com.facebook.buck.util.HumanReadableException)1 IOException (java.io.IOException)1 ClosedByInterruptException (java.nio.channels.ClosedByInterruptException)1 ExecutionException (java.util.concurrent.ExecutionException)1