未验证 提交 c78b8e34 编写于 作者: H Helen 提交者: GitHub

Refactoring CLICommand.java (#4641)

* extracted duplicated code in method

* removed unnecessary assignments

* added back variable to reduce duplication and changed name of method
Co-authored-by: NHelen Spry <spry@Helens-MacBook-Pro.local>
上级 5de91d0d
......@@ -234,7 +234,7 @@ public abstract class CLICommand implements ExtensionPoint, Cloneable {
// add options from the authenticator
SecurityContext sc = null;
Authentication old = null;
Authentication auth = null;
Authentication auth;
try {
sc = SecurityContextHolder.getContext();
old = sc.getAuthentication();
......@@ -251,51 +251,31 @@ public abstract class CLICommand implements ExtensionPoint, Cloneable {
new Object[] {getName(), args.size(), auth.getName(), res});
return res;
} catch (CmdLineException e) {
LOGGER.log(Level.FINE, String.format("Failed call to CLI command %s, with %d arguments, as user %s.",
getName(), args.size(), auth != null ? auth.getName() : "<unknown>"), e);
stderr.println();
stderr.println("ERROR: " + e.getMessage());
logFailedCommandAndPrintExceptionErrorMessage(args, e);
printUsage(stderr, p);
return 2;
} catch (IllegalStateException e) {
LOGGER.log(Level.FINE, String.format("Failed call to CLI command %s, with %d arguments, as user %s.",
getName(), args.size(), auth != null ? auth.getName() : "<unknown>"), e);
stderr.println();
stderr.println("ERROR: " + e.getMessage());
logFailedCommandAndPrintExceptionErrorMessage(args, e);
return 4;
} catch (IllegalArgumentException e) {
LOGGER.log(Level.FINE, String.format("Failed call to CLI command %s, with %d arguments, as user %s.",
getName(), args.size(), auth != null ? auth.getName() : "<unknown>"), e);
stderr.println();
stderr.println("ERROR: " + e.getMessage());
logFailedCommandAndPrintExceptionErrorMessage(args, e);
return 3;
} catch (AbortException e) {
LOGGER.log(Level.FINE, String.format("Failed call to CLI command %s, with %d arguments, as user %s.",
getName(), args.size(), auth != null ? auth.getName() : "<unknown>"), e);
// signals an error without stack trace
stderr.println();
stderr.println("ERROR: " + e.getMessage());
logFailedCommandAndPrintExceptionErrorMessage(args, e);
return 5;
} catch (AccessDeniedException e) {
LOGGER.log(Level.FINE, String.format("Failed call to CLI command %s, with %d arguments, as user %s.",
getName(), args.size(), auth != null ? auth.getName() : "<unknown>"), e);
stderr.println();
stderr.println("ERROR: " + e.getMessage());
logFailedCommandAndPrintExceptionErrorMessage(args, e);
return 6;
} catch (BadCredentialsException e) {
// to the caller, we can't reveal whether the user didn't exist or the password didn't match.
// do that to the server log instead
String id = UUID.randomUUID().toString();
LOGGER.log(Level.INFO, "CLI login attempt failed: " + id, e);
stderr.println();
stderr.println("ERROR: Bad Credentials. Search the server log for " + id + " for more details.");
logAndPrintError(e, "Bad Credentials. Search the server log for " + id + " for more details.",
"CLI login attempt failed: " + id, Level.INFO);
return 7;
} catch (Throwable e) {
final String errorMsg = String.format("Unexpected exception occurred while performing %s command.",
getName());
stderr.println();
stderr.println("ERROR: " + errorMsg);
LOGGER.log(Level.WARNING, errorMsg, e);
String errorMsg = "Unexpected exception occurred while performing " + getName() + " command.";
logAndPrintError(e, errorMsg, errorMsg, Level.WARNING);
Functions.printStackTrace(e, stderr);
return 1;
} finally {
......@@ -304,6 +284,20 @@ public abstract class CLICommand implements ExtensionPoint, Cloneable {
}
}
private void logFailedCommandAndPrintExceptionErrorMessage(List<String> args, Throwable e) {
Authentication auth = getTransportAuthentication();
String logMessage = String.format("Failed call to CLI command %s, with %d arguments, as user %s.",
getName(), args.size(), auth != null ? auth.getName() : "<unknown>");
logAndPrintError(e, e.getMessage(), logMessage, Level.FINE);
}
private void logAndPrintError(Throwable e, String errorMessage, String logMessage, Level logLevel) {
LOGGER.log(logLevel, logMessage, e);
this.stderr.println();
this.stderr.println("ERROR: " + errorMessage);
}
/**
* Get parser for this command.
*
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册