2424import com .google .cloud .tools .jib .cache .CacheDirectoryNotOwnedException ;
2525import com .google .cloud .tools .jib .cache .CacheMetadataCorruptedException ;
2626import com .google .cloud .tools .jib .cache .Caches ;
27+ import com .google .cloud .tools .jib .frontend .HelpfulMessageBuilder ;
2728import com .google .cloud .tools .jib .http .Authorization ;
2829import com .google .cloud .tools .jib .http .Authorizations ;
2930import com .google .cloud .tools .jib .image .ImageReference ;
@@ -82,6 +83,9 @@ private Class<? extends BuildableManifestTemplate> getManifestTemplateClass() {
8283 /** {@code User-Agent} header suffix to send to the registry. */
8384 private static final String USER_AGENT_SUFFIX = "jib-maven-plugin" ;
8485
86+ private static final HelpfulMessageBuilder helpfulMessageBuilder =
87+ new HelpfulMessageBuilder ("Build image failed" );
88+
8589 @ Nullable
8690 @ Parameter (defaultValue = "${session}" , readonly = true )
8791 private MavenSession session ;
@@ -101,9 +105,10 @@ public void execute() throws MojoExecutionException, MojoFailureException {
101105 if (mainClass == null ) {
102106 mainClass = projectProperties .getMainClassFromMavenJarPlugin ();
103107 if (mainClass == null ) {
104- throwMojoExecutionExceptionWithHelpMessage (
105- new MojoFailureException ("Could not find main class specified in maven-jar-plugin" ),
106- "add a `mainClass` configuration to jib-maven-plugin" );
108+ throw new MojoExecutionException (
109+ helpfulMessageBuilder .withSuggestion (
110+ "add a `mainClass` configuration to jib-maven-plugin" ),
111+ new MojoFailureException ("Could not find main class specified in maven-jar-plugin" ));
107112 }
108113 }
109114 Preconditions .checkNotNull (mainClass );
@@ -190,17 +195,19 @@ void buildImage(BuildImageSteps buildImageSteps) throws MojoExecutionException {
190195 buildImageSteps .run ();
191196
192197 } catch (CacheMetadataCorruptedException cacheMetadataCorruptedException ) {
193- throwMojoExecutionExceptionWithHelpMessage (
194- cacheMetadataCorruptedException , "run 'mvn clean' to clear the cache" );
198+ throw new MojoExecutionException (
199+ helpfulMessageBuilder .withSuggestion ("run 'mvn clean' to clear the cache" ),
200+ cacheMetadataCorruptedException );
195201
196202 } catch (ExecutionException executionException ) {
197203 BuildConfiguration buildConfiguration = buildImageSteps .getBuildConfiguration ();
198204
199205 if (executionException .getCause () instanceof HttpHostConnectException ) {
200206 // Failed to connect to registry.
201- throwMojoExecutionExceptionWithHelpMessage (
202- executionException .getCause (),
203- "make sure your Internet is up and that the registry you are pushing to exists" );
207+ throw new MojoExecutionException (
208+ helpfulMessageBuilder .withSuggestion (
209+ "make sure your Internet is up and that the registry you are pushing to exists" ),
210+ executionException .getCause ());
204211
205212 } else if (executionException .getCause () instanceof RegistryUnauthorizedException ) {
206213 handleRegistryUnauthorizedException (
@@ -216,26 +223,29 @@ void buildImage(BuildImageSteps buildImageSteps) throws MojoExecutionException {
216223 buildConfiguration );
217224
218225 } else if (executionException .getCause () instanceof UnknownHostException ) {
219- throwMojoExecutionExceptionWithHelpMessage (
220- executionException .getCause (),
221- "make sure that the registry you configured exists/is spelled properly" );
226+ throw new MojoExecutionException (
227+ helpfulMessageBuilder .withSuggestion (
228+ "make sure that the registry you configured exists/is spelled properly" ),
229+ executionException .getCause ());
222230
223231 } else {
224- throwMojoExecutionExceptionWithHelpMessage (executionException .getCause (), null );
232+ throw new MojoExecutionException (
233+ helpfulMessageBuilder .withNoHelp (), executionException .getCause ());
225234 }
226235
227236 } catch (InterruptedException | IOException ex ) {
228237 getLog ().error (ex );
229238 // TODO: Add more suggestions for various build failures.
230- throwMojoExecutionExceptionWithHelpMessage ( ex , null );
239+ throw new MojoExecutionException ( helpfulMessageBuilder . withNoHelp (), ex );
231240
232241 } catch (CacheDirectoryNotOwnedException ex ) {
233- throwMojoExecutionExceptionWithHelpMessage (
234- ex ,
235- "check that '"
236- + ex .getCacheDirectory ()
237- + "' is not used by another application or set the `useOnlyProjectCache` "
238- + "configuration" );
242+ throw new MojoExecutionException (
243+ helpfulMessageBuilder .withSuggestion (
244+ "check that '"
245+ + ex .getCacheDirectory ()
246+ + "' is not used by another application or set the `useOnlyProjectCache` "
247+ + "configuration" ),
248+ ex );
239249 }
240250 }
241251
@@ -319,10 +329,11 @@ private void handleRegistryUnauthorizedException(
319329 if (registryUnauthorizedException .getHttpResponseException ().getStatusCode ()
320330 == HttpStatusCodes .STATUS_CODE_FORBIDDEN ) {
321331 // No permissions for registry/repository.
322- throwMojoExecutionExceptionWithHelpMessage (
323- registryUnauthorizedException ,
324- "make sure you have permissions for "
325- + registryUnauthorizedException .getImageReference ());
332+ throw new MojoExecutionException (
333+ helpfulMessageBuilder .withSuggestion (
334+ "make sure you have permissions for "
335+ + registryUnauthorizedException .getImageReference ()),
336+ registryUnauthorizedException );
326337
327338 } else if ((buildConfiguration .getCredentialHelperNames () == null
328339 || buildConfiguration .getCredentialHelperNames ().isEmpty ())
@@ -331,35 +342,23 @@ private void handleRegistryUnauthorizedException(
331342 .getKnownRegistryCredentials ()
332343 .has (registryUnauthorizedException .getRegistry ()))) {
333344 // No credential helpers defined.
334- throwMojoExecutionExceptionWithHelpMessage (
335- registryUnauthorizedException ,
336- "set a credential helper name with the configuration 'credHelpers' or "
337- + "set credentials for '"
338- + registryUnauthorizedException .getRegistry ()
339- + "' in your Maven settings" );
345+ throw new MojoExecutionException (
346+ helpfulMessageBuilder .withSuggestion (
347+ "set a credential helper name with the configuration 'credHelpers' or "
348+ + "set credentials for '"
349+ + registryUnauthorizedException .getRegistry ()
350+ + "' in your Maven settings" ),
351+ registryUnauthorizedException );
340352
341353 } else {
342354 // Credential helper probably was not configured correctly or did not have the necessary
343355 // credentials.
344- throwMojoExecutionExceptionWithHelpMessage (
345- registryUnauthorizedException ,
346- "make sure your credentials for '"
347- + registryUnauthorizedException .getRegistry ()
348- + "' are set up correctly" );
349- }
350- }
351-
352- /**
353- * Wraps an exception in a {@link MojoExecutionException} and provides a suggestion on how to fix
354- * the error.
355- */
356- private <T extends Throwable > void throwMojoExecutionExceptionWithHelpMessage (
357- T ex , @ Nullable String suggestion ) throws MojoExecutionException {
358- StringBuilder message = new StringBuilder ("Build image failed" );
359- if (suggestion != null ) {
360- message .append (", perhaps you should " );
361- message .append (suggestion );
356+ throw new MojoExecutionException (
357+ helpfulMessageBuilder .withSuggestion (
358+ "make sure your credentials for '"
359+ + registryUnauthorizedException .getRegistry ()
360+ + "' are set up correctly" ),
361+ registryUnauthorizedException );
362362 }
363- throw new MojoExecutionException (message .toString (), ex );
364363 }
365364}
0 commit comments