Skip to content

Commit

Permalink
feat(Error Messages): Improved feedback to users when an error occurs (
Browse files Browse the repository at this point in the history
  • Loading branch information
ndickerson authored Dec 7, 2022
1 parent a179c1b commit 6d914a5
Show file tree
Hide file tree
Showing 68 changed files with 883 additions and 712 deletions.
4 changes: 4 additions & 0 deletions dataloader.properties
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ loginUrl=https://rest.bullhornstaffing.com/rest-services/login
# Columns that are mapped to an empty value will be ignored.
#
# ---------------------------------------------------------------------------------------------------------------------
error_codeColumn=
errorColumn=
error_detailsColumn=
tips_to_resolveColumn=
failure_reasonColumn=
dataloader_actionColumn=
recruiterUserIDColumn=owner.id
Expand Down
54 changes: 26 additions & 28 deletions src/main/java/com/bullhorn/dataloader/CommandLineInterface.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.bullhorn.dataloader;

import java.io.IOException;

import com.bullhorn.dataloader.enums.Command;
import com.bullhorn.dataloader.service.Action;
import com.bullhorn.dataloader.service.ActionFactory;
Expand All @@ -20,40 +22,36 @@ class CommandLineInterface {
*
* @param args The user's command line parameters
*/
public void start(String[] args) {
public void start(String[] args) throws IOException, InterruptedException {
printUtil.log("Args: " + String.join(" ", args));

try {
if (args.length == 0) {
printUtil.printAndLog("ERROR: Missing action");
printUtil.printUsage();
return;
}

// parse command from command line
Command command = null;
for (Command iter : Command.values()) {
if (iter.getMethodName().equalsIgnoreCase(args[0])) {
command = iter;
break;
}
}
if (args.length == 0) {
printUtil.printAndLog("ERROR: Missing action");
printUtil.printUsage();
return;
}

if (command == null) {
printUtil.printAndLog("ERROR: Unrecognized action: " + args[0]);
printUtil.printUsage();
return;
// parse command from command line
Command command = null;
for (Command iter : Command.values()) {
if (iter.getMethodName().equalsIgnoreCase(args[0])) {
command = iter;
break;
}
}

Action action = actionFactory.getAction(command);
if (!action.isValidArguments(args)) {
printUtil.printUsage();
return;
}
if (command == null) {
printUtil.printAndLog("ERROR: Unrecognized action: " + args[0]);
printUtil.printUsage();
return;
}

action.run(args);
} catch (Exception e) {
printUtil.printAndLog(e);
Action action = actionFactory.getAction(command);
if (!action.isValidArguments(args)) {
printUtil.printUsage();
return;
}

action.run(args);
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/bullhorn/dataloader/data/Cell.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/**
* Represents the raw data from an individual cell of data in a spreadsheet.
*
* <p>
* Contains the contents of an individual cell (value) and the column header (name) for this cell.
*/
public class Cell {
Expand Down
15 changes: 9 additions & 6 deletions src/main/java/com/bullhorn/dataloader/data/CsvFileReader.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package com.bullhorn.dataloader.data;

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.commons.io.input.BOMInputStream;

import com.bullhorn.dataloader.enums.ErrorInfo;
import com.bullhorn.dataloader.util.ArrayUtil;
import com.bullhorn.dataloader.util.DataLoaderException;
import com.bullhorn.dataloader.util.PrintUtil;
import com.bullhorn.dataloader.util.PropertyFileUtil;
import com.csvreader.CsvReader;
Expand All @@ -35,7 +38,7 @@ public class CsvFileReader extends CsvReader {
* @param filePath the path to the CSV file
*/
public CsvFileReader(String filePath, PropertyFileUtil propertyFileUtil, PrintUtil printUtil) throws IOException {
super(new BOMInputStream(new FileInputStream(filePath)), ',',
super(new BOMInputStream(Files.newInputStream(Paths.get(filePath))), ',',
propertyFileUtil.getSingleByteEncoding() ? StandardCharsets.ISO_8859_1 : StandardCharsets.UTF_8);
this.filePath = filePath;
this.propertyFileUtil = propertyFileUtil;
Expand All @@ -61,7 +64,7 @@ public boolean readRecord() throws IOException {

/**
* Returns the data for the current row in the format of a row object.
*
* <p>
* First, call csvFileReader.readRecord() to read the next row, and then call this method instead of getValues() to
* return the Row object instead of the raw string array of values.
*
Expand All @@ -70,8 +73,8 @@ public boolean readRecord() throws IOException {
*/
public Row getRow() throws IOException {
if (getHeaderCount() != getValues().length) {
throw new IOException("Row " + rowNumber + ": Header column count " + getHeaderCount()
+ " is not equal to row column count " + getValues().length);
throw new DataLoaderException(ErrorInfo.INVALID_NUMBER_OF_COLUMNS, "Row " + rowNumber + ": Header column count "
+ getHeaderCount() + " does not match row column count " + getValues().length);
}

Row row = new Row(filePath, rowNumber);
Expand Down Expand Up @@ -116,7 +119,7 @@ private void mapHeaders() throws IOException {
private void checkForDuplicateHeaders() {
Set<String> uniqueHeaders = Sets.newHashSet(mappedHeaders);
if (mappedHeaders.size() != uniqueHeaders.size()) {
throw new IllegalStateException("Provided CSV file contains the following duplicate headers:\n"
throw new DataLoaderException(ErrorInfo.DUPLICATE_COLUMNS_PROVIDED, "Provided CSV file contains the following duplicate headers:\n"
+ ArrayUtil.getDuplicates(mappedHeaders).stream().map(s -> "\t" + s + "\n").collect(Collectors.joining()));
}
}
Expand Down
17 changes: 13 additions & 4 deletions src/main/java/com/bullhorn/dataloader/data/CsvFileWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ public class CsvFileWriter {

public static final String RESULTS_DIR = "results/";
private static final String ACTION_COLUMN = "dataloader_action";
private static final String REASON_COLUMN = "failure_reason";
private static final String ERROR_CODE_COLUMN = "error_code";
private static final String ERROR_COLUMN = "error";
private static final String ERROR_DETAILS_COLUMN = "error_details";
private static final String TIPS_TO_RESOLVE_COLUMN = "tips_to_resolve";
private static final String SUCCESS_CSV = "_success.csv";
private static final String FAILURE_CSV = "_failure.csv";
public static String successFilePath;
Expand Down Expand Up @@ -50,7 +53,7 @@ public static String getResultsFilePath(String inputFilePath, Command command, R
* Error/Success CSV files are placed in a results folder in the current working directory. They are named
* based on the original filename used. Given /path/to/MyCandidates.csv, this class will set up log files in
* the current working directory (may not be the /path/to/ directory).
*
* <p>
* Output Files:
* - results/MyCandidates_yyyy-mm-dd_HH.MM.SS_failure.csv
* - results/MyCandidates_yyyy-mm-dd_HH.MM.SS_success.csv
Expand Down Expand Up @@ -97,7 +100,10 @@ public synchronized void writeRow(Row row, Result result) throws IOException {
}
} else {
csvWriter = getOrCreateFailureCsvWriter();
values.add(0, result.getFailureText());
values.add(0, result.getErrorInfo().getTipsToResolve());
values.add(0, result.getErrorDetails());
values.add(0, result.getErrorInfo().getTitle());
values.add(0, result.getErrorInfo().getCode().toString());
if (command.equals(Command.LOAD) || command.equals(Command.LOAD_ATTACHMENTS)) {
values.add(0, result.getBullhornId().toString());
}
Expand Down Expand Up @@ -134,7 +140,10 @@ private CsvWriter getOrCreateFailureCsvWriter() throws IOException {
failureCsv = new CsvWriter(fileWriter, ',');

List<String> headerList = new ArrayList<>(Arrays.asList(headers));
headerList.add(0, REASON_COLUMN);
headerList.add(0, TIPS_TO_RESOLVE_COLUMN);
headerList.add(0, ERROR_DETAILS_COLUMN);
headerList.add(0, ERROR_COLUMN);
headerList.add(0, ERROR_CODE_COLUMN);
if (command.equals(Command.LOAD) || command.equals(Command.LOAD_ATTACHMENTS)) {
headerList.add(0, StringConsts.ID);
}
Expand Down
Loading

0 comments on commit 6d914a5

Please sign in to comment.