Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

errorprone :: ThrowSpecificExceptions #780

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/main/java/emissary/admin/Startup.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import emissary.config.Configurator;
import emissary.config.ServiceConfigGuide;
import emissary.core.EmissaryException;
import emissary.core.EmissaryRuntimeException;
import emissary.core.Namespace;
import emissary.directory.DirectoryEntry;
import emissary.directory.DirectoryPlace;
Expand Down Expand Up @@ -538,7 +539,7 @@ public static boolean verifyNoInvisiblePlacesStarted() {
// start all other places, so it isn't per se "announced", but it is known and logged
activeDirPlaces.removeIf(dir -> dir.equalsIgnoreCase("DirectoryPlace"));
} catch (EmissaryException e) {
throw new RuntimeException(e);
throw new EmissaryRuntimeException(e);
}

// compares place names in active dirs and active places, removes them from set if found
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/emissary/command/ServerCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import emissary.command.converter.ProjectBaseConverter;
import emissary.command.validator.ServerModeValidator;
import emissary.core.EmissaryException;
import emissary.core.EmissaryRuntimeException;
import emissary.server.EmissaryServer;
import emissary.server.api.Pause;

Expand Down Expand Up @@ -90,7 +91,7 @@ public void setupCommand() {
setupServer();
} catch (EmissaryException e) {
LOG.error("Got an exception", e);
throw new RuntimeException(e);
throw new EmissaryRuntimeException(e);
}
}

Expand All @@ -113,7 +114,7 @@ public void setupServer() throws EmissaryException {
}

if (flavorSet.contains("STANDALONE") && flavorSet.contains("CLUSTER")) {
throw new RuntimeException("Can not run a server in both STANDALONE and CLUSTER");
throw new IllegalArgumentException("Can not run a server in both STANDALONE and CLUSTER");
} else {
overrideFlavor(String.join(",", flavorSet));
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/emissary/command/ServiceCommand.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package emissary.command;

import emissary.client.EmissaryResponse;
import emissary.core.EmissaryRuntimeException;
import emissary.directory.EmissaryNode;

import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -109,7 +110,7 @@ public void run(CommandLine c) {
} else if (isUnpause()) {
unpauseService();
} else {
throw new RuntimeException("Emissary " + getServiceName() + " is already running");
throw new EmissaryRuntimeException("Emissary " + getServiceName() + " is already running");
}
} else {
// no running server so fire it up
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/emissary/core/BaseDataObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -913,7 +913,7 @@ public void putParameters(final Map<? extends String, ? extends Object> m, final
this.parameters.put(name, v);
}
} else {
throw new RuntimeException("Unhandled parameter merge policy " + policy + " for " + name);
throw new IllegalStateException("Unhandled parameter merge policy " + policy + " for " + name);
}
}
} else {
Expand All @@ -924,7 +924,7 @@ public void putParameters(final Map<? extends String, ? extends Object> m, final
this.parameters.put(name, value);
}
} else {
throw new RuntimeException("Unhandled parameter merge policy " + policy + " for " + name);
throw new IllegalStateException("Unhandled parameter merge policy " + policy + " for " + name);
}
}
}
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/emissary/core/EmissaryRuntimeException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package emissary.core;

