public JibContainer runBuild()
      throws BuildStepsExecutionException, IOException, CacheDirectoryCreationException {
    try {
      logger.accept(LogEvent.lifecycle(""));
      logger.accept(LogEvent.lifecycle(startupMessage));

      JibContainer jibContainer = jibContainerBuilder.containerize(containerizer);

      logger.accept(LogEvent.lifecycle(""));
      logger.accept(LogEvent.lifecycle(successMessage));

      // when an image is built, write out the digest and id
      if (imageDigestOutputPath != null) {
        String imageDigest = jibContainer.getDigest().toString();
        Files.write(imageDigestOutputPath, imageDigest.getBytes(StandardCharsets.UTF_8));
      }
      if (imageIdOutputPath != null) {
        String imageId = jibContainer.getImageId().toString();
        Files.write(imageIdOutputPath, imageId.getBytes(StandardCharsets.UTF_8));
      }
      if (imageJsonOutputPath != null) {
        ImageMetadataOutput metadataOutput = ImageMetadataOutput.fromJibContainer(jibContainer);
        String imageJson = metadataOutput.toJson();
        Files.write(imageJsonOutputPath, imageJson.getBytes(StandardCharsets.UTF_8));
      }

      return jibContainer;

    } catch (HttpHostConnectException ex) {
      // Failed to connect to registry.
      throw new BuildStepsExecutionException(helpfulSuggestions.forHttpHostConnect(), ex);

    } catch (RegistryUnauthorizedException ex) {
      handleRegistryUnauthorizedException(ex, helpfulSuggestions);

    } catch (RegistryCredentialsNotSentException ex) {
      throw new BuildStepsExecutionException(helpfulSuggestions.forCredentialsNotSent(), ex);

    } catch (RegistryAuthenticationFailedException ex) {
      if (ex.getCause() instanceof ResponseException) {
        handleRegistryUnauthorizedException(
            new RegistryUnauthorizedException(
                ex.getServerUrl(), ex.getImageName(), (ResponseException) ex.getCause()),
            helpfulSuggestions);
      } else {
        // Unknown cause
        throw new BuildStepsExecutionException(helpfulSuggestions.none(), ex);
      }

    } catch (UnknownHostException ex) {
      throw new BuildStepsExecutionException(helpfulSuggestions.forUnknownHost(), ex);

    } catch (InsecureRegistryException ex) {
      throw new BuildStepsExecutionException(helpfulSuggestions.forInsecureRegistry(), ex);

    } catch (RegistryException ex) {
      String message = Verify.verifyNotNull(ex.getMessage()); // keep null-away happy
      throw new BuildStepsExecutionException(message, ex);

    } catch (ExecutionException ex) {
      String message = ex.getCause().getMessage();
      throw new BuildStepsExecutionException(
          message == null ? "(null exception message)" : message, ex.getCause());

    } catch (InterruptedException ex) {
      Thread.currentThread().interrupt();
      throw new BuildStepsExecutionException(helpfulSuggestions.none(), ex);
    }

    throw new IllegalStateException("unreachable");
  }
@Test
  public void testBuildImage_httpHostConnectException()
      throws InterruptedException, IOException, CacheDirectoryCreationException, RegistryException,
          ExecutionException {
    HttpHostConnectException mockHttpHostConnectException =
        Mockito.mock(HttpHostConnectException.class);
    Mockito.doThrow(mockHttpHostConnectException)
        .when(mockJibContainerBuilder)
        .containerize(mockContainerizer);

    try {
      testJibBuildRunner.runBuild();
      Assert.fail();

    } catch (BuildStepsExecutionException ex) {
      Assert.assertEquals(TEST_HELPFUL_SUGGESTIONS.forHttpHostConnect(), ex.getMessage());
    }
  }