forked from airlift/airline
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial work on improved error handling (#53)
Adds a new ParserErrorHandler interface and incorporates it into the ParserMetadata. Doesn't yet utilise it properly
- Loading branch information
Showing
10 changed files
with
326 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
airline-core/src/main/java/com/github/rvesse/airline/parser/ParseResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/** | ||
* Copyright (C) 2010-16 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.github.rvesse.airline.parser; | ||
|
||
import java.util.Collection; | ||
import java.util.Collections; | ||
|
||
import com.github.rvesse.airline.parser.errors.ParseException; | ||
|
||
/** | ||
* Represents parsing results | ||
* | ||
* @author rvesse | ||
* | ||
* @param <T> | ||
* Command type | ||
*/ | ||
public class ParseResult<T> { | ||
private final ParseState<T> state; | ||
private final Collection<ParseException> errors; | ||
|
||
public ParseResult(ParseState<T> state, Collection<ParseException> errors) { | ||
if (state == null) | ||
throw new NullPointerException("state cannot be null"); | ||
this.state = state; | ||
this.errors = errors != null ? Collections.<ParseException> unmodifiableCollection(errors) | ||
: Collections.<ParseException> emptyList(); | ||
} | ||
|
||
/** | ||
* Indicates whether parsing was successful | ||
* | ||
* @return True if successful, false if any errors occurred | ||
*/ | ||
public boolean wasSuccessful() { | ||
return this.errors.size() == 0; | ||
} | ||
|
||
/** | ||
* Gets the final parser state | ||
* | ||
* @return Parser state | ||
*/ | ||
public ParseState<T> getState() { | ||
return this.state; | ||
} | ||
|
||
/** | ||
* Gets the collection of errors that occurred, may be empty if parsing was | ||
* successful | ||
* | ||
* @return Errors | ||
*/ | ||
public Collection<ParseException> getErrors() { | ||
return this.errors; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...main/java/com/github/rvesse/airline/parser/errors/handlers/AbstractCollectingHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* Copyright (C) 2010-16 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.github.rvesse.airline.parser.errors.handlers; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import com.github.rvesse.airline.parser.errors.ParseException; | ||
|
||
public abstract class AbstractCollectingHandler implements ParserErrorHandler { | ||
|
||
protected List<ParseException> errors = new ArrayList<>(); | ||
|
||
public AbstractCollectingHandler() { | ||
super(); | ||
} | ||
|
||
@Override | ||
public void handleError(ParseException e) { | ||
this.errors.add(e); | ||
} | ||
|
||
protected List<ParseException> getCollection() { | ||
return this.errors; | ||
} | ||
|
||
protected void resetCollection() { | ||
this.errors = new ArrayList<>(); | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
airline-core/src/main/java/com/github/rvesse/airline/parser/errors/handlers/CollectAll.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* Copyright (C) 2010-16 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.github.rvesse.airline.parser.errors.handlers; | ||
|
||
import com.github.rvesse.airline.parser.ParseResult; | ||
import com.github.rvesse.airline.parser.ParseState; | ||
|
||
/** | ||
* Error handler which collects all the errors for later inspection | ||
* | ||
* @author rvesse | ||
* | ||
*/ | ||
public class CollectAll extends AbstractCollectingHandler { | ||
|
||
@Override | ||
public <T> ParseResult<T> finished(ParseState<T> state) { | ||
ParseResult<T> result = new ParseResult<>(state, getCollection()); | ||
resetCollection(); | ||
return result; | ||
} | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
airline-core/src/main/java/com/github/rvesse/airline/parser/errors/handlers/FailAll.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* Copyright (C) 2010-16 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.github.rvesse.airline.parser.errors.handlers; | ||
|
||
import java.util.Collection; | ||
|
||
import com.github.rvesse.airline.parser.ParseResult; | ||
import com.github.rvesse.airline.parser.ParseState; | ||
import com.github.rvesse.airline.parser.errors.ParseException; | ||
|
||
public class FailAll extends AbstractCollectingHandler { | ||
|
||
@Override | ||
public <T> ParseResult<T> finished(ParseState<T> state) { | ||
Collection<ParseException> errors = getCollection(); | ||
if (errors.size() == 1) { | ||
throw errors.iterator().next(); | ||
} else if (errors.size() > 1) { | ||
ParseException aggEx = new ParseException("Parsing encountered %d error(s)", errors.size()); | ||
for (ParseException e : errors) { | ||
aggEx.addSuppressed(e); | ||
} | ||
throw aggEx; | ||
} else { | ||
return new ParseResult<>(state, null); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.