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

IntelliJ/Java: Duplicate code -- Refactored as CLIReportUnexpectedExceptionHelper #4076

Closed
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions core/src/main/java/hudson/cli/CLICommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
import hudson.Extension;
import hudson.ExtensionList;
import hudson.ExtensionPoint;
import hudson.Functions;
import hudson.cli.declarative.CLIMethod;
import hudson.ExtensionPoint.LegacyInstancesAreScopedToHudson;
import hudson.Functions;
import hudson.cli.declarative.OptionHandlerExtension;
import jenkins.model.Jenkins;
import hudson.remoting.Channel;
Expand Down Expand Up @@ -293,12 +293,7 @@ public int main(List<String> args, Locale locale, InputStream stdin, PrintStream
stderr.println("ERROR: Bad Credentials. Search the server log for " + id + " for more details.");
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);
Functions.printStackTrace(e, stderr);
reportException(getName(), LOGGER, stderr, e);
return 1;
} finally {
if(sc != null)
Expand Down Expand Up @@ -565,4 +560,13 @@ public static CLICommand getCurrent() {
}
}

@Restricted(NoExternalUse.class)
static public void reportException(String name, Logger logger, PrintStream stderr, Throwable e) {
final String errorMsg = String.format("Unexpected exception occurred while performing %s command.",
name);
stderr.println();
stderr.println("ERROR: " + errorMsg);
logger.log(Level.WARNING, errorMsg, e);
Functions.printStackTrace(e, stderr);
}
}
14 changes: 4 additions & 10 deletions core/src/main/java/hudson/cli/declarative/CLIRegisterer.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import hudson.Extension;
import hudson.ExtensionComponent;
import hudson.ExtensionFinder;
import hudson.Functions;
import hudson.Util;
import hudson.cli.CLICommand;
import hudson.cli.CloneableCLICommand;
Expand All @@ -42,8 +41,8 @@
import org.acegisecurity.context.SecurityContextHolder;
import org.jvnet.hudson.annotation_indexer.Index;
import org.jvnet.localizer.ResourceBundleHolder;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;

import javax.annotation.Nonnull;
import java.io.IOException;
Expand All @@ -59,12 +58,12 @@
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.Stack;
import static java.util.logging.Level.SEVERE;

import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;

import static java.util.logging.Level.SEVERE;

/**
* Discover {@link CLIMethod}s and register them as {@link CLICommand} implementations.
*
Expand Down Expand Up @@ -259,12 +258,7 @@ public int main(List<String> args, Locale locale, InputStream stdin, PrintStream
stderr.println("ERROR: Bad Credentials. Search the server log for " + id + " for more details.");
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);
Functions.printStackTrace(e, stderr);
CLICommand.reportException(getName(), LOGGER, stderr, e);
return 1;
}
}
Expand Down