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

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

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

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

View File

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

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