Skip to content
This repository was archived by the owner on Jun 26, 2022. It is now read-only.

Commit 775facb

Browse files
authored
Improve error message for an invalid rule has syntax (#9)
## What is the goal of this PR? Users could quite sensibly think that they can assign a type to a variable attribute in a rule `then`. This is not permitted because the type of a variable in the rule `then` has already been determined by the rule `when`. Users should be clearly told what the exact issue is and how to remedy this. ## What are the changes implemented in this PR? Tell the use the exact type and variable causing the issue and suggest to them to remove the type. For example: ``` rule people-are-the-same: when { $p isa person, has name $n; } then { $p has name $n; }; ``` throws error: ``` [TQL31] TypeQL Error: Rule 'people-are-the-same' 'then' '$p has name $n' tries to assign type 'name' to variable 'n', but this variable already had a type assigned by the rule 'when'. Try omitting this type assignment. ```
1 parent e13abef commit 775facb

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

common/exception/ErrorMessage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public class ErrorMessage extends com.vaticle.typedb.common.exception.ErrorMessa
8484
public static final ErrorMessage INVALID_RULE_THEN =
8585
new ErrorMessage(30, "Rule '%s' 'then' '%s': must be exactly one attribute ownership, or exactly one relation.");
8686
public static final ErrorMessage INVALID_RULE_THEN_HAS =
87-
new ErrorMessage(31, "Rule '%s' 'then' '%s': is trying to assign both an attribute type and a variable attribute value.");
87+
new ErrorMessage(31, "Rule '%s' 'then' '%s' tries to assign type '%s' to variable '%s', but this variable already had a type assigned by the rule 'when'. Try omitting this type assignment.");
8888
public static final ErrorMessage INVALID_RULE_THEN_VARIABLES =
8989
new ErrorMessage(32, "Rule '%s' 'then' variables must be present in rule 'when'.");
9090
public static final ErrorMessage REDUNDANT_NESTED_NEGATION =

pattern/schema/Rule.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,15 @@ private static void validateThen(String label, @Nullable Conjunction<? extends P
134134

135135
// rule 'has' cannot assign both an attribute type and a named variable
136136
if (then.has().size() == 1 && then.has().get(0).type().isPresent() && then.has().get(0).attribute().isNamed()) {
137-
throw TypeQLException.of(INVALID_RULE_THEN_HAS.message(label, then));
137+
String attrVar = then.has().get(0).attribute().name();
138+
String attrType;
139+
if (then.has().get(0).type().get().label().isPresent()) {
140+
attrType = then.has().get(0).type().get().label().get().label();
141+
} else {
142+
assert then.has().get(0).type().get().isNamed();
143+
attrType = then.has().get(0).type().get().name();
144+
}
145+
throw TypeQLException.of(INVALID_RULE_THEN_HAS.message(label, then, attrType, attrVar));
138146
}
139147

140148
// all user-written variables in the 'then' must be present in the 'when', if it exists.

0 commit comments

Comments
 (0)