Skip to content

Commit

Permalink
Merge pull request AY2324S1-CS2103T-F12-4#155 from jason-raiin/error-…
Browse files Browse the repository at this point in the history
…messages

ParseException refactor
  • Loading branch information
juzzztinsoong authored Nov 6, 2023
2 parents c45c760 + d3de9a1 commit 4ec7087
Show file tree
Hide file tree
Showing 42 changed files with 670 additions and 495 deletions.
15 changes: 15 additions & 0 deletions src/main/java/seedu/address/commons/util/StringUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,19 @@ public static boolean isNonZeroUnsignedInteger(String s) {
return false;
}
}

/**
* Returns true if {@code s} is an integer and false otherwise.
* @throws NullPointerException if {@code s} is null.
*/
public static boolean isNumeric(String s) {
requireNonNull(s);

try {
Integer.parseInt(s);
return true;
} catch (NumberFormatException nfe) {
return false;
}
}
}
23 changes: 17 additions & 6 deletions src/main/java/seedu/address/logic/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,28 @@
*/
public class Messages {

public static final String MESSAGE_UNKNOWN_COMMAND = "Unknown command";
public static final String MESSAGE_INVALID_COMMAND_FORMAT = "Invalid command format! \n%1$s";
public static final String MESSAGE_INVALID_PERSON_DISPLAYED_INDEX = "The person index provided is invalid";
public static final String MESSAGE_INVALID_MEETING_DISPLAYED_INDEX = "The meeting index provided is invalid";
public static final String MESSAGE_INVALID_ATTENDEE_INDEX = "The attendee index provided is invalid";
// success messages
public static final String MESSAGE_PERSONS_LISTED_OVERVIEW = "%1$d persons listed!";
public static final String MESSAGE_PERSON_VIEWED_OVERVIEW = "Listing details for %s!";
public static final String MESSAGE_MEETING_VIEWED_OVERVIEW = "Listing details for meeting: %s";
public static final String MESSAGE_MEETINGS_LISTED_OVERVIEW = "%1$d meetings listed!";

// command parsing error messages
public static final String MESSAGE_UNKNOWN_COMMAND = "Unknown command";
public static final String MESSAGE_INVALID_COMMAND_FORMAT = "Invalid command format!";
public static final String MESSAGE_DUPLICATE_FIELDS = "Multiple values specified for the following "
+ "single-valued field(s): ";
public static final String MESSAGE_MEETINGS_LISTED_OVERVIEW = "%1$d meetings listed!";
public static final String MESSAGE_INVALID_FIELDS = "Invalid parameters provided.";

// index parsing error messages
public static final String MESSAGE_INVALID_INDEX_FORMAT = "Index must be a positive integer.";
public static final String MESSAGE_MISSING_INDEX = "Index is a required parameter.";
public static final String MESSAGE_TOO_MANY_INDEXES = "Too many index arguments provided.";

// index value error messages
public static final String MESSAGE_INVALID_PERSON_DISPLAYED_INDEX = "The person index provided is invalid!";
public static final String MESSAGE_INVALID_MEETING_DISPLAYED_INDEX = "The meeting index provided is invalid!";
public static final String MESSAGE_INVALID_ATTENDEE_INDEX = "The attendee index provided is invalid!";

/**
* Returns an error message indicating the duplicate prefixes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
public class AddMeetingContactCommand extends Command {

public static final String COMMAND_WORD = "addmc";
public static final int EXPECTED_INDEXES = 2;

public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Adds the contact indicated by the contact index to the attendees list of the meeting "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
public class DeleteCommand extends Command {

public static final String COMMAND_WORD = "deletec";
public static final int EXPECTED_INDEXES = 1;

public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Deletes the person identified by the index number used in the displayed person list.\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
public class DeleteMeetingCommand extends Command {

public static final String COMMAND_WORD = "deletem";
public static final int EXPECTED_INDEXES = 1;

public static final String MESSAGE_DELETE_MEETING_SUCCESS = "Meeting deleted: %1$s";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
public class EditCommand extends Command {

public static final String COMMAND_WORD = "editc";
public static final int EXPECTED_INDEXES = 1;

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Edits the details of the person identified "
+ "by the index number used in the displayed person list. "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
*/
public class EditMeetingCommand extends Command {
public static final String COMMAND_WORD = "editm";
public static final int EXPECTED_INDEXES = 1;

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Edits the details of the meeting identified "
+ "by the index number used in the displayed meeting list. "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
public class MarkMeetingCommand extends Command {

public static final String COMMAND_WORD = "mark";
public static final int EXPECTED_INDEXES = 1;

public static final String MESSAGE_MARK_MEETING_SUCCESS = "Meeting marked as complete: %1$s";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
public class RemoveMeetingContactCommand extends Command {

public static final String COMMAND_WORD = "rmmc";
public static final int EXPECTED_INDEXES = 2;

public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Removes the attendee indicated by the attendee index in the attendees list of the meeting "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
public class ViewContactCommand extends Command {

public static final String COMMAND_WORD = "viewc";
public static final int EXPECTED_INDEXES = 1;

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Views detailed information of the person identified "
+ "by the index number used in the displayed person list.\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
public class ViewMeetingCommand extends Command {

public static final String COMMAND_WORD = "viewm";
public static final int EXPECTED_INDEXES = 1;

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Views detailed information of the meeting identified "
+ "by the index number used in the displayed meeting list.\n"
Expand Down
44 changes: 24 additions & 20 deletions src/main/java/seedu/address/logic/parser/AddCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,33 +34,37 @@ public class AddCommandParser implements Parser<AddCommand> {
* @throws ParseException if the user input does not conform the expected format
*/
public AddCommand parse(String args) throws ParseException {
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_LASTTIME,
PREFIX_STATUS, PREFIX_REMARK, PREFIX_TAG);
try {
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL,
PREFIX_LASTTIME, PREFIX_STATUS, PREFIX_REMARK, PREFIX_TAG);

if (!arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL)
|| !argMultimap.getPreamble().isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddCommand.MESSAGE_USAGE));
}
if (!arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL)
|| !argMultimap.getPreamble().isEmpty()) {
throw new ParseException(MESSAGE_INVALID_COMMAND_FORMAT);
}

argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_LASTTIME,
PREFIX_STATUS, PREFIX_REMARK);
Name name = ParserUtil.parseName(argMultimap.getValue(PREFIX_NAME).get());
Phone phone = ParserUtil.parsePhone(argMultimap.getValue(PREFIX_PHONE).get());
Email email = ParserUtil.parseEmail(argMultimap.getValue(PREFIX_EMAIL).get());
LocalDateTime lastContactedTime = ParserUtil.parseContactTime(argMultimap.getValue(PREFIX_LASTTIME).orElse(""));
Status status = ParserUtil.parseStatus(argMultimap.getValue(PREFIX_STATUS).orElse(""));
Remark remark = ParserUtil.parseRemark(argMultimap.getValue(PREFIX_REMARK).orElse(""));
Set<Tag> tagList = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG));
argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_LASTTIME,
PREFIX_STATUS, PREFIX_REMARK);
Name name = ParserUtil.parseName(argMultimap.getValue(PREFIX_NAME).get());
Phone phone = ParserUtil.parsePhone(argMultimap.getValue(PREFIX_PHONE).get());
Email email = ParserUtil.parseEmail(argMultimap.getValue(PREFIX_EMAIL).get());
LocalDateTime lastContactedTime = ParserUtil
.parseContactTime(argMultimap.getValue(PREFIX_LASTTIME).orElse(""));
Status status = ParserUtil.parseStatus(argMultimap.getValue(PREFIX_STATUS).orElse(""));
Remark remark = ParserUtil.parseRemark(argMultimap.getValue(PREFIX_REMARK).orElse(""));
Set<Tag> tagList = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG));

Person person = new Person(name, phone, email, lastContactedTime, status, remark, tagList);
Person person = new Person(name, phone, email, lastContactedTime, status, remark, tagList);

return new AddCommand(person);
return new AddCommand(person);
} catch (ParseException pe) {
throw new ParseException(AddCommand.MESSAGE_USAGE, pe);
}
}

/**
* Returns true if none of the prefixes contains empty {@code Optional} values in the given
* {@code ArgumentMultimap}.
* Returns true if none of the prefixes contains empty {@code Optional} values
* in the given {@code ArgumentMultimap}.
*/
private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) {
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,35 +38,39 @@ public class AddMeetingCommandParser implements Parser<AddMeetingCommand> {
* @throws ParseException if the user input does not conform the expected format
*/
public AddMeetingCommand parse(String args) throws ParseException {
logger.info("Begin AddMeetingCommand Parse");
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_TITLE, PREFIX_LOCATION, PREFIX_START, PREFIX_END, PREFIX_TAG);
try {
logger.info("Begin AddMeetingCommand Parse");
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_TITLE, PREFIX_LOCATION, PREFIX_START,
PREFIX_END, PREFIX_TAG);

if (!arePrefixesPresent(argMultimap, PREFIX_TITLE, PREFIX_LOCATION, PREFIX_START, PREFIX_END)
|| !argMultimap.getPreamble().isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddMeetingCommand.MESSAGE_USAGE));
}
if (!arePrefixesPresent(argMultimap, PREFIX_TITLE, PREFIX_LOCATION, PREFIX_START, PREFIX_END)
|| !argMultimap.getPreamble().isEmpty()) {
throw new ParseException(MESSAGE_INVALID_COMMAND_FORMAT);
}

argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_TITLE, PREFIX_LOCATION, PREFIX_START, PREFIX_END);
Title title = ParserUtil.parseTitle(argMultimap.getValue(PREFIX_TITLE).get());
Location location = ParserUtil.parseLocation(argMultimap.getValue(PREFIX_LOCATION).get());
LocalDateTime start = ParserUtil.parseMeetingTime(argMultimap.getValue(PREFIX_START).get());
LocalDateTime end = ParserUtil.parseMeetingTime(argMultimap.getValue(PREFIX_END).get());
if (!MeetingTime.isValidMeetingTime(start, end)) {
throw new ParseException(MeetingTime.MESSAGE_CONSTRAINTS);
}
Set<Attendee> attendeeList = ParserUtil.parseAttendees(argMultimap.getAllValues(PREFIX_NAME));
Set<Tag> tagList = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG));
MeetingStatus status = new MeetingStatus(false);
argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_TITLE, PREFIX_LOCATION, PREFIX_START, PREFIX_END);
Title title = ParserUtil.parseTitle(argMultimap.getValue(PREFIX_TITLE).get());
Location location = ParserUtil.parseLocation(argMultimap.getValue(PREFIX_LOCATION).get());
LocalDateTime start = ParserUtil.parseMeetingTime(argMultimap.getValue(PREFIX_START).get());
LocalDateTime end = ParserUtil.parseMeetingTime(argMultimap.getValue(PREFIX_END).get());
if (!MeetingTime.isValidMeetingTime(start, end)) {
throw new ParseException(MeetingTime.MESSAGE_CONSTRAINTS);
}
Set<Attendee> attendeeList = ParserUtil.parseAttendees(argMultimap.getAllValues(PREFIX_NAME));
Set<Tag> tagList = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG));
MeetingStatus status = new MeetingStatus(false);

Meeting meeting = new Meeting(title, location, start, end, attendeeList, tagList, status);
Meeting meeting = new Meeting(title, location, start, end, attendeeList, tagList, status);

return new AddMeetingCommand(meeting);
return new AddMeetingCommand(meeting);
} catch (ParseException pe) {
throw new ParseException(AddMeetingCommand.MESSAGE_USAGE, pe);
}
}

/**
* Returns true if none of the prefixes contains empty {@code Optional} values in the given
* {@code ArgumentMultimap}.
* Returns true if none of the prefixes contains empty {@code Optional} values
* in the given {@code ArgumentMultimap}.
*/
private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) {
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package seedu.address.logic.parser;

import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;

import java.util.List;

import seedu.address.commons.core.index.Index;
Expand All @@ -21,21 +19,17 @@ public class AddMeetingContactCommandParser implements Parser<AddMeetingContactC
* @throws ParseException if the user input does not conform to the expected format
*/
public AddMeetingContactCommand parse(String args) throws ParseException {
List<Index> indexes;
try {
List<Index> indexes = ParserUtil.parseIndexes(args);
if (indexes.size() != 2) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddMeetingContactCommand.MESSAGE_USAGE));
}

