Home » Java » java – Display better error messages for the user-Exceptionshub

java – Display better error messages for the user-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

     private boolean relatedCommand(String input) {
            // make sure the split afterwards has at least size one
            if (input.matches(" .*")) {
                return false;
            }
            final String command = input.split(" ".toString())[0];
            return COMMAND_PACKAGE
                    .keySet()
                    .stream()
                    .map(Pattern::toString)
                    .anyMatch(patternText -> patternText.startsWith(command + " "));
      }


    public Command getCommand(final String input) throws InvalidInputException {
            if (relatedCommand(input)) {
                Terminal.printError("test");
                throw new InvalidInputException("invalid arguments");
            } else {
                throw new InvalidInputException("unknown command");
            }
            ...
     }

I am having trouble with giving the user a more specific error message. For example I have the commands add track <argument1> and add switch <argument1>. If the user just types in “add” he shouldn’t get the error message “invalid arguments”. Instead it should be “invalid command: either use add track or add switch). Since relatedCommand() is a boolean. How do I implement this efficiently?

How to&Answers:

As you mentioned, currently the method returns a boolean, which is a two-state outcome. At the same time you are seeking for three-state outcome:

  1. command is known
  2. command is unknown
  3. there are several commands known for the prefix.

For the question given in regards to the way of returning the proper result, one option would be to return the collection of possible commands, which can represent the required three states:

  1. the collection consists of one element
  2. the collection is empty
  3. the collection consists of more than one element.

The changes to your original code can be like these:

private Collection<String> relatedCommand(String input) {
        // make sure the split afterwards has at least size one
        if (input.matches(" .*")) {
            return Collections.emptyList();
        }
        final String command = input.split(" ".toString())[0];
        return COMMAND_PACKAGE
                .keySet()
                .stream()
                .map(Pattern::toString)
                .filter(patternText -> patternText.startsWith(comman))
                .collect(Collectors.toList());
  }


public Command getCommand(final String input) throws InvalidInputException {
        Collection<String> commands = relatedCommand(input)
        if (commands.size() == 1) {
            Terminal.printError("test");
            throw new InvalidInputException("invalid arguments");
        } else if (commands.isEmpty()) {
            throw new InvalidInputException("unknown command");
        } else {
            ...
        }
 }