Skip to content
This repository has been archived by the owner on Mar 5, 2023. It is now read-only.

Command parsers: use exceptions for error handling #305 #333

Merged
merged 3 commits into from
May 29, 2017

Conversation

yamgent
Copy link
Member

@yamgent yamgent commented Mar 11, 2017

Fixes #305.

Proposed merge commit message:

All CommandParser#parse(String) return a generic Command type, instead
of the specific command that they are tasked to parse (for example,
AddCommand for AddCommandParser).

This is because the methods might also return an IncorrectCommand if it
encounters a parse error. This causes an unnecessary loss of specificity
in the return value type if the parsing is successful (e.g. receiving a
Command object instead of an AddCommand object), and in the error info
if the parsing failed (e.g. receiving an IncorrectCommand instead of a
specific Exception object containing the exact stack trace).

Let's change all CommandParser#parse(String) methods to return their
specific command type, and throw a ParseErrorException instead when
encountering an error, to allow the caller to deal with it.

@mention-bot
Copy link

@yamgent, thanks for your PR! By analyzing the history of the files in this pull request, we identified @damithc, @yl-coder and @MightyCupcakes to be potential reviewers.

@CanIHasReview-bot
Copy link

v1

@yamgent submitted v1 for review.

(📚 Archive)

Checkout this PR version locally
git fetch https://github.com/se-edu/addressbook-level4.git refs/pr/333/1/head:BRANCHNAME

where BRANCHNAME is the name of the local branch you want to fetch this PR to.

Copy link
Contributor

@PierceAndy PierceAndy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[v1 1/3]
Commit message:

  • CommandParser -> command parser (referring to command parser classes in general)
  • return an IncorrectCommand -> return an IncorrectCommand object
  • This behavior is not explicit... -> This error handling behavior is not explicit. Instead of throwing an exception as expected, a child class object (IncorrectCommand) of the return type (Command) is returned.

@PierceAndy
Copy link
Contributor

I'm not sure referring to command parsers in general as CommandParser is a good practice, as that might imply that there is a CommandParser parent class.

Copy link
Contributor

@MightyCupcakes MightyCupcakes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No further comments from me

