3737
3838/** Runs {@link BuildImageSteps} and builds helpful error messages. */
3939public class BuildImageStepsRunner {
40-
41- private static final HelpfulMessageBuilder helpfulMessageBuilder =
42- new HelpfulMessageBuilder ("Build image failed" );
43-
4440 /**
4541 * Sets up a new {@link BuildImageStepsRunner}. Creates the directory for the cache, if needed.
4642 *
@@ -86,7 +82,13 @@ private BuildImageStepsRunner(
8682 () -> new BuildImageSteps (buildConfiguration , sourceFilesConfiguration , cachesInitializer );
8783 }
8884
89- public void buildImage () throws BuildImageStepsExecutionException {
85+ /**
86+ * Runs the {@link BuildImageSteps}.
87+ *
88+ * @param helpfulSuggestions suggestions to use in help messages for exceptions
89+ */
90+ public void buildImage (HelpfulSuggestions helpfulSuggestions )
91+ throws BuildImageStepsExecutionException {
9092 BuildImageSteps buildImageSteps = buildImageStepsSupplier .get ();
9193
9294 try {
@@ -95,22 +97,21 @@ public void buildImage() throws BuildImageStepsExecutionException {
9597 } catch (CacheMetadataCorruptedException cacheMetadataCorruptedException ) {
9698 // TODO: Have this be different for Maven and Gradle.
9799 throw new BuildImageStepsExecutionException (
98- helpfulMessageBuilder .withSuggestion ("run 'mvn clean' to clear the cache" ),
99- cacheMetadataCorruptedException );
100+ helpfulSuggestions .forCacheMetadataCorrupted (), cacheMetadataCorruptedException );
100101
101102 } catch (ExecutionException executionException ) {
102103 BuildConfiguration buildConfiguration = buildImageSteps .getBuildConfiguration ();
103104
104105 if (executionException .getCause () instanceof HttpHostConnectException ) {
105106 // Failed to connect to registry.
106107 throw new BuildImageStepsExecutionException (
107- helpfulMessageBuilder .withSuggestion (
108- "make sure your Internet is up and that the registry you are pushing to exists" ),
109- executionException .getCause ());
108+ helpfulSuggestions .forHttpHostConnect (), executionException .getCause ());
110109
111110 } else if (executionException .getCause () instanceof RegistryUnauthorizedException ) {
112111 handleRegistryUnauthorizedException (
113- (RegistryUnauthorizedException ) executionException .getCause (), buildConfiguration );
112+ (RegistryUnauthorizedException ) executionException .getCause (),
113+ buildConfiguration ,
114+ helpfulSuggestions );
114115
115116 } else if (executionException .getCause () instanceof RegistryAuthenticationFailedException
116117 && executionException .getCause ().getCause () instanceof HttpResponseException ) {
@@ -119,45 +120,39 @@ public void buildImage() throws BuildImageStepsExecutionException {
119120 buildConfiguration .getTargetImageRegistry (),
120121 buildConfiguration .getTargetImageRepository (),
121122 (HttpResponseException ) executionException .getCause ().getCause ()),
122- buildConfiguration );
123+ buildConfiguration ,
124+ helpfulSuggestions );
123125
124126 } else if (executionException .getCause () instanceof UnknownHostException ) {
125127 throw new BuildImageStepsExecutionException (
126- helpfulMessageBuilder .withSuggestion (
127- "make sure that the registry you configured exists/is spelled properly" ),
128- executionException .getCause ());
128+ helpfulSuggestions .forUnknownHost (), executionException .getCause ());
129129
130130 } else {
131131 throw new BuildImageStepsExecutionException (
132- helpfulMessageBuilder . withNoHelp (), executionException .getCause ());
132+ helpfulSuggestions . none (), executionException .getCause ());
133133 }
134134
135135 } catch (InterruptedException | IOException ex ) {
136136 // TODO: Add more suggestions for various build failures.
137- throw new BuildImageStepsExecutionException (helpfulMessageBuilder . withNoHelp (), ex );
137+ throw new BuildImageStepsExecutionException (helpfulSuggestions . none (), ex );
138138
139139 } catch (CacheDirectoryNotOwnedException ex ) {
140140 throw new BuildImageStepsExecutionException (
141- helpfulMessageBuilder .withSuggestion (
142- "check that '"
143- + ex .getCacheDirectory ()
144- + "' is not used by another application or set the `useOnlyProjectCache` "
145- + "configuration" ),
146- ex );
141+ helpfulSuggestions .forCacheDirectoryNotOwned (ex .getCacheDirectory ()), ex );
147142 }
148143 }
149144
150145 private void handleRegistryUnauthorizedException (
151146 RegistryUnauthorizedException registryUnauthorizedException ,
152- BuildConfiguration buildConfiguration )
147+ BuildConfiguration buildConfiguration ,
148+ HelpfulSuggestions helpfulSuggestions )
153149 throws BuildImageStepsExecutionException {
154150 if (registryUnauthorizedException .getHttpResponseException ().getStatusCode ()
155151 == HttpStatusCodes .STATUS_CODE_FORBIDDEN ) {
156152 // No permissions for registry/repository.
157153 throw new BuildImageStepsExecutionException (
158- helpfulMessageBuilder .withSuggestion (
159- "make sure you have permissions for "
160- + registryUnauthorizedException .getImageReference ()),
154+ helpfulSuggestions .forHttpStatusCodeForbidden (
155+ registryUnauthorizedException .getImageReference ()),
161156 registryUnauthorizedException );
162157
163158 } else {
@@ -176,25 +171,23 @@ private void handleRegistryUnauthorizedException(
176171 buildConfiguration .getTargetImageCredentialHelperName () != null
177172 || buildConfiguration .getKnownTargetRegistryCredentials () != null ;
178173
179- if ((isRegistryForBase && !areBaseImageCredentialsConfigured )
180- || (isRegistryForTarget && !areTargetImageCredentialsConfigured )) {
181- // No credential helpers defined.
174+ if (isRegistryForBase && !areBaseImageCredentialsConfigured ) {
175+ throw new BuildImageStepsExecutionException (
176+ helpfulSuggestions .forNoCredentialHelpersDefinedForBaseImage (
177+ registryUnauthorizedException .getRegistry ()),
178+ registryUnauthorizedException );
179+ }
180+ if (isRegistryForTarget && !areTargetImageCredentialsConfigured ) {
182181 throw new BuildImageStepsExecutionException (
183- helpfulMessageBuilder .withSuggestion (
184- "set a credential helper name with the configuration 'credHelpers' or "
185- + "set credentials for '"
186- + registryUnauthorizedException .getRegistry ()
187- + "' in your Maven settings" ),
182+ helpfulSuggestions .forNoCredentialHelpersDefinedForTargetImage (
183+ registryUnauthorizedException .getRegistry ()),
188184 registryUnauthorizedException );
189185 }
190186
191187 // Credential helper probably was not configured correctly or did not have the necessary
192188 // credentials.
193189 throw new BuildImageStepsExecutionException (
194- helpfulMessageBuilder .withSuggestion (
195- "make sure your credentials for '"
196- + registryUnauthorizedException .getRegistry ()
197- + "' are set up correctly" ),
190+ helpfulSuggestions .forCredentialsNotCorrect (registryUnauthorizedException .getRegistry ()),
198191 registryUnauthorizedException );
199192 }
200193 }
0 commit comments