Use exceptions with explicit messages

This commit is contained in:
René Pfeuffer
2020-03-25 15:31:20 +01:00
parent af45388c27
commit 7247a81c9d
8 changed files with 104 additions and 20 deletions

View File

@@ -24,8 +24,18 @@
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";
}
}

View File

@@ -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";
}
}

View File

@@ -24,8 +24,15 @@
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";
}
}

View File

@@ -24,13 +24,18 @@
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<ContextEntry> context, String message) {
super(context, message);
}
public PluginInstallException(String message, Throwable cause) {
super(message, cause);
public PluginInstallException(List<ContextEntry> context, String message, Exception cause) {
super(context, message, cause);
}
}

View File

@@ -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());
}
}
}

View File

@@ -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": {

View File

@@ -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": {

View File

@@ -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));