Skip to content

Commit cab7c4d

Browse files
l46kokcopybara-github
authored andcommitted
Fix error message for missing attribute
PiperOrigin-RevId: 872160282
1 parent b68d0ea commit cab7c4d

File tree

4 files changed

+44
-13
lines changed

4 files changed

+44
-13
lines changed

common/src/main/java/dev/cel/common/exceptions/CelAttributeNotFoundException.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ public static CelAttributeNotFoundException forFieldResolution(Collection<String
3939
return new CelAttributeNotFoundException(formatErrorMessage(fields));
4040
}
4141

42+
public static CelAttributeNotFoundException forMissingAttributes(Collection<String> attributes) {
43+
return new CelAttributeNotFoundException(
44+
"No such attribute(s): " + String.join(", ", attributes));
45+
}
46+
4247
private static String formatErrorMessage(Collection<String> fields) {
4348
String maybePlural = "";
4449
if (fields.size() > 1) {

runtime/src/main/java/dev/cel/runtime/planner/MissingAttribute.java

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,44 @@
2222
final class MissingAttribute implements Attribute {
2323

2424
private final ImmutableSet<String> missingAttributes;
25+
private final Kind kind;
2526

2627
@Override
2728
public Object resolve(GlobalResolver ctx, ExecutionFrame frame) {
28-
throw CelAttributeNotFoundException.forFieldResolution(missingAttributes);
29+
switch (kind) {
30+
case ATTRIBUTE_NOT_FOUND:
31+
throw CelAttributeNotFoundException.forMissingAttributes(missingAttributes);
32+
case FIELD_NOT_FOUND:
33+
throw CelAttributeNotFoundException.forFieldResolution(missingAttributes);
34+
}
35+
36+
throw new IllegalArgumentException("Unexpected kind: " + kind);
2937
}
3038

3139
@Override
3240
public Attribute addQualifier(Qualifier qualifier) {
3341
throw new UnsupportedOperationException("Unsupported operation");
3442
}
3543

36-
static MissingAttribute newMissingAttribute(String... attributeNames) {
37-
return newMissingAttribute(ImmutableSet.copyOf(attributeNames));
44+
static MissingAttribute newMissingAttribute(ImmutableSet<String> attributeNames) {
45+
return new MissingAttribute(attributeNames, Kind.ATTRIBUTE_NOT_FOUND);
3846
}
3947

40-
static MissingAttribute newMissingAttribute(ImmutableSet<String> attributeNames) {
41-
return new MissingAttribute(attributeNames);
48+
static MissingAttribute newMissingField(String... attributeNames) {
49+
return newMissingField(ImmutableSet.copyOf(attributeNames));
4250
}
4351

44-
private MissingAttribute(ImmutableSet<String> missingAttributes) {
52+
static MissingAttribute newMissingField(ImmutableSet<String> attributeNames) {
53+
return new MissingAttribute(attributeNames, Kind.FIELD_NOT_FOUND);
54+
}
55+
56+
private MissingAttribute(ImmutableSet<String> missingAttributes, Kind kind) {
4557
this.missingAttributes = missingAttributes;
58+
this.kind = kind;
59+
}
60+
61+
private enum Kind {
62+
ATTRIBUTE_NOT_FOUND,
63+
FIELD_NOT_FOUND
4664
}
4765
}

runtime/src/main/java/dev/cel/runtime/planner/PresenceTestQualifier.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
package dev.cel.runtime.planner;
1616

17-
import static dev.cel.runtime.planner.MissingAttribute.newMissingAttribute;
17+
import static dev.cel.runtime.planner.MissingAttribute.newMissingField;
1818

1919
import dev.cel.common.values.SelectableValue;
2020
import java.util.Map;
@@ -40,7 +40,7 @@ public Object qualify(Object obj) {
4040
return map.containsKey(value);
4141
}
4242

43-
return newMissingAttribute(value.toString());
43+
return newMissingField(value.toString());
4444
}
4545

4646
static PresenceTestQualifier create(Object value) {

runtime/src/test/java/dev/cel/runtime/planner/ProgramPlannerTest.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,15 @@ public void planIdent_typeLiteral(@TestParameter TypeLiteralTestCase testCase) t
300300
assertThat(result).isEqualTo(testCase.type);
301301
}
302302

303+
@Test
304+
public void plan_ident_missingAttribute_throws() throws Exception {
305+
CelAbstractSyntaxTree ast = compile("int_var");
306+
Program program = PLANNER.plan(ast);
307+
308+
CelEvaluationException e = assertThrows(CelEvaluationException.class, program::eval);
309+
assertThat(e).hasMessageThat().contains("evaluation error at <input>:0: No such attribute(s)");
310+
}
311+
303312
@Test
304313
public void plan_ident_withContainer() throws Exception {
305314
CelAbstractSyntaxTree ast = compile("abbr.ident");
@@ -713,14 +722,13 @@ public void plan_select_onMapVariable() throws Exception {
713722
public void plan_select_mapVarInputMissing_throws() throws Exception {
714723
CelAbstractSyntaxTree ast = compile("map_var.foo");
715724
Program program = PLANNER.plan(ast);
716-
String errorMessage = "evaluation error at <input>:7: Error resolving ";
725+
String errorMessage = "evaluation error at <input>:7: No such attribute(s): ";
717726
if (isParseOnly) {
718727
errorMessage +=
719-
"fields 'cel.expr.conformance.proto3.map_var, cel.expr.conformance.map_var,"
720-
+ " cel.expr.map_var, cel.map_var, map_var'";
721-
} else {
722-
errorMessage += "field 'map_var'";
728+
"cel.expr.conformance.proto3.map_var, cel.expr.conformance.map_var, cel.expr.map_var,"
729+
+ " cel.map_var, ";
723730
}
731+
errorMessage += "map_var";
724732

725733
CelEvaluationException e =
726734
assertThrows(CelEvaluationException.class, () -> program.eval(ImmutableMap.of()));

0 commit comments

Comments
 (0)