From 7247a81c9d986d4e1159a8baf78a43b2d4839497 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Pfeuffer?= Date: Wed, 25 Mar 2020 15:31:20 +0100 Subject: [PATCH] Use exceptions with explicit messages --- .../PluginChecksumMismatchException.java | 16 ++++++-- .../scm/plugin/PluginCleanupException.java | 40 +++++++++++++++++++ .../scm/plugin/PluginDownloadException.java | 13 ++++-- .../scm/plugin/PluginInstallException.java | 17 +++++--- .../sonia/scm/plugin/PluginInstaller.java | 10 ++--- .../main/resources/locales/de/plugins.json | 12 ++++++ .../main/resources/locales/en/plugins.json | 12 ++++++ .../scm/plugin/DefaultPluginManagerTest.java | 4 +- 8 files changed, 104 insertions(+), 20 deletions(-) create mode 100644 scm-webapp/src/main/java/sonia/scm/plugin/PluginCleanupException.java diff --git a/scm-webapp/src/main/java/sonia/scm/plugin/PluginChecksumMismatchException.java b/scm-webapp/src/main/java/sonia/scm/plugin/PluginChecksumMismatchException.java index 0cdb3c6b2c..af419fa297 100644 --- a/scm-webapp/src/main/java/sonia/scm/plugin/PluginChecksumMismatchException.java +++ b/scm-webapp/src/main/java/sonia/scm/plugin/PluginChecksumMismatchException.java @@ -21,11 +21,21 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ - + package sonia.scm.plugin; +import static sonia.scm.ContextEntry.ContextBuilder.entity; + public class PluginChecksumMismatchException extends PluginInstallException { - public PluginChecksumMismatchException(String message) { - super(message); + public PluginChecksumMismatchException(AvailablePlugin plugin, String calculatedChecksum, String expectedChecksum) { + super( + entity("Plugin", plugin.getDescriptor().getInformation().getName()).build(), + String.format("downloaded plugin checksum %s does not match expected %s", calculatedChecksum, expectedChecksum) + ); + } + + @Override + public String getCode() { + return "6mRuFxaWM1"; } } diff --git a/scm-webapp/src/main/java/sonia/scm/plugin/PluginCleanupException.java b/scm-webapp/src/main/java/sonia/scm/plugin/PluginCleanupException.java new file mode 100644 index 0000000000..82c4d94b66 --- /dev/null +++ b/scm-webapp/src/main/java/sonia/scm/plugin/PluginCleanupException.java @@ -0,0 +1,40 @@ +/* + * MIT License + * + * Copyright (c) 2020-present Cloudogu GmbH and Contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package sonia.scm.plugin; + +import java.nio.file.Path; + +import static sonia.scm.ContextEntry.ContextBuilder.entity; + +public class PluginCleanupException extends PluginInstallException { + public PluginCleanupException(Path file) { + super(entity("File", file.toString()).build(), "failed to cleanup, after broken installation"); + } + + @Override + public String getCode() { + return "8nRuFzjss1"; + } +} diff --git a/scm-webapp/src/main/java/sonia/scm/plugin/PluginDownloadException.java b/scm-webapp/src/main/java/sonia/scm/plugin/PluginDownloadException.java index 75dfc78b40..b67a766ed2 100644 --- a/scm-webapp/src/main/java/sonia/scm/plugin/PluginDownloadException.java +++ b/scm-webapp/src/main/java/sonia/scm/plugin/PluginDownloadException.java @@ -21,11 +21,18 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ - + package sonia.scm.plugin; +import static sonia.scm.ContextEntry.ContextBuilder.entity; + public class PluginDownloadException extends PluginInstallException { - public PluginDownloadException(String message, Throwable cause) { - super(message, cause); + public PluginDownloadException(AvailablePlugin plugin, Exception cause) { + super(entity("Plugin", plugin.getDescriptor().getInformation().getName()).build(), "failed to download plugin", cause); + } + + @Override + public String getCode() { + return "9iRuFz1UB1"; } } diff --git a/scm-webapp/src/main/java/sonia/scm/plugin/PluginInstallException.java b/scm-webapp/src/main/java/sonia/scm/plugin/PluginInstallException.java index e5d1d0d2a2..b70f668b37 100644 --- a/scm-webapp/src/main/java/sonia/scm/plugin/PluginInstallException.java +++ b/scm-webapp/src/main/java/sonia/scm/plugin/PluginInstallException.java @@ -21,16 +21,21 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ - + package sonia.scm.plugin; -public class PluginInstallException extends RuntimeException { +import sonia.scm.ContextEntry; +import sonia.scm.ExceptionWithContext; - public PluginInstallException(String message) { - super(message); +import java.util.List; + +abstract class PluginInstallException extends ExceptionWithContext { + + public PluginInstallException(List context, String message) { + super(context, message); } - public PluginInstallException(String message, Throwable cause) { - super(message, cause); + public PluginInstallException(List context, String message, Exception cause) { + super(context, message, cause); } } diff --git a/scm-webapp/src/main/java/sonia/scm/plugin/PluginInstaller.java b/scm-webapp/src/main/java/sonia/scm/plugin/PluginInstaller.java index 0649d161e1..1ca34c5166 100644 --- a/scm-webapp/src/main/java/sonia/scm/plugin/PluginInstaller.java +++ b/scm-webapp/src/main/java/sonia/scm/plugin/PluginInstaller.java @@ -21,7 +21,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ - + package sonia.scm.plugin; import com.google.common.hash.HashCode; @@ -64,7 +64,7 @@ class PluginInstaller { return new PendingPluginInstallation(plugin.install(), file); } catch (IOException ex) { cleanup(file); - throw new PluginDownloadException("failed to download plugin", ex); + throw new PluginDownloadException(plugin, ex); } } @@ -74,7 +74,7 @@ class PluginInstaller { Files.deleteIfExists(file); } } catch (IOException e) { - throw new PluginInstallException("failed to cleanup, after broken installation"); + throw new PluginCleanupException(file); } } @@ -84,9 +84,7 @@ class PluginInstaller { String calculatedChecksum = hash.toString(); if (!checksum.get().equalsIgnoreCase(calculatedChecksum)) { cleanup(file); - throw new PluginChecksumMismatchException( - String.format("downloaded plugin checksum %s does not match expected %s", calculatedChecksum, checksum.get()) - ); + throw new PluginChecksumMismatchException(plugin, calculatedChecksum, checksum.get()); } } } diff --git a/scm-webapp/src/main/resources/locales/de/plugins.json b/scm-webapp/src/main/resources/locales/de/plugins.json index 809da9d0df..8c5c88e0f8 100644 --- a/scm-webapp/src/main/resources/locales/de/plugins.json +++ b/scm-webapp/src/main/resources/locales/de/plugins.json @@ -203,6 +203,18 @@ "4GRrgkSC01": { "displayName": "Unerwartetes Merge-Ergebnis", "description": "Der Merge hatte ein unerwartetes Ergebis, das nicht automatisiert behandelt werden konnte. Nähere Details sind im Log zu finden. Führen Sie den Merge ggf. manuell durch." + }, + "6mRuFxaWM1": { + "displayName": "Falsche Checksumme", + "description": "Die Checksumme des heruntergeladenen Plugins stimmt nicht mit der erwarteten Checksumme überein. Bitte versuchen Sie es erneut und prüfen Sie die Interneteinstellungen wie z. B. die Proxy-Einstellungen." + }, + "9iRuFz1UB1": { + "displayName": "Fehler beim Herunterladen", + "description": "Das Plugin konnte nicht vom Server heruntergeladen werden. BitteThe plugin could not be loaded from the server. Bitte versuchen Sie es erneut und prüfen Sie die Interneteinstellungen wie z. B. die Proxy-Einstellungen. Weitere Details finden sich im Server Log." + }, + "8nRuFzjss1": { + "displayName": "Fehler beim Löschen falscher Downloads", + "description": "Ein fehlerhaft heruntergeladenes Plugin konnte nicht gelöscht werden. Bitte prüfen Sie die Server Logs und löschen die Datei manuell." } }, "namespaceStrategies": { diff --git a/scm-webapp/src/main/resources/locales/en/plugins.json b/scm-webapp/src/main/resources/locales/en/plugins.json index 69e8d057ee..998e796bac 100644 --- a/scm-webapp/src/main/resources/locales/en/plugins.json +++ b/scm-webapp/src/main/resources/locales/en/plugins.json @@ -203,6 +203,18 @@ "4GRrgkSC01": { "displayName": "Unexpected merge result", "description": "The merge led to an unexpected result, that could not be handled automatically. More details could be found in the log. Please merge the branches manually." + }, + "6mRuFxaWM1": { + "displayName": "Wrong checksum", + "description": "The checksum of the downloaded plugin did not match the expected checksum. Please try again or check the internet settings like proxies." + }, + "9iRuFz1UB1": { + "displayName": "Could not load plugin", + "description": "The plugin could not be loaded from the server. Please try again or check the internet settings like proxies. More information can be found in the server log." + }, + "8nRuFzjss1": { + "displayName": "Error while cleaning up failed plugin", + "description": "A failed plugin download could not be removed correctly. Please check the server log and remove the plugin manually." } }, "namespaceStrategies": { diff --git a/scm-webapp/src/test/java/sonia/scm/plugin/DefaultPluginManagerTest.java b/scm-webapp/src/test/java/sonia/scm/plugin/DefaultPluginManagerTest.java index 47c351ff30..8f3f7bbe57 100644 --- a/scm-webapp/src/test/java/sonia/scm/plugin/DefaultPluginManagerTest.java +++ b/scm-webapp/src/test/java/sonia/scm/plugin/DefaultPluginManagerTest.java @@ -21,7 +21,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ - + package sonia.scm.plugin; import com.google.common.collect.ImmutableList; @@ -252,7 +252,7 @@ class DefaultPluginManagerTest { PendingPluginInstallation pendingMail = mock(PendingPluginInstallation.class); doReturn(pendingMail).when(installer).install(mail); - doThrow(new PluginChecksumMismatchException("checksum does not match")).when(installer).install(review); + doThrow(new PluginChecksumMismatchException(mail, "1", "2")).when(installer).install(review); assertThrows(PluginInstallException.class, () -> manager.install("scm-review-plugin", false));