Index meetingIndex = indexes.get(0);
Index contactIndex = indexes.get(1);
return new AddMeetingContactCommand(meetingIndex, contactIndex);
indexes = ParserUtil.parseIndexes(args, AddMeetingContactCommand.EXPECTED_INDEXES);
} catch (ParseException pe) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddMeetingContactCommand.MESSAGE_USAGE), pe);
throw new ParseException(AddMeetingContactCommand.MESSAGE_USAGE, pe);
}
}

Index meetingIndex = indexes.get(0);
Index contactIndex = indexes.get(1);

return new AddMeetingContactCommand(meetingIndex, contactIndex);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.Messages.MESSAGE_UNKNOWN_COMMAND;
import static seedu.address.logic.parser.ParserUtil.verifyNoArgs;

import java.util.logging.Logger;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -50,7 +51,7 @@ public class AddressBookParser {
public Command parseCommand(String userInput) throws ParseException {
final Matcher matcher = BASIC_COMMAND_FORMAT.matcher(userInput.trim());
if (!matcher.matches()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, HelpCommand.MESSAGE_USAGE));
throw new ParseException(MESSAGE_INVALID_COMMAND_FORMAT + "\n" + HelpCommand.MESSAGE_USAGE);
}

final String commandWord = matcher.group("commandWord");
Expand All @@ -73,21 +74,26 @@ public Command parseCommand(String userInput) throws ParseException {
return new DeleteCommandParser().parse(arguments);

case ClearCommand.COMMAND_WORD:
verifyNoArgs(arguments);
return new ClearCommand();

case FindCommand.COMMAND_WORD:
return new FindCommandParser().parse(arguments);

case ListCommand.COMMAND_WORD:
verifyNoArgs(arguments);
return new ListCommand();

case ListMeetingCommand.COMMAND_WORD:
verifyNoArgs(arguments);
return new ListMeetingCommand();

case ExitCommand.COMMAND_WORD:
verifyNoArgs(arguments);
return new ExitCommand();

case HelpCommand.COMMAND_WORD:
verifyNoArgs(arguments);
return new HelpCommand();

case ViewContactCommand.COMMAND_WORD:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package seedu.address.logic.parser;

import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.DeleteCommand;
import seedu.address.logic.parser.exceptions.ParseException;
Expand All @@ -17,13 +15,14 @@ public class DeleteCommandParser implements Parser<DeleteCommand> {
* @throws ParseException if the user input does not conform the expected format
*/
public DeleteCommand parse(String args) throws ParseException {
Index index;
try {
Index index = ParserUtil.parseIndex(args);
return new DeleteCommand(index);
index = ParserUtil.parseIndexes(args, DeleteCommand.EXPECTED_INDEXES).get(0);
} catch (ParseException pe) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteCommand.MESSAGE_USAGE), pe);
throw new ParseException(DeleteCommand.MESSAGE_USAGE, pe);
}

return new DeleteCommand(index);
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package seedu.address.logic.parser;

import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.DeleteMeetingCommand;
import seedu.address.logic.parser.exceptions.ParseException;
Expand All @@ -17,13 +15,14 @@ public class DeleteMeetingCommandParser implements Parser<DeleteMeetingCommand>
* @throws ParseException if the user input does not conform the expected format
*/
public DeleteMeetingCommand parse(String args) throws ParseException {
Index index;
try {
Index index = ParserUtil.parseIndex(args);
return new DeleteMeetingCommand(index);
index = ParserUtil.parseIndexes(args, DeleteMeetingCommand.EXPECTED_INDEXES).get(0);
} catch (ParseException pe) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteMeetingCommand.MESSAGE_USAGE), pe);
throw new ParseException(DeleteMeetingCommand.MESSAGE_USAGE, pe);
}

return new DeleteMeetingCommand(index);
}

}
Loading

0 comments on commit 4ec7087

Please sign in to comment.