Skip to content

Commit f9d55be

Browse files
authored
Rename UserError
The top-level class Throwable represents all errors and exceptions in Java. This hierarchy is divided into Error and Exception, the former being serious problems that applications should not try to catch and the latter representing exceptional conditions that an application might want to catch and handle. This commit renames org.elasticsearch.cli.UserError to org.elasticsearch.UserException to make its name consistent with where it falls in this hierarchy. Relates #19254
1 parent 36b887e commit f9d55be

File tree

11 files changed

+50
-51
lines changed

11 files changed

+50
-51
lines changed

core/src/main/java/org/elasticsearch/bootstrap/Elasticsearch.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import org.elasticsearch.cli.ExitCodes;
2626
import org.elasticsearch.cli.SettingCommand;
2727
import org.elasticsearch.cli.Terminal;
28-
import org.elasticsearch.cli.UserError;
28+
import org.elasticsearch.cli.UserException;
2929
import org.elasticsearch.monitor.jvm.JvmInfo;
3030

3131
import java.io.IOException;
@@ -73,11 +73,11 @@ static int main(final String[] args, final Elasticsearch elasticsearch, final Te
7373
@Override
7474
protected void execute(Terminal terminal, OptionSet options, Map<String, String> settings) throws Exception {
7575
if (options.nonOptionArguments().isEmpty() == false) {
76-
throw new UserError(ExitCodes.USAGE, "Positional arguments not allowed, found " + options.nonOptionArguments());
76+
throw new UserException(ExitCodes.USAGE, "Positional arguments not allowed, found " + options.nonOptionArguments());
7777
}
7878
if (options.has(versionOption)) {
7979
if (options.has(daemonizeOption) || options.has(pidfileOption)) {
80-
throw new UserError(ExitCodes.USAGE, "Elasticsearch version option is mutually exclusive with any other option");
80+
throw new UserException(ExitCodes.USAGE, "Elasticsearch version option is mutually exclusive with any other option");
8181
}
8282
terminal.println("Version: " + org.elasticsearch.Version.CURRENT
8383
+ ", Build: " + Build.CURRENT.shortHash() + "/" + Build.CURRENT.date()

core/src/main/java/org/elasticsearch/cli/Command.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public final int main(String[] args, Terminal terminal) throws Exception {
5555
printHelp(terminal);
5656
terminal.println(Terminal.Verbosity.SILENT, "ERROR: " + e.getMessage());
5757
return ExitCodes.USAGE;
58-
} catch (UserError e) {
58+
} catch (UserException e) {
5959
if (e.exitCode == ExitCodes.USAGE) {
6060
printHelp(terminal);
6161
}
@@ -79,7 +79,7 @@ void mainWithoutErrorHandling(String[] args, Terminal terminal) throws Exception
7979
if (options.has(silentOption)) {
8080
if (options.has(verboseOption)) {
8181
// mutually exclusive, we can remove this with jopt-simple 5.0, which natively supports it
82-
throw new UserError(ExitCodes.USAGE, "Cannot specify -s and -v together");
82+
throw new UserException(ExitCodes.USAGE, "Cannot specify -s and -v together");
8383
}
8484
terminal.setVerbosity(Terminal.Verbosity.SILENT);
8585
} else if (options.has(verboseOption)) {
@@ -110,7 +110,7 @@ protected static void exit(int status) {
110110
/**
111111
* Executes this command.
112112
*
113-
* Any runtime user errors (like an input file that does not exist), should throw a {@link UserError}. */
113+
* Any runtime user errors (like an input file that does not exist), should throw a {@link UserException}. */
114114
protected abstract void execute(Terminal terminal, OptionSet options) throws Exception;
115115

116116
}

core/src/main/java/org/elasticsearch/cli/MultiCommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ protected void execute(Terminal terminal, OptionSet options) throws Exception {
6060
}
6161
String[] args = arguments.values(options).toArray(new String[0]);
6262
if (args.length == 0) {
63-
throw new UserError(ExitCodes.USAGE, "Missing command");
63+
throw new UserException(ExitCodes.USAGE, "Missing command");
6464
}
6565
Command subcommand = subcommands.get(args[0]);
6666
if (subcommand == null) {
67-
throw new UserError(ExitCodes.USAGE, "Unknown command [" + args[0] + "]");
67+
throw new UserException(ExitCodes.USAGE, "Unknown command [" + args[0] + "]");
6868
}
6969
subcommand.mainWithoutErrorHandling(Arrays.copyOfRange(args, 1, args.length), terminal);
7070
}

core/src/main/java/org/elasticsearch/cli/SettingCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ protected void execute(Terminal terminal, OptionSet options) throws Exception {
4141
final Map<String, String> settings = new HashMap<>();
4242
for (final KeyValuePair kvp : settingOption.values(options)) {
4343
if (kvp.value.isEmpty()) {
44-
throw new UserError(ExitCodes.USAGE, "Setting [" + kvp.key + "] must not be empty");
44+
throw new UserException(ExitCodes.USAGE, "Setting [" + kvp.key + "] must not be empty");
4545
}
4646
settings.put(kvp.key, kvp.value);
4747
}

core/src/main/java/org/elasticsearch/cli/UserError.java renamed to core/src/main/java/org/elasticsearch/cli/UserException.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@
2222
/**
2323
* An exception representing a user fixable problem in {@link Command} usage.
2424
*/
25-
public class UserError extends Exception {
25+
public class UserException extends Exception {
2626

2727
/** The exist status the cli should use when catching this user error. */
2828
public final int exitCode;
2929

30-
/** Constructs a UserError with an exit status and message to show the user. */
31-
public UserError(int exitCode, String msg) {
30+
/** Constructs a UserException with an exit status and message to show the user. */
31+
public UserException(int exitCode, String msg) {
3232
super(msg);
3333
this.exitCode = exitCode;
3434
}

core/src/main/java/org/elasticsearch/plugins/InstallPluginCommand.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import org.elasticsearch.cli.ExitCodes;
3030
import org.elasticsearch.cli.SettingCommand;
3131
import org.elasticsearch.cli.Terminal;
32-
import org.elasticsearch.cli.UserError;
32+
import org.elasticsearch.cli.UserException;
3333
import org.elasticsearch.common.collect.Tuple;
3434
import org.elasticsearch.common.hash.MessageDigests;
3535
import org.elasticsearch.common.io.FileSystemUtils;
@@ -188,7 +188,7 @@ protected void execute(Terminal terminal, OptionSet options, Map<String, String>
188188
// TODO: in jopt-simple 5.0 we can enforce a min/max number of positional args
189189
List<String> args = arguments.values(options);
190190
if (args.size() != 1) {
191-
throw new UserError(ExitCodes.USAGE, "Must supply a single plugin id argument");
191+
throw new UserException(ExitCodes.USAGE, "Must supply a single plugin id argument");
192192
}
193193
String pluginId = args.get(0);
194194
boolean isBatch = options.has(batchOption) || System.console() == null;
@@ -250,7 +250,7 @@ private Path download(Terminal terminal, String pluginId, Path tmpDir) throws Ex
250250
if (plugins.isEmpty() == false) {
251251
msg += ", did you mean " + (plugins.size() == 1 ? "[" + plugins.get(0) + "]": "any of " + plugins.toString()) + "?";
252252
}
253-
throw new UserError(ExitCodes.USAGE, msg);
253+
throw new UserException(ExitCodes.USAGE, msg);
254254
}
255255
terminal.println("-> Downloading " + URLDecoder.decode(pluginId, "UTF-8"));
256256
return downloadZip(terminal, pluginId, tmpDir);
@@ -328,20 +328,20 @@ private Path downloadZipAndChecksum(Terminal terminal, String urlString, Path tm
328328
BufferedReader checksumReader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
329329
expectedChecksum = checksumReader.readLine();
330330
if (checksumReader.readLine() != null) {
331-
throw new UserError(ExitCodes.IO_ERROR, "Invalid checksum file at " + checksumUrl);
331+
throw new UserException(ExitCodes.IO_ERROR, "Invalid checksum file at " + checksumUrl);
332332
}
333333
}
334334

335335
byte[] zipbytes = Files.readAllBytes(zip);
336336
String gotChecksum = MessageDigests.toHexString(MessageDigests.sha1().digest(zipbytes));
337337
if (expectedChecksum.equals(gotChecksum) == false) {
338-
throw new UserError(ExitCodes.IO_ERROR, "SHA1 mismatch, expected " + expectedChecksum + " but got " + gotChecksum);
338+
throw new UserException(ExitCodes.IO_ERROR, "SHA1 mismatch, expected " + expectedChecksum + " but got " + gotChecksum);
339339
}
340340

341341
return zip;
342342
}
343343

344-
private Path unzip(Path zip, Path pluginsDir) throws IOException, UserError {
344+
private Path unzip(Path zip, Path pluginsDir) throws IOException, UserException {
345345
// unzip plugin to a staging temp dir
346346

347347
final Path target = stagingDirectory(pluginsDir);
@@ -386,7 +386,7 @@ private Path unzip(Path zip, Path pluginsDir) throws IOException, UserError {
386386
Files.delete(zip);
387387
if (hasEsDir == false) {
388388
IOUtils.rm(target);
389-
throw new UserError(ExitCodes.DATA_ERROR, "`elasticsearch` directory is missing in the plugin zip");
389+
throw new UserException(ExitCodes.DATA_ERROR, "`elasticsearch` directory is missing in the plugin zip");
390390
}
391391
return target;
392392
}
@@ -425,7 +425,7 @@ private PluginInfo verify(Terminal terminal, Path pluginRoot, boolean isBatch, E
425425
// don't let luser install plugin as a module...
426426
// they might be unavoidably in maven central and are packaged up the same way)
427427
if (MODULES.contains(info.getName())) {
428-
throw new UserError(ExitCodes.USAGE, "plugin '" + info.getName() + "' cannot be installed like this, it is a system module");
428+
throw new UserException(ExitCodes.USAGE, "plugin '" + info.getName() + "' cannot be installed like this, it is a system module");
429429
}
430430

431431
// check for jar hell before any copying
@@ -475,7 +475,7 @@ private void install(Terminal terminal, boolean isBatch, Path tmpRoot, Environme
475475

476476
final Path destination = env.pluginsFile().resolve(info.getName());
477477
if (Files.exists(destination)) {
478-
throw new UserError(
478+
throw new UserException(
479479
ExitCodes.USAGE,
480480
"plugin directory " + destination.toAbsolutePath() +
481481
" already exists. To update the plugin, uninstall it first using 'remove " + info.getName() + "' command");
@@ -520,15 +520,15 @@ private void install(Terminal terminal, boolean isBatch, Path tmpRoot, Environme
520520
/** Copies the files from {@code tmpBinDir} into {@code destBinDir}, along with permissions from dest dirs parent. */
521521
private void installBin(PluginInfo info, Path tmpBinDir, Path destBinDir) throws Exception {
522522
if (Files.isDirectory(tmpBinDir) == false) {
523-
throw new UserError(ExitCodes.IO_ERROR, "bin in plugin " + info.getName() + " is not a directory");
523+
throw new UserException(ExitCodes.IO_ERROR, "bin in plugin " + info.getName() + " is not a directory");
524524
}
525525
Files.createDirectory(destBinDir);
526526
setFileAttributes(destBinDir, DIR_AND_EXECUTABLE_PERMS);
527527

528528
try (DirectoryStream<Path> stream = Files.newDirectoryStream(tmpBinDir)) {
529529
for (Path srcFile : stream) {
530530
if (Files.isDirectory(srcFile)) {
531-
throw new UserError(
531+
throw new UserException(
532532
ExitCodes.DATA_ERROR,
533533
"Directories not allowed in bin dir for plugin " + info.getName() + ", found " + srcFile.getFileName());
534534
}
@@ -547,7 +547,7 @@ private void installBin(PluginInfo info, Path tmpBinDir, Path destBinDir) throws
547547
*/
548548
private void installConfig(PluginInfo info, Path tmpConfigDir, Path destConfigDir) throws Exception {
549549
if (Files.isDirectory(tmpConfigDir) == false) {
550-
throw new UserError(ExitCodes.IO_ERROR, "config in plugin " + info.getName() + " is not a directory");
550+
throw new UserException(ExitCodes.IO_ERROR, "config in plugin " + info.getName() + " is not a directory");
551551
}
552552

553553
Files.createDirectories(destConfigDir);
@@ -563,7 +563,7 @@ private void installConfig(PluginInfo info, Path tmpConfigDir, Path destConfigDi
563563
try (DirectoryStream<Path> stream = Files.newDirectoryStream(tmpConfigDir)) {
564564
for (Path srcFile : stream) {
565565
if (Files.isDirectory(srcFile)) {
566-
throw new UserError(ExitCodes.DATA_ERROR, "Directories not allowed in config dir for plugin " + info.getName());
566+
throw new UserException(ExitCodes.DATA_ERROR, "Directories not allowed in config dir for plugin " + info.getName());
567567
}
568568

569569
Path destFile = destConfigDir.resolve(tmpConfigDir.relativize(srcFile));

core/src/main/java/org/elasticsearch/plugins/RemovePluginCommand.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,9 @@
2929
import joptsimple.OptionSet;
3030
import joptsimple.OptionSpec;
3131
import org.apache.lucene.util.IOUtils;
32-
import org.elasticsearch.cli.Command;
3332
import org.elasticsearch.cli.ExitCodes;
3433
import org.elasticsearch.cli.SettingCommand;
35-
import org.elasticsearch.cli.UserError;
34+
import org.elasticsearch.cli.UserException;
3635
import org.elasticsearch.common.Strings;
3736
import org.elasticsearch.cli.Terminal;
3837
import org.elasticsearch.common.settings.Settings;
@@ -58,7 +57,7 @@ protected void execute(Terminal terminal, OptionSet options, Map<String, String>
5857
// TODO: in jopt-simple 5.0 we can enforce a min/max number of positional args
5958
List<String> args = arguments.values(options);
6059
if (args.size() != 1) {
61-
throw new UserError(ExitCodes.USAGE, "Must supply a single plugin id argument");
60+
throw new UserException(ExitCodes.USAGE, "Must supply a single plugin id argument");
6261
}
6362
execute(terminal, args.get(0), settings);
6463
}
@@ -71,15 +70,15 @@ void execute(Terminal terminal, String pluginName, Map<String, String> settings)
7170

7271
Path pluginDir = env.pluginsFile().resolve(pluginName);
7372
if (Files.exists(pluginDir) == false) {
74-
throw new UserError(ExitCodes.USAGE, "plugin " + pluginName + " not found; run 'elasticsearch-plugin list' to get list of installed plugins");
73+
throw new UserException(ExitCodes.USAGE, "plugin " + pluginName + " not found; run 'elasticsearch-plugin list' to get list of installed plugins");
7574
}
7675

7776
List<Path> pluginPaths = new ArrayList<>();
7877

7978
Path pluginBinDir = env.binFile().resolve(pluginName);
8079
if (Files.exists(pluginBinDir)) {
8180
if (Files.isDirectory(pluginBinDir) == false) {
82-
throw new UserError(ExitCodes.IO_ERROR, "Bin dir for " + pluginName + " is not a directory");
81+
throw new UserException(ExitCodes.IO_ERROR, "Bin dir for " + pluginName + " is not a directory");
8382
}
8483
pluginPaths.add(pluginBinDir);
8584
terminal.println(VERBOSE, "Removing: " + pluginBinDir);

core/src/test/java/org/elasticsearch/cli/CommandTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ static class UserErrorCommand extends Command {
3030
}
3131
@Override
3232
protected void execute(Terminal terminal, OptionSet options) throws Exception {
33-
throw new UserError(ExitCodes.DATA_ERROR, "Bad input");
33+
throw new UserException(ExitCodes.DATA_ERROR, "Bad input");
3434
}
3535
}
3636

@@ -40,7 +40,7 @@ static class UsageErrorCommand extends Command {
4040
}
4141
@Override
4242
protected void execute(Terminal terminal, OptionSet options) throws Exception {
43-
throw new UserError(ExitCodes.USAGE, "something was no good");
43+
throw new UserException(ExitCodes.USAGE, "something was no good");
4444
}
4545
}
4646

@@ -87,7 +87,7 @@ public void testVerbositySilentAndVerbose() throws Exception {
8787
MockTerminal terminal = new MockTerminal();
8888
NoopCommand command = new NoopCommand();
8989
String[] args = {"-v", "-s"};
90-
UserError e = expectThrows(UserError.class, () -> {
90+
UserException e = expectThrows(UserException.class, () -> {
9191
command.mainWithoutErrorHandling(args, terminal);
9292
});
9393
assertTrue(e.getMessage(), e.getMessage().contains("Cannot specify -s and -v together"));

core/src/test/java/org/elasticsearch/cli/MultiCommandTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public void testNoCommandsConfigured() throws Exception {
6161

6262
public void testUnknownCommand() throws Exception {
6363
multiCommand.subcommands.put("something", new DummySubCommand());
64-
UserError e = expectThrows(UserError.class, () -> {
64+
UserException e = expectThrows(UserException.class, () -> {
6565
execute("somethingelse");
6666
});
6767
assertEquals(ExitCodes.USAGE, e.exitCode);
@@ -70,7 +70,7 @@ public void testUnknownCommand() throws Exception {
7070

7171
public void testMissingCommand() throws Exception {
7272
multiCommand.subcommands.put("command1", new DummySubCommand());
73-
UserError e = expectThrows(UserError.class, () -> {
73+
UserException e = expectThrows(UserException.class, () -> {
7474
execute();
7575
});
7676
assertEquals(ExitCodes.USAGE, e.exitCode);

0 commit comments

Comments
 (0)