/**
* This is the top of the exception hierarchy for Emissary All emissary exceptions extend from here
drivenflywheel marked this conversation as resolved.
Show resolved Hide resolved
*/
public class EmissaryRuntimeException extends RuntimeException {

// Serializable
private static final long serialVersionUID = -1590114207784953305L;

/**
* Create an emissary exception
*
* @param message a string to go along with the exception
*/
public EmissaryRuntimeException(final String message) {
super(message);
}

/**
* Create an emissary exception
*
* @param message a string to go along with the exception
* @param cause the wrapped exception
*/
public EmissaryRuntimeException(final String message, final Throwable cause) {
super(message, cause);
}

/**
* Create an emissary exception
*
* @param cause the wrapped exception
*/
public EmissaryRuntimeException(final Throwable cause) {
super("Exception: " + cause.getMessage(), cause);
}
}
12 changes: 6 additions & 6 deletions src/main/java/emissary/core/Factory.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,25 +63,25 @@ public static Object create(final String className, final Object... args) {
final Constructor<?> constructor = ConstructorLookupCache.lookup(clazz, types.toArray(new Class<?>[0]));
if (constructor == null) {
logger.info("Failed to find constructor for args({}) types ({}) : {}", args.length, types.size(), types);
throw new Error("failed to find suitable constructor for class " + className);
throw new AssertionError("failed to find suitable constructor for class " + className);
} else {
return constructor.newInstance(args);
}
} catch (ClassNotFoundException e1) {
logger.error("Could not find class", e1);
throw new Error(e1);
throw new AssertionError(e1);
} catch (InstantiationException e3) {
logger.error("Could not instantiate", e3);
throw new Error(e3);
throw new AssertionError(e3);
} catch (IllegalAccessException e4) {
logger.error("Could not call constructor", e4);
throw new Error(e4);
throw new AssertionError(e4);
} catch (InvocationTargetException e5) {
logger.error("Constructor failed", e5);
throw new Error(e5);
throw new AssertionError(e5);
} catch (Throwable t) {
logger.error("Problem in factory", t);
throw new Error(t);
throw new AssertionError(t);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void write(final byte[] bs, final int off, final int len) throws IOExcept
this.directBuff.flip();
while (this.directBuff.hasRemaining()) {
if (this.fc.write(this.directBuff) <= 0) {
throw new RuntimeException("no bytes written");
throw new IllegalStateException("no bytes written");
}
}
offset += limit;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/emissary/place/ServiceProviderPlace.java
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ public void process(IBaseDataObject payload) throws ResourceException {
logger.error("Sprouting is no longer supported, lost {} children", children.size());
}
} else {
throw new RuntimeException("Neither process nor processHeavyDuty appears to be implemented");
throw new IllegalStateException("Neither process nor processHeavyDuty appears to be implemented");
}
}

Expand All @@ -642,7 +642,7 @@ public List<IBaseDataObject> processHeavyDuty(IBaseDataObject payload) throws Re
process(payload);
return Collections.emptyList();
} else {
throw new RuntimeException("Neither process nor processHeavyDuty appears to be implemented");
throw new IllegalStateException("Neither process nor processHeavyDuty appears to be implemented");
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/emissary/server/EmissaryServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import emissary.config.ConfigUtil;
import emissary.config.Configurator;
import emissary.core.EmissaryException;
import emissary.core.EmissaryRuntimeException;
import emissary.core.IPausable;
import emissary.core.MetricsManager;
import emissary.core.Namespace;
Expand Down Expand Up @@ -207,7 +208,7 @@ public Server startServer() {
return configuredServer;
} catch (Throwable t) {
String errorMsg = "Emissary server didn't start";
throw new RuntimeException(errorMsg, t);
throw new EmissaryRuntimeException(errorMsg, t);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/emissary/util/UnixFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public UnixFile(File magicFile) throws IOException {
*/
public UnixFile(File magicFile, boolean swallowParseException) throws IOException {
if (!magicFile.exists()) {
throw new RuntimeException("Magic file not found at: " + magicFile.getAbsolutePath());
throw new IllegalArgumentException("Magic file not found at: " + magicFile.getAbsolutePath());
}

this.magicFiles.add(magicFile);
Expand All @@ -76,7 +76,7 @@ public UnixFile(List<String> magicPaths, boolean swallowParseException) throws I
for (String mPath : magicPaths) {
File mFile = new File(mPath);
if (!mFile.exists() || !mFile.canRead()) {
throw new RuntimeException("Magic file not found at " + mFile.getAbsolutePath());
throw new IllegalArgumentException("Magic file not found at " + mFile.getAbsolutePath());
}
this.magicFiles.add(mFile);
util.load(mFile, swallowParseException);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/emissary/util/magic/MagicNumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ private String format(String desc, byte[] data) {
}
}
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
throw new IllegalArgumentException(e);
}
if (subType.charValue() == 'l' && !stack.empty() && stack.peek().charValue() == 'd') {
stack.pop();
Expand Down Expand Up @@ -355,7 +355,7 @@ private boolean testNumeric(byte[] data) {
}
return true;
default:
throw new RuntimeException(
throw new IllegalStateException(
"This MagicNumber instance is configured incorrectly. The unary operator is set to an unknown or unconfigured value.");

}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/emissary/util/magic/MagicNumberFactory.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package emissary.util.magic;

import emissary.core.EmissaryRuntimeException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -153,7 +155,7 @@ public static List<MagicNumber> buildMagicNumberList(byte[] configData, @Nullabl
} catch (IOException ioe) {
log.error("Caught IOException on buildMagicNumberList (throwing a runtime exception): {}", ioe.getMessage(), ioe);
/** Doing all of this in memory - yes, one could erroneously use one of the IO objects but ... */
throw new RuntimeException(ioe);
throw new EmissaryRuntimeException(ioe);
}
return magicNumberList;
}
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/emissary/EmissaryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import emissary.command.BaseCommand;
import emissary.command.EmissaryCommand;
import emissary.core.EmissaryRuntimeException;
import emissary.test.core.junit5.UnitTest;

import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -214,7 +215,7 @@ public void setupCommand() {
@Override
public void run(CommandLine c) {
setup();
throw new RuntimeException("Still broken here");
throw new EmissaryRuntimeException("Still broken here");
}

@Override
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/emissary/config/ConfigUtilTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package emissary.config;

import emissary.core.EmissaryException;
import emissary.core.EmissaryRuntimeException;
import emissary.test.core.junit5.UnitTest;
import emissary.util.shell.Executrix;

Expand Down Expand Up @@ -655,7 +656,7 @@ private Path createFileAndPopulate(final Path dir, final String name, final Stri
ros.write(contents.getBytes());
} catch (IOException ex) {
logger.error("Problem making {}", file, ex);
throw new RuntimeException(ex);
throw new EmissaryRuntimeException(ex);
}
return file;
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/emissary/core/FactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public String getKey() {

static class DemoClassThatThrowsThrowableFromConstructor {
public DemoClassThatThrowsThrowableFromConstructor() throws Throwable {
throw new Throwable("Bogus");
throw new AssertionError("Bogus");
}
}
}
3 changes: 2 additions & 1 deletion src/test/java/emissary/roll/RollManagerTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package emissary.roll;

import emissary.config.ConfigUtil;
import emissary.core.EmissaryRuntimeException;
import emissary.test.core.junit5.UnitTest;
import emissary.util.EmissaryIsolatedClassLoaderExtension;

Expand Down Expand Up @@ -65,7 +66,7 @@ void testFailedRoller() throws Exception {
public void roll() {
if (i++ == 2) {
latch.countDown();
throw new RuntimeException("Not supported yet.");
throw new EmissaryRuntimeException("Not supported yet.");
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/test/java/emissary/util/DisposeHelperTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package emissary.util;

import emissary.core.BaseDataObject;
import emissary.core.EmissaryRuntimeException;
import emissary.core.IBaseDataObject;
import emissary.test.core.junit5.UnitTest;

Expand Down Expand Up @@ -29,7 +30,7 @@ class DisposeHelperTest extends UnitTest {
private static final Runnable THIRD = () -> LoggerFactory.getLogger("DisposeHelperRunnable").warn("DisposeHelperTestThirdRunnable");

private static final Runnable THROWS = () -> {
throw new RuntimeException("DisposeHelperTest");
throw new EmissaryRuntimeException("DisposeHelperTest");
};

@Nullable
Expand Down