From 38e49e6c8efd8cab4ced0cffd2725f8118cb03a9 Mon Sep 17 00:00:00 2001 From: WorkingAmeise Date: Fri, 27 Sep 2024 13:48:48 +0200 Subject: [PATCH] Restructured ussages of the OfflineException --- CHANGELOG.adoc | 3 +- .../tools/ide/cli/CliOfflineException.java | 50 ++++++++++++++++++- .../tools/ide/context/GitContextImpl.java | 2 +- .../devonfw/tools/ide/context/IdeContext.java | 2 +- .../devonfw/tools/ide/io/FileAccessImpl.java | 4 +- .../ide/repo/AbstractToolRepository.java | 2 +- 6 files changed, 55 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 62778dbb7..ce949194c 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -26,12 +26,13 @@ Release with new features and bugfixes: * https://github.com/devonfw/IDEasy/issues/52[#52]: Adjusting Intellij settings in ide-settings * https://github.com/devonfw/IDEasy/issues/588[#588]: ide create installs wrong Java version * https://github.com/devonfw/IDEasy/issues/650[#650]: Improve default success message of step -* https://github.com/devonfw/IDEasy/issues/593[#593]: Tool error reporting still buggy +* https://github.com/devonfw/IDEasy/issues/593[#593]: Tool error reporting still buggy * https://github.com/devonfw/IDEasy/issues/651[#651]: IDE not started in background anymore * https://github.com/devonfw/IDEasy/issues/439[#439]: Refactor and improve tool-dependencies and tomcat * https://github.com/devonfw/IDEasy/issues/356[#356]: Eclipse plugin installation opens an Eclipse window for each plugin installed * https://github.com/devonfw/IDEasy/issues/655[#655]: CVE-2024-26308 and library updates * https://github.com/devonfw/IDEasy/issues/627[#627]: Still log messages break processable command output +* https://github.com/devonfw/IDEasy/issues/525[#525]: Added online state check before downloading The full list of changes for this release can be found in https://github.com/devonfw/IDEasy/milestone/13?closed=1[milestone 2024.09.002]. diff --git a/cli/src/main/java/com/devonfw/tools/ide/cli/CliOfflineException.java b/cli/src/main/java/com/devonfw/tools/ide/cli/CliOfflineException.java index 56be0abd5..9966c27be 100644 --- a/cli/src/main/java/com/devonfw/tools/ide/cli/CliOfflineException.java +++ b/cli/src/main/java/com/devonfw/tools/ide/cli/CliOfflineException.java @@ -1,6 +1,10 @@ package com.devonfw.tools.ide.cli; +import java.net.URL; +import java.nio.file.Path; + import com.devonfw.tools.ide.process.ProcessResult; +import com.devonfw.tools.ide.version.VersionIdentifier; /** * {@link CliException} that is thrown if further processing requires network but the user if offline. @@ -20,9 +24,51 @@ public CliOfflineException() { * * @param message the {@link #getMessage() message}. */ - public CliOfflineException(String message) { - + private CliOfflineException(String message) { super(message, ProcessResult.OFFLINE); } + /** + * Factory method, which is called, when trying to download via a URL + * + * @param url the url, which the software should be downloaded from. + * @return A {@link CliOfflineException} with an informative message. + */ + public static CliOfflineException ofDownloadViaUrl(String url) { + return new CliOfflineException("You are offline and cannot download from URL " + url); + } + + /** + * Factory method, which is called, when trying to download via tool name, edition and version + * + * @param tool the name of the tool, we want to download. + * @param edition the edition of the tool, we want to download. + * @param version the {@link VersionIdentifier} of the tool, we want to download. + * @return A {@link CliOfflineException} with an informative message. + */ + public static CliOfflineException ofDownloadOfTool(String tool, String edition, VersionIdentifier version) { + return new CliOfflineException("Not able to download " + tool + " " + edition + " " + version + " because we are offline"); + } + + /** + * Factory method, which is called, when just a purpose is given. + * + * @param purpose the purpose, which the internet connection serves. + * @return A {@link CliOfflineException} with an informative message. + */ + public static CliOfflineException ofPurpose(String purpose) { + return new CliOfflineException("You are offline but Internet access is required for " + purpose); + } + + /** + * Factory method, which is called, when a clone is performed in offline mode + * + * @param url the url, in which the clone should be executed. + * @param repository the path, where the repository should be cloned to. + * @return A {@link CliOfflineException} with an informative message. + */ + public static CliOfflineException ofClone(URL url, Path repository) { + return new CliOfflineException("Could not clone " + url + " to " + repository + " because you are offline."); + } + } diff --git a/cli/src/main/java/com/devonfw/tools/ide/context/GitContextImpl.java b/cli/src/main/java/com/devonfw/tools/ide/context/GitContextImpl.java index f485f9600..5cc27bcb1 100644 --- a/cli/src/main/java/com/devonfw/tools/ide/context/GitContextImpl.java +++ b/cli/src/main/java/com/devonfw/tools/ide/context/GitContextImpl.java @@ -162,7 +162,7 @@ public void clone(GitUrl gitRepoUrl, Path targetRepository) { } } } else { - throw new CliOfflineException("Could not clone " + parsedUrl + " to " + targetRepository + " because you are offline."); + throw CliOfflineException.ofClone(parsedUrl, targetRepository); } } diff --git a/cli/src/main/java/com/devonfw/tools/ide/context/IdeContext.java b/cli/src/main/java/com/devonfw/tools/ide/context/IdeContext.java index f0410fa3a..b0c527c4e 100644 --- a/cli/src/main/java/com/devonfw/tools/ide/context/IdeContext.java +++ b/cli/src/main/java/com/devonfw/tools/ide/context/IdeContext.java @@ -210,7 +210,7 @@ default void askToContinue(String question) { default void requireOnline(String purpose) { if (isOfflineMode()) { - throw new CliOfflineException("You are offline but Internet access is required for " + purpose); + throw CliOfflineException.ofPurpose(purpose); } } diff --git a/cli/src/main/java/com/devonfw/tools/ide/io/FileAccessImpl.java b/cli/src/main/java/com/devonfw/tools/ide/io/FileAccessImpl.java index c7f910071..9860df712 100644 --- a/cli/src/main/java/com/devonfw/tools/ide/io/FileAccessImpl.java +++ b/cli/src/main/java/com/devonfw/tools/ide/io/FileAccessImpl.java @@ -97,8 +97,8 @@ public void download(String url, Path target) { this.context.info("Trying to download {} from {}", target.getFileName(), url); mkdirs(target.getParent()); try { - if (context.isOffline()) { - throw new CliOfflineException("Not able to download because we are offline"); + if (this.context.isOffline()) { + throw CliOfflineException.ofDownloadViaUrl(url); } if (url.startsWith("http")) { diff --git a/cli/src/main/java/com/devonfw/tools/ide/repo/AbstractToolRepository.java b/cli/src/main/java/com/devonfw/tools/ide/repo/AbstractToolRepository.java index 2479c4732..95955aa29 100644 --- a/cli/src/main/java/com/devonfw/tools/ide/repo/AbstractToolRepository.java +++ b/cli/src/main/java/com/devonfw/tools/ide/repo/AbstractToolRepository.java @@ -51,7 +51,7 @@ public Path download(String tool, String edition, VersionIdentifier version) { VersionIdentifier resolvedVersion = metadata.getVersion(); Set urlCollection = metadata.getUrls(); if (context.isOffline()) { - throw new CliOfflineException("Not able to download " + tool + " because we are offline"); + throw CliOfflineException.ofDownloadOfTool(tool, edition, version); } if (urlCollection.isEmpty()) { throw new IllegalStateException("Invalid download metadata with empty urls file for " + metadata);