-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Command parsers: use exceptions for error handling #305 #333
Conversation
@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. |
abf8142
to
351cd40
Compare
v1@yamgent submitted v1 for review.
Checkout this PR version locallygit fetch https://github.com/se-edu/addressbook-level4.git refs/pr/333/1/head:BRANCHNAME where |
There was a problem hiding this 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.
I'm not sure referring to command parsers in general as |
There was a problem hiding this 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 { |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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()); |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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?
f698917
to
9b44ce9
Compare
v2@yamgent submitted v2 for review.
(📚 Archive) (📈 Interdiff between v1 and v2) Checkout this PR version locallygit fetch https://github.com/se-edu/addressbook-level4.git refs/pr/333/2/head:BRANCHNAME where |
There was a problem hiding this 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
ormore explicit
There was a problem hiding this 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; |
There was a problem hiding this comment.
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?
9b44ce9
to
9fc4133
Compare
v3@yamgent submitted v3 for review.
(📚 Archive) (📈 Interdiff between v2 and v3) Checkout this PR version locallygit fetch https://github.com/se-edu/addressbook-level4.git refs/pr/333/3/head:BRANCHNAME where |
There was a problem hiding this 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.
a048bf4
to
8124f0b
Compare
v15@yamgent submitted v15 for review.
(📚 Archive) (📈 Interdiff between v14 and v15) Checkout this PR version locallygit fetch https://github.com/se-edu/addressbook-level4.git refs/pr/333/15/head:BRANCHNAME where |
@damithc I removed |
caaab15
to
76c4505
Compare
v16@yamgent submitted v16 for review.
(📚 Archive) (📈 Interdiff between v15 and v16) Checkout this PR version locallygit fetch https://github.com/se-edu/addressbook-level4.git refs/pr/333/16/head:BRANCHNAME where |
v16: Rebased @damithc Just a reminder for the review. :) |
There was a problem hiding this 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
76c4505
to
e014124
Compare
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.
e014124
to
3b626bb
Compare
v17@yamgent submitted v17 for review.
(📚 Archive) (📈 Interdiff between v16 and v17) Checkout this PR version locallygit fetch https://github.com/se-edu/addressbook-level4.git refs/pr/333/17/head:BRANCHNAME where |
Fixes #305.
Proposed merge commit message: