Skip to content

Commit

Permalink
Merge pull request #1434 from ashitsalesforce/master
Browse files Browse the repository at this point in the history
improve code readability around exit code usage
  • Loading branch information
ashitsalesforce authored Jan 4, 2025
2 parents 4c36bf4 + f35bf81 commit 71b45fa
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
24 changes: 20 additions & 4 deletions src/main/java/com/salesforce/dataloader/process/ProcessRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,9 @@ public static void logErrorAndExitProcess(String message, Throwable throwable, i

public static ProcessRunner runBatchMode(Map<String, String>commandLineOptionsMap, ILoaderProgress progressMonitor) throws UnsupportedOperationException {
ProcessRunner runner = null;
String errorMessage = "";
int exitCode = AppUtil.EXIT_CODE_NO_ERRORS;
Throwable throwable = null;
try {
// create the process
runner = ProcessRunner.getInstance(commandLineOptionsMap);
Expand All @@ -257,15 +260,22 @@ public static ProcessRunner runBatchMode(Map<String, String>commandLineOptionsMa
// run the process
runner.run(progressMonitor);
progressMonitor = runner.getMonitor();

if (progressMonitor != null) {
if (progressMonitor.isCanceled()) {
logErrorAndExitProcess(progressMonitor.getMessage(), null, AppUtil.EXIT_CODE_CLIENT_ERROR);
errorMessage = progressMonitor.getMessage();
throwable = null;
exitCode = AppUtil.EXIT_CODE_CLIENT_ERROR;
} else if (!progressMonitor.isSuccess()) {
logErrorAndExitProcess(progressMonitor.getMessage(), null, AppUtil.EXIT_CODE_SERVER_ERROR);
errorMessage = progressMonitor.getMessage();
throwable = null;
exitCode = AppUtil.EXIT_CODE_SERVER_ERROR;
} else if (AppConfig.getCurrentConfig() != null
&& AppConfig.getCurrentConfig().getBoolean(AppConfig.PROP_PROCESS_EXIT_WITH_ERROR_ON_FAILED_ROWS_BATCH_MODE)
&& progressMonitor.getNumberRowsWithError() > 0) {
DataLoaderRunner.setExitCode(AppUtil.EXIT_CODE_RESULTS_ERROR);
errorMessage = Messages.getFormattedString("Process.operationSuccessWithErrorRows", progressMonitor.getNumberRowsWithError());
throwable = null;
exitCode = AppUtil.EXIT_CODE_RESULTS_ERROR;
}
}
} catch (Throwable t) {
Expand All @@ -277,7 +287,13 @@ public static ProcessRunner runBatchMode(Map<String, String>commandLineOptionsMa
if (t.getClass().equals(ExitException.class)) {
throw (ExitException)t;
}
logErrorAndExitProcess("Unable to run process", t, AppUtil.EXIT_CODE_OPERATION_ERROR);
errorMessage = "Unable to run process";
throwable = t;
exitCode = AppUtil.EXIT_CODE_OPERATION_ERROR;
} finally {
if (exitCode != AppUtil.EXIT_CODE_NO_ERRORS) {
logErrorAndExitProcess(errorMessage, throwable, exitCode);
}
}
return runner;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

public class ExitException extends RuntimeException {
private static final long serialVersionUID = 1L;
private int exitCode = AppUtil.EXIT_CODE_NO_ERRORS;
private int exitCode = AppUtil.EXIT_CODE_OPERATION_ERROR;
public ExitException(Throwable ex, int exitCode) {
super(ex);
this.exitCode = exitCode;
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Process.errorDaoOpen=Unable to open Data Access Object.
Process.creatingMap=Creating Map
Process.aboutPerform=About to perform: {0}
Process.missingRequiredArg=Missing required argument: {0}
Process.operationSuccessWithErrorRows=Operation succeeded with failure to process {0} records.
Action.loading=Loading: {0}
Action.logCanceled=Load was canceled.
Action.exception=Exception occurred during loading
Expand Down

0 comments on commit 71b45fa

Please sign in to comment.