bitflicker64 commented on code in PR #3259:
URL: https://github.com/apache/hugegraph/pull/3259#discussion_r4267201578


##########
hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/cypher/CypherErrorMapper.java:
##########
@@ -0,0 +1,76 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.hugegraph.api.cypher;
+
+import java.util.Locale;
+import java.util.regex.Pattern;
+
+/**
+ * Classify cypher execution failures into stable error codes and attach
+ * actionable hints for the most common, cryptic translator messages.
+ */
+public final class CypherErrorMapper {
+
+    public static final String SYNTAX_ERROR = "HugeGraph.Cypher.SyntaxError";
+    public static final String UNSUPPORTED_FEATURE =
+            "HugeGraph.Cypher.UnsupportedFeature";
+    public static final String EXECUTION_ERROR = 
"HugeGraph.Cypher.ExecutionError";
+
+    private static final Pattern UNDEFINED_VARIABLE =
+            Pattern.compile("variable `[^`]+` not defined");
+
+    private CypherErrorMapper() {
+    }
+
+    public static CypherModel.CypherError map(Throwable e) {
+        String message = e.getMessage() != null ? e.getMessage() : 
e.toString();
+        // strip noisy exception class prefixes, e.g. 
"...driver.exception.ResponseException: "
+        message = message.replaceFirst(
+                "^([a-zA-Z0-9_]+\\.)+[A-Za-z0-9_]+(Exception|Error):\\s*", "");
+        String lower = message.toLowerCase(Locale.ROOT);
+
+        if (lower.contains("undefined vertex label")
+                || lower.contains("undefined edge label")
+                || lower.contains("undefined property key")
+                || lower.contains("undefined index label")) {
+            return new CypherModel.CypherError(EXECUTION_ERROR, message,
+                    "Create the schema element via the schema API before " +
+                    "running the query");
+        }
+        if (UNDEFINED_VARIABLE.matcher(lower).find()) {
+            return new CypherModel.CypherError(SYNTAX_ERROR, message,
+                    "Declare the variable in a MATCH, UNWIND or WITH clause " +
+                    "before referencing it");
+        }
+        if (lower.contains("invalid input") || lower.contains("unknown 
function")) {
+            return new CypherModel.CypherError(SYNTAX_ERROR, message,
+                    "Check the query syntax and function names supported by " +
+                    "the built-in Cypher translator");
+        }
+        if (lower.contains("expected exactly one statement")) {
+            return new CypherModel.CypherError(SYNTAX_ERROR, message,
+                    "Submit exactly one Cypher statement per request");
+        }
+        if (lower.contains("not supported") || lower.contains("unsupported")) {

Review Comment:
   Minor: The "not supported" / "unsupported" substring match also catches 
HugeGraph runtime errors, not only translator gaps.
   
   Static trace: TraversalUtil.newUnsupportedPredicate() throws 
BackendException "Unsupported predicate: '%s'", and TraversalUtil line 965 
throws "CONTAINS query with relation '%s' is not supported". Raised while a 
translated traversal executes, they reach CypherClient as ResponseException 
messages and get UnsupportedFeature with the hint "This construct is not 
covered by the built-in translator", which points the user at the wrong layer.
   
   Match the translator's own forms (messages starting with "Unsupported 
clause", "Unsupported match pattern" and similar) instead of any occurrence of 
the words.



##########
hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/cypher/CypherErrorMapper.java:
##########
@@ -0,0 +1,76 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.hugegraph.api.cypher;
+
+import java.util.Locale;
+import java.util.regex.Pattern;
+
+/**
+ * Classify cypher execution failures into stable error codes and attach
+ * actionable hints for the most common, cryptic translator messages.
+ */
+public final class CypherErrorMapper {
+
+    public static final String SYNTAX_ERROR = "HugeGraph.Cypher.SyntaxError";
+    public static final String UNSUPPORTED_FEATURE =
+            "HugeGraph.Cypher.UnsupportedFeature";
+    public static final String EXECUTION_ERROR = 
"HugeGraph.Cypher.ExecutionError";
+
+    private static final Pattern UNDEFINED_VARIABLE =
+            Pattern.compile("variable `[^`]+` not defined");
+
+    private CypherErrorMapper() {
+    }
+
+    public static CypherModel.CypherError map(Throwable e) {
+        String message = e.getMessage() != null ? e.getMessage() : 
e.toString();
+        // strip noisy exception class prefixes, e.g. 
"...driver.exception.ResponseException: "
+        message = message.replaceFirst(
+                "^([a-zA-Z0-9_]+\\.)+[A-Za-z0-9_]+(Exception|Error):\\s*", "");
+        String lower = message.toLowerCase(Locale.ROOT);
+
+        if (lower.contains("undefined vertex label")
+                || lower.contains("undefined edge label")
+                || lower.contains("undefined property key")
+                || lower.contains("undefined index label")) {
+            return new CypherModel.CypherError(EXECUTION_ERROR, message,
+                    "Create the schema element via the schema API before " +
+                    "running the query");
+        }
+        if (UNDEFINED_VARIABLE.matcher(lower).find()) {
+            return new CypherModel.CypherError(SYNTAX_ERROR, message,
+                    "Declare the variable in a MATCH, UNWIND or WITH clause " +
+                    "before referencing it");
+        }
+        if (lower.contains("invalid input") || lower.contains("unknown 
function")) {

Review Comment:
   Important: Front-end semantic errors from the translator are not classified 
as SyntaxError, and one is labelled UnsupportedFeature.
   
   Probe at e3cd2e2 (CypherAst.parse + this mapper, same wrapping as 
testRealParserFailures): "MATCH (n) RETURN n.age + 'a' * true" (Type mismatch), 
"CREATE (n) CREATE (n) RETURN n" (Variable `n` already declared), "MATCH (n) 
WITH n.name RETURN n" (Expression in WITH must be aliased) all throw 
SyntaxException but map to ExecutionError with an empty hint. "MATCH (n) RETURN 
1 AS a, 2 AS a" throws SyntaxException "Multiple result columns with the same 
name are not supported" and maps to UnsupportedFeature with the 
translator-coverage hint. A client that routes on code treats a query that 
never ran as an execution failure.
   
   Treat the parser position suffix "(line N, column M (offset: K))" that these 
messages carry as a SyntaxError marker before the unsupported check, and add 
these queries to testRealParserFailures.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to