jerqi commented on code in PR #9446:
URL: https://github.com/apache/gravitino/pull/9446#discussion_r2608918601
##########
catalogs/hive-metastore-common/src/main/java/org/apache/gravitino/hive/client/HiveExceptionConverter.java:
##########
@@ -79,6 +94,15 @@ public TargetType type() {
}
}
+ private static final Set<String> noSuchExceptionSet =
Review Comment:
Should u use upper case letter for the name?
##########
catalogs/hive-metastore-common/src/main/java/org/apache/gravitino/hive/client/HiveExceptionConverter.java:
##########
@@ -89,6 +113,131 @@ private HiveExceptionConverter() {}
* @return A Gravitino exception
*/
public static RuntimeException toGravitinoException(Exception e,
ExceptionTarget target) {
- return null;
+ Throwable cause = unwrapException(e);
+ return convertException(cause, target);
+ }
+
+ /**
+ * Unwraps nested exceptions, especially InvocationTargetException from
reflection calls.
+ *
+ * @param e The exception to unwrap
+ * @return The unwrapped exception
+ */
+ private static Throwable unwrapException(Exception e) {
+ Throwable cause = e;
+ if (e instanceof InvocationTargetException) {
+ InvocationTargetException ite = (InvocationTargetException) e;
+ cause = ite.getTargetException();
+ if (cause == null) {
+ cause = e;
+ }
+ }
+ return cause;
+ }
+
+ /**
+ * Converts the exception to the appropriate Gravitino exception based on
its type.
+ *
+ * @param cause The exception cause
+ * @param target The target Hive object of the operation
+ * @return A Gravitino exception
+ */
+ private static RuntimeException convertException(Throwable cause,
ExceptionTarget target) {
+ if (cause instanceof RuntimeException && cause.getCause() instanceof
Exception) {
+ return toGravitinoException((Exception) cause.getCause(), target);
+ }
+
+ String message = cause.getMessage();
+ String lowerMessage = message != null ? message.toLowerCase(Locale.ROOT) :
"";
+ String exceptionClassName = cause.getClass().getName();
+
+ if (exceptionClassName.contains("AlreadyExistsException")) {
+ return toAlreadyExistsException(cause, target, message);
+ }
+
+ if (noSuchExceptionSet.contains(exceptionClassName)) {
+ return toNoSuchObjectException(cause, target, message);
+ }
+
+ if (exceptionClassName.contains("InvalidOperationException")) {
+ if (isNonEmptySchemaMessage(lowerMessage)) {
+ return new NonEmptySchemaException(
+ cause, "Hive schema %s is not empty in Hive Metastore",
target.name());
+ }
+ return new IllegalArgumentException(cause.getMessage(), cause);
+ }
+
+ if (exceptionClassName.contains("MetaException")) {
+ if (lowerMessage.contains("invalid partition key")) {
+ return new NoSuchPartitionException(
+ cause, "Hive partition %s does not exist in Hive Metastore",
target.name());
+ }
+ }
+
+ if (exceptionClassName.contains("TException")) {
+ if (lowerMessage.contains("already exists")) {
+ return toAlreadyExistsException(cause, target, message);
+ }
+ if (lowerMessage.contains("does not exist")
Review Comment:
Could u extract similar set for the code?
--
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]