@@ -17,11 +15,11 @@
* Parses the given {@code String} of arguments in the context of the DeleteCommand
* and returns an DeleteCommand object for execution.
*/
public Command parse(String args) {
public DeleteCommand parse(String args) throws ParseErrorException {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two many spaces.

/**
* Represents a parse error encountered by a parser.
*/
public class ParseErrorException extends Exception {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just call it ParseException? Exceptions are supposed to only be used for errors anyway, so the Error is implied.

return new IncorrectCommand(MESSAGE_UNKNOWN_COMMAND);
}
} catch (ParseErrorException parserException) {
return new IncorrectCommand(parserException.getMessage());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than catching it here, propagate it out instead? No reason why we have to go to the trouble of wrapping the exception into a command. We also lose some information as well (e.g. the stacktrace)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reading further through the diff, however, I see that this commit still punts out at returning an IncorrectCommand, which is strange since the commit message promised me that all command parsers would now throw a ParseException? Unless Parser is not considered a command parser?

@yamgent yamgent force-pushed the 305-parsers-exceptions branch 2 times, most recently from f698917 to 9b44ce9 Compare March 14, 2017 04:25
@CanIHasReview-bot
Copy link

v2

@yamgent submitted v2 for review.

(📚 Archive) (📈 Interdiff between v1 and v2)

Checkout this PR version locally
git fetch https://github.com/se-edu/addressbook-level4.git refs/pr/333/2/head:BRANCHNAME

where BRANCHNAME is the name of the local branch you want to fetch this PR to.

Copy link
Contributor

@PierceAndy PierceAndy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[v2 1/4]
Commit message:

  • instead of returning an IncorrectCommand -> instead of returning an IncorrectCommand object
  • found a parse error -> encountered a parse error?
  • more explicit stated -> more explicitly stated or more explicit

Copy link
Contributor

@PierceAndy PierceAndy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[v2 4/4]
Commit message:

  • into an IncorrectCommand -> into an IncorrectCommand object

@@ -0,0 +1,10 @@
package seedu.address.logic.parser;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

create a new sub package seedu.address.logic.parser.exceptions and put this exception in the package to be consistent with the others?

@CanIHasReview-bot
Copy link

v3

@yamgent submitted v3 for review.

(📚 Archive) (📈 Interdiff between v2 and v3)

Checkout this PR version locally
git fetch https://github.com/se-edu/addressbook-level4.git refs/pr/333/3/head:BRANCHNAME

where BRANCHNAME is the name of the local branch you want to fetch this PR to.

Copy link
Contributor

@MightyCupcakes MightyCupcakes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[v3 1/4] Commit message:

Let's add a ParseException, allowing command parsers to throw this
exception, instead of returning an IncorrectCommand object, to tell
the caller that it has encountered a parse error. Therefore, the error
handling behavior of all command parsers' parse(String) methods can be
more explicit.

The opening sentence is too long.

Let's add the ParseException class which will allow the various command parsers to throw this exception instead of returning an IncorrectCommand object. This will inform the caller that a parse error has occurred.

@CanIHasReview-bot
Copy link

v15

@yamgent submitted v15 for review.

(📚 Archive) (📈 Interdiff between v14 and v15)

Checkout this PR version locally
git fetch https://github.com/se-edu/addressbook-level4.git refs/pr/333/15/head:BRANCHNAME

where BRANCHNAME is the name of the local branch you want to fetch this PR to.

@yamgent
Copy link
Member Author

yamgent commented May 23, 2017

@damithc I removed IncorrectCommand for this iteration, need you to take a look at it again to approve the new design decision.

@yamgent yamgent requested a review from damithc May 23, 2017 01:51
@yamgent yamgent force-pushed the 305-parsers-exceptions branch 2 times, most recently from caaab15 to 76c4505 Compare May 25, 2017 02:42
@CanIHasReview-bot
Copy link

v16

@yamgent submitted v16 for review.

(📚 Archive) (📈 Interdiff between v15 and v16)

Checkout this PR version locally
git fetch https://github.com/se-edu/addressbook-level4.git refs/pr/333/16/head:BRANCHNAME

where BRANCHNAME is the name of the local branch you want to fetch this PR to.

@yamgent
Copy link
Member Author

yamgent commented May 25, 2017

v16: Rebased

@damithc Just a reminder for the review. :)

Copy link
Contributor

@damithc damithc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm ok with the code. @throws ParseException if any illegal values are found sounds a bit odd though (feels like "shouldn't we throw IllegalValueException in that case?") May be ... if the command does not conform the the exapected format or something like that? I'll leave up to you

All command parsers return an IncorrectCommand object when they
encounter a parse error.

The parse error can be made more visible if an exception was thrown
instead. Otherwise, the caller is unable to handle any parsing errors
such as IllegalValueException. Context information, such as the
stacktrace, is also lost when wrapping the exception instead of
returning it directly.

Let's change all command parsers' parse(String) methods such that they
throw ParseException instead of returning an IncorrectCommand object.
All command parsers' parse(String) methods return the base Command
type, instead of the specific command that they are tasked to parse
(for example, AddCommand for AddCommandParser).

Let's change all command parsers' parse(String)'s return type from the
generic Command type to their specific command type that they are
tasked to parse.
Parser throws a ParseException instead of IncorrectCommand when
encountering an unrecognized command.

IncorrectCommand is no longer in use anywhere.

Let's remove IncorrectCommand.
@CanIHasReview-bot
Copy link

v17

@yamgent submitted v17 for review.

(📚 Archive) (📈 Interdiff between v16 and v17)

Checkout this PR version locally
git fetch https://github.com/se-edu/addressbook-level4.git refs/pr/333/17/head:BRANCHNAME

where BRANCHNAME is the name of the local branch you wish to fetch this PR to.

@yamgent yamgent removed the request for review from pyokagan May 29, 2017 00:45
@yamgent yamgent merged commit cfc8ecc into se-edu:master May 29, 2017
@yamgent yamgent deleted the 305-parsers-exceptions branch May 29, 2017 00:53
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants