This is an automated email from the ASF dual-hosted git repository.
yangjiaqi pushed a commit to branch master
in repository
https://gitbox.apache.org/repos/asf/incubator-hugegraph-toolchain.git
The following commit(s) were added to refs/heads/master by this push:
new 7bfcd665 feat(hubble): warp the exception info in HugeClientUtil
(#589)
7bfcd665 is described below
commit 7bfcd665515e58a2f618851cb6da9b1c3ee5b4de
Author: Hongjun Li <[email protected]>
AuthorDate: Wed Mar 20 14:30:44 2024 +0800
feat(hubble): warp the exception info in HugeClientUtil (#589)
* feat(hugegraph-hubble): constrain the message and cause returned by
partial exceptions of hubble and server connection creation
---
.editorconfig | 31 ++++++++++++++++++++
.../org/apache/hugegraph/driver/HugeClient.java | 11 +++++++
.../java/org/apache/hugegraph/HugeGraphHubble.java | 3 +-
.../hugegraph/exception/GenericException.java | 34 ++++++++++++++++++++++
.../apache/hugegraph/handler/ExceptionAdvisor.java | 13 +++++++++
.../org/apache/hugegraph/util/HugeClientUtil.java | 30 ++++++++-----------
pom.xml | 8 +++++
7 files changed, 110 insertions(+), 20 deletions(-)
diff --git a/.editorconfig b/.editorconfig
new file mode 100644
index 00000000..5c479266
--- /dev/null
+++ b/.editorconfig
@@ -0,0 +1,31 @@
+#
+# 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.
+#
+
+root = true
+
+[*]
+charset = utf-8
+end_of_line = lf
+insert_final_newline = true
+
+[*.{java, xml, py}]
+indent_style = space
+indent_size = 4
+
+[*.{java, xml}]
+# Ignore the IDEA unsupported warning & it works well (indeed)
+continuation_indent_size = 8
diff --git
a/hugegraph-client/src/main/java/org/apache/hugegraph/driver/HugeClient.java
b/hugegraph-client/src/main/java/org/apache/hugegraph/driver/HugeClient.java
index 290f9171..798405d7 100644
--- a/hugegraph-client/src/main/java/org/apache/hugegraph/driver/HugeClient.java
+++ b/hugegraph-client/src/main/java/org/apache/hugegraph/driver/HugeClient.java
@@ -27,6 +27,11 @@ import org.apache.hugegraph.version.ClientVersion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+/**
+ * The HugeClient class is the main entry point for interacting with a
HugeGraph server.
+ * It provides methods for managing graphs, schemas, jobs, tasks, and other
resources.
+ * It also implements the Closeable interface, so it can be used in a
try-with-resources statement.
+ */
public class HugeClient implements Closeable {
private static final Logger LOG =
LoggerFactory.getLogger(RestClient.class);
@@ -51,6 +56,11 @@ public class HugeClient implements Closeable {
private AuthManager auth;
private MetricsManager metrics;
+ /**
+ * Constructs a new HugeClient using the provided builder.
+ *
+ * @param builder the HugeClientBuilder to use for configuration
+ */
public HugeClient(HugeClientBuilder builder) {
this.borrowedClient = false;
RestClientConfig config;
@@ -76,6 +86,7 @@ public class HugeClient implements Closeable {
try {
this.initManagers(this.client, builder.graph());
} catch (Throwable e) {
+ // TODO: catch some exception(like IO/Network related) rather than
throw Throwable
this.client.close();
throw e;
}
diff --git
a/hugegraph-hubble/hubble-be/src/main/java/org/apache/hugegraph/HugeGraphHubble.java
b/hugegraph-hubble/hubble-be/src/main/java/org/apache/hugegraph/HugeGraphHubble.java
index cfcb698f..5e25ed8c 100644
---
a/hugegraph-hubble/hubble-be/src/main/java/org/apache/hugegraph/HugeGraphHubble.java
+++
b/hugegraph-hubble/hubble-be/src/main/java/org/apache/hugegraph/HugeGraphHubble.java
@@ -48,8 +48,7 @@ public class HugeGraphHubble extends
SpringBootServletInitializer {
}
@Override
- protected SpringApplicationBuilder configure(
- SpringApplicationBuilder builder) {
+ protected SpringApplicationBuilder configure(SpringApplicationBuilder
builder) {
return builder.sources(this.getClass());
}
}
diff --git
a/hugegraph-hubble/hubble-be/src/main/java/org/apache/hugegraph/exception/GenericException.java
b/hugegraph-hubble/hubble-be/src/main/java/org/apache/hugegraph/exception/GenericException.java
new file mode 100644
index 00000000..36c23b8e
--- /dev/null
+++
b/hugegraph-hubble/hubble-be/src/main/java/org/apache/hugegraph/exception/GenericException.java
@@ -0,0 +1,34 @@
+/*
+ *
+ * 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.exception;
+
+/**
+ * Used to wrap other unexpected exceptions like Client/ServerException, and
convert them into this.
+ * This is typically done to handle exceptions uniformly in the hubble UI.
+ */
+public class GenericException extends ParameterizedException {
+
+ public GenericException(ServerException e) {
+ super(e.getMessage(), e.getCause());
+ }
+
+ public GenericException(Exception e) {
+ super(e.getMessage(), e.getCause());
+ }
+}
diff --git
a/hugegraph-hubble/hubble-be/src/main/java/org/apache/hugegraph/handler/ExceptionAdvisor.java
b/hugegraph-hubble/hubble-be/src/main/java/org/apache/hugegraph/handler/ExceptionAdvisor.java
index 3d0091a4..5ddefbb1 100644
---
a/hugegraph-hubble/hubble-be/src/main/java/org/apache/hugegraph/handler/ExceptionAdvisor.java
+++
b/hugegraph-hubble/hubble-be/src/main/java/org/apache/hugegraph/handler/ExceptionAdvisor.java
@@ -21,6 +21,7 @@ package org.apache.hugegraph.handler;
import org.apache.hugegraph.common.Constant;
import org.apache.hugegraph.common.Response;
import org.apache.hugegraph.exception.ExternalException;
+import org.apache.hugegraph.exception.GenericException;
import org.apache.hugegraph.exception.IllegalGremlinException;
import org.apache.hugegraph.exception.InternalException;
import org.apache.hugegraph.exception.ParameterizedException;
@@ -63,6 +64,18 @@ public class ExceptionAdvisor {
.build();
}
+ @ExceptionHandler(GenericException.class)
+ @ResponseStatus(HttpStatus.OK)
+ public Response exceptionHandler(GenericException e) {
+ log.error("GenericException:", e);
+ return Response.builder()
+ .status(Constant.STATUS_BAD_REQUEST)
+ .message("Faied to connect the graph server. Please
refer to the " +
+ "Hubble log for details.")
+ .cause(null)
+ .build();
+ }
+
@ExceptionHandler(ParameterizedException.class)
@ResponseStatus(HttpStatus.OK)
public Response exceptionHandler(ParameterizedException e) {
diff --git
a/hugegraph-hubble/hubble-be/src/main/java/org/apache/hugegraph/util/HugeClientUtil.java
b/hugegraph-hubble/hubble-be/src/main/java/org/apache/hugegraph/util/HugeClientUtil.java
index ab555804..e0bb4238 100644
---
a/hugegraph-hubble/hubble-be/src/main/java/org/apache/hugegraph/util/HugeClientUtil.java
+++
b/hugegraph-hubble/hubble-be/src/main/java/org/apache/hugegraph/util/HugeClientUtil.java
@@ -24,6 +24,7 @@ import org.apache.hugegraph.common.Constant;
import org.apache.hugegraph.driver.HugeClient;
import org.apache.hugegraph.entity.GraphConnection;
import org.apache.hugegraph.exception.ExternalException;
+import org.apache.hugegraph.exception.GenericException;
import org.apache.hugegraph.exception.ServerException;
import org.apache.hugegraph.rest.ClientException;
import org.apache.hugegraph.structure.gremlin.Result;
@@ -48,15 +49,12 @@ public final class HugeClientUtil {
String password = connection.getPassword();
int timeout = connection.getTimeout();
String protocol = connection.getProtocol() == null ?
- DEFAULT_PROTOCOL :
- connection.getProtocol();
+ DEFAULT_PROTOCOL : connection.getProtocol();
String trustStoreFile = connection.getTrustStoreFile();
String trustStorePassword = connection.getTrustStorePassword();
String url = UriComponentsBuilder.newInstance()
- .scheme(protocol)
- .host(host).port(port)
- .toUriString();
+
.scheme(protocol).host(host).port(port).toUriString();
if (username == null) {
username = "";
password = "";
@@ -84,15 +82,12 @@ public final class HugeClientUtil {
String message = e.getMessage();
if (Constant.STATUS_UNAUTHORIZED == e.status() ||
(message != null && message.startsWith("Authentication"))) {
- throw new ExternalException(
- "graph-connection.username-or-password.incorrect", e);
+ throw new
ExternalException("graph-connection.username-or-password.incorrect", e);
}
- if (message != null && message.contains("Invalid syntax for " +
- "username and password")) {
- throw new ExternalException(
- "graph-connection.missing-username-password", e);
+ if (message != null && message.contains("Invalid syntax for
username and password")) {
+ throw new
ExternalException("graph-connection.missing-username-password", e);
}
- throw e;
+ throw new GenericException(e);
} catch (ClientException e) {
Throwable cause = e.getCause();
if (cause == null || cause.getMessage() == null) {
@@ -105,10 +100,11 @@ public final class HugeClientUtil {
message.contains("Host name may not be null")) {
throw new ExternalException("service.unknown-host", e, host);
} else if (message.contains("<!doctype html>")) {
- throw new ExternalException("service.suspected-web",
- e, host, port);
+ throw new ExternalException("service.suspected-web", e, host,
port);
}
throw e;
+ } catch (Exception e) {
+ throw new GenericException(e);
}
try {
@@ -116,13 +112,11 @@ public final class HugeClientUtil {
rs.iterator().forEachRemaining(Result::getObject);
} catch (ServerException e) {
if (Constant.STATUS_UNAUTHORIZED == e.status()) {
- throw new ExternalException(
- "graph-connection.username-or-password.incorrect", e);
+ throw new
ExternalException("graph-connection.username-or-password.incorrect", e);
}
String message = e.message();
if (message != null && message.contains("Could not rebind [g]")) {
- throw new ExternalException("graph-connection.graph.unexist",
e,
- graph, host, port);
+ throw new ExternalException("graph-connection.graph.unexist",
e, graph, host, port);
}
if (!isAcceptable(message)) {
throw e;
diff --git a/pom.xml b/pom.xml
index 692a5dfc..2e062a4d 100644
--- a/pom.xml
+++ b/pom.xml
@@ -530,6 +530,14 @@
<goal>clean</goal>
</goals>
</execution>
+ <!-- auto delete .flattened-pom.xml after "install" step,
will influence deploy step now-->
+ <!--execution>
+ <id>remove-flattened-pom</id>
+ <phase>install</phase>
+ <goals>
+ <goal>clean</goal>
+ </goals>
+ </execution-->
</executions>
</plugin>
</plugins>