Gracefully fail when submit_rule/1 has no solutions
If a submit_rule is coded with no solution for a change, e.g.: submit_rule(submit()) :- fail. the server used to crash with 500s from a number of REST APIs, caused by a ClassCastException inside of SubmitRuleEvaluator. An empty result list is not a ListTerm, it is a SymbolTerm and using the NIL instance '[]'. This cannot be cast to a ListTerm for return to the caller. Instead return List<Term> and handle the conversion, ensuring an empty list is returned to callers when there are no results. Change-Id: Iea1211afb2f3970ba98ea1eff14df07452cf5ccd
This commit is contained in:
@@ -14,6 +14,7 @@
|
||||
|
||||
package com.google.gerrit.server.project;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.gerrit.reviewdb.client.Change;
|
||||
import com.google.gerrit.reviewdb.client.PatchSet;
|
||||
import com.google.gerrit.reviewdb.server.ReviewDb;
|
||||
@@ -30,6 +31,7 @@ import com.googlecode.prolog_cafe.lang.VariableTerm;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
@@ -124,8 +126,7 @@ public class SubmitRuleEvaluator {
|
||||
* @return List of {@link Term} objects returned from the evaluated rules.
|
||||
* @throws RuleEvalException
|
||||
*/
|
||||
public ListTerm evaluate() throws RuleEvalException {
|
||||
List<Term> results = new ArrayList<Term>();
|
||||
public List<Term> evaluate() throws RuleEvalException {
|
||||
PrologEnvironment env = getPrologEnvironment();
|
||||
try {
|
||||
submitRule = env.once("gerrit", userRuleLocatorName, new VariableTerm());
|
||||
@@ -133,6 +134,7 @@ public class SubmitRuleEvaluator {
|
||||
env.once("gerrit", "assume_range_from_label");
|
||||
}
|
||||
|
||||
List<Term> results = new ArrayList<Term>();
|
||||
try {
|
||||
for (Term[] template : env.all("gerrit", userRuleWrapperName,
|
||||
submitRule, new VariableTerm())) {
|
||||
@@ -152,7 +154,16 @@ public class SubmitRuleEvaluator {
|
||||
if (!skipFilters) {
|
||||
resultsTerm = runSubmitFilters(resultsTerm, env);
|
||||
}
|
||||
return (ListTerm) resultsTerm;
|
||||
if (resultsTerm.isList()) {
|
||||
List<Term> r = Lists.newArrayList();
|
||||
for (Term t = resultsTerm; t.isList();) {
|
||||
ListTerm l = (ListTerm) t;
|
||||
r.add(l.car().dereference());
|
||||
t = l.cdr().dereference();
|
||||
}
|
||||
return r;
|
||||
}
|
||||
return Collections.emptyList();
|
||||
} finally {
|
||||
env.close();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user