extract nested try block into method.
Fix this sonar issue of "Try-catch blocks should not be nested" to increase readability.
@@ -231,25 +231,12 @@ class PullBaseImageStep implements Callable<ImagesAndRegistryClient> {
eventHandlers.dispatch(LogEvent.info("trying mirror " + mirror + " for the base image"));
try (ProgressEventDispatcher progressDispatcher2 =
progressDispatcher1.newChildProducer().create("trying mirror " + mirror, 2)) {
- // First, try with no credentials. This works with public GCR images.
RegistryClient registryClient =
buildContext.newBaseImageRegistryClientFactory(mirror).newRegistryClient();
- try {
- List<Image> images =
- pullBaseImages(registryClient, progressDispatcher2.newChildProducer());
+ List<Image> images = pullPublicImages(eventHandlers, registryClient, progressDispatcher2);
eventHandlers.dispatch(LogEvent.info("pulled manifest from mirror " + mirror));
return Optional.of(new ImagesAndRegistryClient(images, registryClient));
- } catch (RegistryUnauthorizedException ex) {
- // in case if a mirror requires bearer auth
- eventHandlers.dispatch(LogEvent.debug("mirror " + mirror + " requires auth"));
- registryClient.doPullBearerAuth();
- List<Image> images =
- pullBaseImages(registryClient, progressDispatcher2.newChildProducer());
- eventHandlers.dispatch(LogEvent.info("pulled manifest from mirror " + mirror));
- return Optional.of(new ImagesAndRegistryClient(images, registryClient));
- }
-
} catch (IOException | RegistryException ex) {
// Ignore errors from this mirror and continue.
eventHandlers.dispatch(
@@ -261,6 +248,23 @@ class PullBaseImageStep implements Callable<ImagesAndRegistryClient> {
}
}
+ private List<Image> pullPublicImages(
+ EventHandlers eventHandlers,
+ RegistryClient registryClient,
+ ProgressEventDispatcher progressDispatcher)
+ throws IOException, RegistryException, LayerCountMismatchException,
+ BadContainerConfigurationFormatException {
+ try {
+ // First, try with no credentials. This works with public GCR images.
+ return pullBaseImages(registryClient, progressDispatcher.newChildProducer());
+
+ } catch (RegistryUnauthorizedException ex) {
+ // in case if a registry requires bearer auth
+ registryClient.doPullBearerAuth();
+ return pullBaseImages(registryClient, progressDispatcher.newChildProducer());
+ }
+ }
+
/**
* Pulls the base images specified in the platforms list.
*
