xtern commented on code in PR #2613:
URL: https://github.com/apache/ignite-3/pull/2613#discussion_r1355846290


##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/lang/SqlExceptionMapperUtil.java:
##########
@@ -0,0 +1,70 @@
+/*
+ * 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.ignite.internal.lang;
+
+import static 
org.apache.ignite.internal.lang.IgniteExceptionMapperUtil.mapToPublicException;
+import static org.apache.ignite.lang.ErrorGroups.Common.INTERNAL_ERR;
+
+import org.apache.ignite.lang.ErrorGroups.Common;
+import org.apache.ignite.lang.IgniteCheckedException;
+import org.apache.ignite.lang.IgniteException;
+import org.apache.ignite.lang.TraceableException;
+import org.apache.ignite.sql.SqlException;
+
+/**
+ * This utility class provides an ability to map Ignite internal exceptions to 
public SqlException.
+ */
+public class SqlExceptionMapperUtil {
+
+    /**
+     * This method provides a mapping from internal exception to SQL public 
ones.
+     *
+     * <p>The rules of mapping are the following:</p>
+     * <ul>
+     *     <li>any instance of {@link Error} is returned as is, except {@link 
AssertionError}
+     *     that will always be mapped to {@link IgniteException} with the 
{@link Common#INTERNAL_ERR} error code.</li>
+     *     <li>any instance of {@link IgniteException} or {@link 
IgniteCheckedException} is returned as is.</li>
+     *     <li>any other instance of {@link TraceableException} is wrapped 
into {@link SqlException}
+     *         with the original {@link TraceableException#traceId() traceUd} 
and {@link TraceableException#code() code}.</li>
+     *     <li>if there are no any mappers that can do a mapping from the 
given error to a public exception,
+     *     then {@link SqlException} with the {@link Common#INTERNAL_ERR} 
error code is returned.</li>
+     * </ul>
+     *
+     * @param origin Exception to be mapped.
+     * @return Public exception.
+     */
+    public static Throwable mapToPublicSqlException(Throwable origin) {
+        Throwable e = mapToPublicException(origin);
+        if (e instanceof Error) {
+            return e;
+        }
+        if (e instanceof SqlException) {
+            return e;
+        }
+        if (e instanceof IgniteException && ((IgniteException) e).code() == 
INTERNAL_ERR) {
+            return new SqlException(((IgniteException) e).traceId(), 
INTERNAL_ERR, e.getCause());

Review Comment:
   :thinking:  in this case, the original stacktrace is lost and I'm not sure 
that this original exception was  created by `mapToPublicException` call
   
   p.s. as I understand the original error message is also lost



##########
modules/core/src/main/java/org/apache/ignite/internal/lang/IgniteExceptionMapperUtil.java:
##########
@@ -87,32 +87,40 @@ public static Throwable mapToPublicException(Throwable 
origin) {
             if (origin instanceof AssertionError) {
                 return new IgniteException(INTERNAL_ERR, origin);
             }
-
             return origin;
         }
 
-        IgniteExceptionMapper<? extends Exception, ? extends Exception> m = 
EXCEPTION_CONVERTERS.get(origin.getClass());
+        Throwable res;
+
+        // Try to find appropriate mapper, moving from original class to 
supper-classes step by step.
+        Class exceptionClass = origin.getClass();
+        IgniteExceptionMapper<? extends Exception, ? extends Exception> m;
+        while ((m = EXCEPTION_CONVERTERS.get(exceptionClass)) == null && 
exceptionClass != Throwable.class) {
+            exceptionClass = exceptionClass.getSuperclass();
+        }
+
         if (m != null) {
-            Exception mapped = map(m, origin);
+            res = map(m, origin);
 
-            assert mapped instanceof IgniteException || mapped instanceof 
IgniteCheckedException :
-                    "Unexpected mapping of internal exception to a public one 
[origin=" + origin + ", mapped=" + mapped + ']';
+            assert res instanceof IgniteException || res instanceof 
IgniteCheckedException :
+                    "Unexpected mapping of internal exception to a public one 
[origin=" + origin + ", mapped=" + res + ']';
 
-            return mapped;
+        } else {
+            res = origin;
         }
 
-        if (origin instanceof IgniteException || origin instanceof 
IgniteCheckedException) {
-
-            return origin;
+        if (res instanceof IgniteException || res instanceof 
IgniteCheckedException) {
+            return res;
         }
 
         // There are no exception mappings for the given exception. This case 
should be considered as internal error.
         return new IgniteException(INTERNAL_ERR, origin);
+

Review Comment:
   ```suggestion
   ```



##########
modules/core/src/test/java/org/apache/ignite/internal/lang/IgniteExceptionMapperUtilTest.java:
##########
@@ -203,5 +203,12 @@ public static class CustomNoMappingException extends 
IgniteInternalException {
         public CustomNoMappingException() {
             super(INTERNAL_ERR, "Test internal exception [err=no mapping]");
         }
+
+        /**
+         * Creates a new instance of CustomNoMappingException with given code.
+         */
+        public CustomNoMappingException(int code) {

Review Comment:
   it seems this constructor is no longer used



##########
modules/system-view/src/test/java/org/apache/ignite/internal/lang/SqlExceptionMapperUtilTest.java:
##########
@@ -0,0 +1,85 @@
+/*
+ * 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.ignite.internal.lang;
+
+import static 
org.apache.ignite.internal.lang.SqlExceptionMapperUtil.mapToPublicSqlException;
+import static org.apache.ignite.lang.ErrorGroups.Common.INTERNAL_ERR;
+import static org.apache.ignite.lang.ErrorGroups.Sql.EXECUTION_CANCELLED_ERR;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.instanceOf;
+import static org.hamcrest.Matchers.is;
+import static org.junit.jupiter.api.Assertions.assertSame;
+
+import org.apache.ignite.sql.NoRowSetExpectedException;
+import org.apache.ignite.sql.SqlException;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests mapping internal exceptions to public SqlException.
+ */
+class SqlExceptionMapperUtilTest {
+    /**
+     * Tests a default mapping of internal exceptions passed from the sql 
engine.
+     */
+    @Test
+    public void testSqlInternalExceptionDefaultMapping() {
+        CustomNoMappingException internalSqlErr = new 
CustomNoMappingException(EXECUTION_CANCELLED_ERR);
+        Throwable mappedErr = mapToPublicSqlException(internalSqlErr);
+
+        assertThat(mappedErr, instanceOf(SqlException.class));
+
+        SqlException mappedSqlErr = (SqlException) mappedErr;
+
+        assertThat("Mapped exception should have the same trace identifier.", 
mappedSqlErr.traceId(), is(internalSqlErr.traceId()));
+        assertThat("Mapped exception shouldn't have the same error code.", 
mappedSqlErr.code(), is(INTERNAL_ERR));
+    }
+
+    /**
+     * Tests a default mapping of internal exceptions passed from the sql 
engine.
+     */
+    @Test
+    public void testSqlInternalExceptionDefaultMappingForSqlException() {
+        NoRowSetExpectedException sqlErr = new NoRowSetExpectedException();
+
+        Throwable mappedErr = mapToPublicSqlException(sqlErr);
+
+        assertSame(sqlErr, mappedErr);
+    }
+
+    /**
+     * Test exception.
+     */
+    public static class CustomNoMappingException extends 
IgniteInternalException {
+        /** Serial version UID. */
+        private static final long serialVersionUID = 0L;
+
+        /**
+         * Creates a new instance of CustomNoMappingException.
+         */
+        public CustomNoMappingException() {

Review Comment:
   this constructor is not used



-- 
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]

Reply via email to