Repository navigation
feat(api): return structured error code and hint for Cypher query failures #3241
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
ed21e39
aabdde7
dc076e0
16cd32e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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")) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: This branch matches Evidence: at this head, Requested change: limit |
||
| return new CypherModel.CypherError(UNSUPPORTED_FEATURE, message, | ||
| "This construct is not covered by the built-in translator, " + | ||
| "see the Cypher compatibility guide for supported syntax"); | ||
| } | ||
| return new CypherModel.CypherError(EXECUTION_ERROR, message, ""); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,8 @@ | |
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonInclude; | ||
|
|
||
| import io.swagger.v3.oas.annotations.media.Schema; | ||
|
|
||
| /** | ||
|
|
@@ -37,6 +39,11 @@ public class CypherModel { | |
| @Schema(description = "The query result") | ||
| public Result result = new Result(); | ||
|
|
||
| @Schema(description = "Structured errors, present only on failure so that " | ||
| + "older clients keep parsing successful responses") | ||
| @JsonInclude(JsonInclude.Include.NON_EMPTY) | ||
| public List<CypherError> errors = Collections.emptyList(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Critical: This field breaks every existing Java client call to Evidence: with Requested change: keep the top-level shape unchanged. The existing |
||
|
|
||
| public static CypherModel dataOf(String requestId, List<Object> data) { | ||
| CypherModel res = new CypherModel(); | ||
| res.requestId = requestId; | ||
|
|
@@ -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); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Important: Failed queries still break the Java client and Hubble, because hugegraph-client's Evidence: with Requested change: to answer your open question, please move the code and hint into |
||
| } | ||
| return res; | ||
| } | ||
|
|
||
|
|
@@ -77,4 +88,22 @@ private static class Result { | |
| public Map<String, Object> meta = Collections.EMPTY_MAP; | ||
| } | ||
|
|
||
| public static class CypherError { | ||
|
|
||
| @Schema(description = "Stable error classification code") | ||
| public String code; | ||
|
|
||
| @Schema(description = "The error message") | ||
| public String message; | ||
|
|
||
| @Schema(description = "Actionable hint, empty when unavailable") | ||
| public String hint = ""; | ||
|
|
||
| public CypherError(String code, String message, String hint) { | ||
| this.code = code; | ||
| this.message = message; | ||
| this.hint = hint; | ||
| } | ||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Important:
SyntaxErroris assigned only when the message contains "not defined" or "undefined", so real parse errors fall through toExecutionErrorwith an empty hint on line 65. The PR's goal is to let clients branch on the failure mode, and these codes are described as stable, so a client that checks forHugeGraph.Cypher.SyntaxErrorwill miss most syntax errors.Evidence: I ran
CypherAst.parse(q, {})from translation 1.0.4 withgremlin+cfog_server_extensions+inline_parameters:MATCH (n:person RETURN ngivesSyntaxException: Invalid input 'R': expected ..., mapped toExecutionError.MATCH (n:person) RETRUN ngivesInvalid input 'R': expected 'u/U', mapped toExecutionError.MATCH (n) RETURN n; MATCH (m) RETURN mgivesExpected exactly one statement per query but got: 2, mapped toExecutionError.RETURN date()givesSyntaxException: Unknown function 'date', mapped toExecutionError.MATCH (n:person) RETURN not_defined_vargives "Variablenot_defined_varnot defined", the only one mapped toSyntaxError.The PR description's own live check shows the same result: a syntax error returned
HugeGraph.Cypher.ExecutionErrorwith"hint":"". The branch also catches any other HugeGraph message with "undefined", such asUndefined index label: 'x', and tells the user to declare a variable.Requested change: match the parser messages explicitly (
invalid input,expected exactly one statement,unknown function,variable ... not defined) forSyntaxError, narrow the variable hint to theVariable ... not definedmessage, and add a test that sends a real parse error such asMATCH (n:person RETURN nand asserts"code":"HugeGraph.Cypher.SyntaxError".