Skip to content

Commit 5b4f33a

Browse files
authored
Adds build exception help messages to Gradle. (GoogleContainerTools#259)
* Refactors suggestions and HelpfulMessageBuilder to HelpfulSuggestions. * Change Maven to use HelpfulSuggestions. * Adds exception suggestions to Gradle. * Removes null assertions.
1 parent aadb947 commit 5b4f33a

14 files changed

Lines changed: 361 additions & 212 deletions

File tree

‎jib-core/src/main/java/com/google/cloud/tools/jib/frontend/BuildImageStepsRunner.java‎

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@
3737

3838
/** Runs {@link BuildImageSteps} and builds helpful error messages. */
3939
public 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
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* Copyright 2018 Google LLC. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package com.google.cloud.tools.jib.frontend;
18+
19+
import java.nio.file.Path;
20+
import java.util.function.Function;
21+
22+
/** Builds messages that provides suggestions on how to fix the error. */
23+
public class HelpfulSuggestions {
24+
25+
private final String messagePrefix;
26+
private final String clearCacheCommand;
27+
private final String baseImageCredHelperConfiguration;
28+
private final Function<String, String> baseImageAuthConfiguration;
29+
private final String targetImageCredHelperConfiguration;
30+
private final Function<String, String> targetImageAuthConfiguration;
31+
32+
/**
33+
* Creates a new {@link HelpfulSuggestions} with frontend-specific texts.
34+
*
35+
* @param messagePrefix the initial message text
36+
* @param clearCacheCommand the command for clearing the cache
37+
* @param baseImageCredHelperConfiguration the configuration defining the credential helper name
38+
* for the base image
39+
* @param baseImageAuthConfiguration the way to define raw credentials for the base image - takes
40+
* the base image registry as an argument
41+
* @param targetImageCredHelperConfiguration the configuration defining the credential helper name
42+
* for the target image
43+
* @param targetImageAuthConfiguration the way to define raw credentials for the target image -
44+
* takes the target image registry as an argument
45+
*/
46+
public HelpfulSuggestions(
47+
String messagePrefix,
48+
String clearCacheCommand,
49+
String baseImageCredHelperConfiguration,
50+
Function<String, String> baseImageAuthConfiguration,
51+
String targetImageCredHelperConfiguration,
52+
Function<String, String> targetImageAuthConfiguration) {
53+
this.messagePrefix = messagePrefix;
54+
this.clearCacheCommand = clearCacheCommand;
55+
this.baseImageCredHelperConfiguration = baseImageCredHelperConfiguration;
56+
this.baseImageAuthConfiguration = baseImageAuthConfiguration;
57+
this.targetImageCredHelperConfiguration = targetImageCredHelperConfiguration;
58+
this.targetImageAuthConfiguration = targetImageAuthConfiguration;
59+
}
60+
61+
public String forCacheMetadataCorrupted() {
62+
return suggest("run '" + clearCacheCommand + "' to clear the cache");
63+
}
64+
65+
public String forHttpHostConnect() {
66+
return suggest("make sure your Internet is up and that the registry you are pushing to exists");
67+
}
68+
69+
public String forUnknownHost() {
70+
return suggest("make sure that the registry you configured exists/is spelled properly");
71+
}
72+
73+
public String forCacheDirectoryNotOwned(Path cacheDirectory) {
74+
return suggest(
75+
"check that '"
76+
+ cacheDirectory
77+
+ "' is not used by another application or set the `useOnlyProjectCache` "
78+
+ "configuration");
79+
}
80+
81+
public String forHttpStatusCodeForbidden(String imageReference) {
82+
return suggest("make sure you have permissions for " + imageReference);
83+
}
84+
85+
public String forNoCredentialHelpersDefinedForBaseImage(String registry) {
86+
return forNoCredentialHelpersDefined(
87+
baseImageCredHelperConfiguration, baseImageAuthConfiguration.apply(registry));
88+
}
89+
90+
public String forNoCredentialHelpersDefinedForTargetImage(String registry) {
91+
return forNoCredentialHelpersDefined(
92+
targetImageCredHelperConfiguration, targetImageAuthConfiguration.apply(registry));
93+
}
94+
95+
public String forCredentialsNotCorrect(String registry) {
96+
return suggest("make sure your credentials for '" + registry + "' are set up correctly");
97+
}
98+
99+
public String forDockerContextInsecureRecursiveDelete(String directory) {
100+
return suggest("clear " + directory + " manually before creating the Docker context");
101+
}
102+
103+
public String none() {
104+
return messagePrefix;
105+
}
106+
107+
/** @return the message containing a suggestion */
108+
public String suggest(String suggestion) {
109+
return messagePrefix + ", perhaps you should " + suggestion;
110+
}
111+
112+
private String forNoCredentialHelpersDefined(
113+
String credHelperConfiguration, String authConfiguration) {
114+
return suggest(
115+
"set a credential helper name with the configuration '"
116+
+ credHelperConfiguration
117+
+ "' or "
118+
+ authConfiguration);
119+
}
120+
}

0 commit comments

Comments
 (0)