bitflicker64 commented on code in PR #3241:
URL: https://github.com/apache/hugegraph/pull/3241#discussion_r4268029600
##########
hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/cypher/CypherModel.java:
##########
@@ -45,11 +52,15 @@ public static CypherModel dataOf(String requestId,
List<Object> data) {
return res;
}
- public static CypherModel failOf(String requestId, String message) {
+ public static CypherModel failOf(String requestId, String message,
+ CypherError error) {
CypherModel res = new CypherModel();
res.requestId = requestId;
res.status.code = 400;
res.status.message = message;
+ if (error != null) {
+ res.errors = Collections.singletonList(error);
Review Comment:
Important: Failed queries still break the Java client and Hubble, because
`failOf` always adds a top-level `errors` array. The `NON_EMPTY` change fixed
only the success path.
hugegraph-client's `CypherAPI.post` parses every `/cypher` body with
`RestResult.readObject(Response.class)`. `Response` declares only `requestId`,
`status` and `result`, has no `@JsonIgnoreProperties`, and `RestResult` uses a
default `ObjectMapper`, so the unknown field throws before any caller sees
`status`. On toolchain master, `CypherManager.execute()` never reaches its
`status.code() != 200` branch, which is what turns the server's
`status.message` into a `ServerException`. Hubble's
`QueryService.executeCypher()` then gets a `SerializeException` (`Failed to
deserialize: ...`) through its `ClientException` branch, where it used to get
the Cypher error through the `ServerException` branch.
Evidence: with `hugegraph-client-1.8.0.jar`, `hugegraph-common-1.7.0.jar`
and Jackson 2.15.2, a default `ObjectMapper` reads today's failure body
(`{"requestId":"r1","status":{"message":"Variable `x` not
defined","code":400,"attributes":{}},"result":{"data":[],"meta":{}}}`) into
`Response` with `status.code=400`. The same body with this PR's `errors` entry
appended throws `UnrecognizedPropertyException: Unrecognized field "errors"
(class org.apache.hugegraph.structure.gremlin.Response), not marked as
ignorable`. `testSuccessResponseKeepsLegacyShape` covers only the success body,
and the three failure tests read the body as a string, so nothing in the build
catches this.
Requested change: to answer your open question, please move the code and
hint into `status.attributes` (for example `{"code": ..., "hint": ...}`) and
drop the top-level `errors` field. `CypherModel.Status.attributes` already
exists and the client maps it as `Map<String, ?>`, so no client release is
needed. Then add a failure-path test that parses a failed response with
`FAIL_ON_UNKNOWN_PROPERTIES` enabled, the way the success test does.
##########
hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/cypher/CypherErrorMapper.java:
##########
@@ -0,0 +1,79 @@
+/*
+ * 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;
+
+/**
+ * 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 CypherErrorMapper() {
+ }
+
+ public static CypherModel.CypherError map(Throwable e) {
+ String raw = e.getMessage() != null ? e.getMessage() : e.toString();
+ // translator/parser exception types indicate a real parse failure even
+ // when the message is generic; check class name and FULL message
+ // before the class-prefix strip below removes the evidence
+ String className = e.getClass().getName();
+ String lowerFull = raw.toLowerCase();
+ if (className.contains("SyntaxException")
+ || className.contains("ParseException")
+ || lowerFull.contains("syntaxexception")
+ || lowerFull.contains("parseexception")
+ || lowerFull.contains("no viable alternative")
+ // openCypher front-end: "Invalid input 'R': expected ..."
+ || lowerFull.contains("invalid input '")
+ || lowerFull.contains("mismatched input")
+ || lowerFull.contains("extraneous input")
+ || lowerFull.contains("token recognition error")) {
+ return new CypherModel.CypherError(SYNTAX_ERROR,
+ raw.replaceFirst(
+
"^([a-zA-Z0-9_]+\\.)+[A-Za-z0-9_]+(Exception|Error):\\s*", ""),
+ "Check the Cypher statement near the position reported " +
+ "in the message");
+ }
+ String message = raw.replaceFirst(
+ "^([a-zA-Z0-9_]+\\.)+[A-Za-z0-9_]+(Exception|Error):\\s*", "");
+ String lower = message.toLowerCase();
+ if (lower.contains("undefined vertex label")
+ || lower.contains("undefined edge label")
+ || lower.contains("undefined property key")) {
+ return new CypherModel.CypherError(EXECUTION_ERROR, message,
+ "Create the schema element via the schema API before " +
+ "running the query");
+ }
+ if (lower.contains("not defined") || lower.contains("undefined")) {
+ return new CypherModel.CypherError(SYNTAX_ERROR, message,
+ "Declare the variable in a MATCH, UNWIND or WITH clause " +
+ "before referencing it");
+ }
+ if (lower.contains("not supported") || lower.contains("unsupported")) {
Review Comment:
Minor: This branch matches `not supported` or `unsupported` anywhere in the
message, so HugeGraph's own query limits come back as `UnsupportedFeature` with
a hint that blames the translator.
Evidence: at this head, `TraversalUtil.newUnsupportedPredicate()` throws
`BackendException("Unsupported predicate: '%s'")`, and `LocalContainsStep`
raises `CONTAINS query with relation '%s' is not supported`. Both run in the
HugeGraph traversal optimizer while a translated Cypher traversal executes,
after the translator has accepted the query, so pointing the user at the Cypher
compatibility guide sends them to the wrong place.
Requested change: limit `UNSUPPORTED_FEATURE` to failures raised by the
translator (check the exception class or the specific translator messages), and
let HugeGraph backend and optimizer messages fall through to `EXECUTION_ERROR`.
A test that sends a query using an unsupported predicate would cover it.
--
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]