You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ignite.apache.org by "isapego (via GitHub)" <gi...@apache.org> on 2023/11/16 10:42:40 UTC

[PR] IGNITE-20871 Fix stack trace in client exceptions [ignite-3]

isapego opened a new pull request, #2845:
URL: https://github.com/apache/ignite-3/pull/2845

   (no comment)


-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] IGNITE-20871 Fix stack trace in client exceptions [ignite-3]

Posted by "isapego (via GitHub)" <gi...@apache.org>.
isapego commented on code in PR #2845:
URL: https://github.com/apache/ignite-3/pull/2845#discussion_r1395672637


##########
modules/client/src/main/java/org/apache/ignite/internal/client/ClientUtils.java:
##########
@@ -17,22 +17,58 @@
 
 package org.apache.ignite.internal.client;
 
-import static org.apache.ignite.internal.util.ExceptionUtils.sneakyThrow;
-
+import java.util.Objects;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ExecutionException;
 import org.apache.ignite.client.ClientOperationType;
 import org.apache.ignite.client.IgniteClientConfiguration;
 import org.apache.ignite.internal.client.proto.ClientOp;
+import org.apache.ignite.internal.lang.IgniteExceptionMapperUtil;
 import org.apache.ignite.internal.logger.IgniteLogger;
 import org.apache.ignite.internal.logger.Loggers;
 import org.apache.ignite.internal.util.ExceptionUtils;
+import org.apache.ignite.lang.IgniteCheckedException;
+import org.apache.ignite.lang.IgniteException;
 import org.apache.ignite.lang.LoggerFactory;
 
 /**
  * Client utilities.
  */
 public class ClientUtils {
+    /**
+     * Wraps an exception in an IgniteException, extracting trace identifier and error code when the specified exception or one of its
+     * causes is an IgniteException itself.
+     *
+     * @param e Internal exception.
+     * @return Public exception.
+     */
+    public static IgniteException ensurePublicException(Throwable e) {
+        Objects.requireNonNull(e);
+
+        e = ExceptionUtils.unwrapCause(e);
+
+        if (e instanceof IgniteException) {
+            IgniteException iex = (IgniteException) e;
+
+            try {
+                return ExceptionUtils.copyExceptionWithCause(e.getClass(), iex.traceId(), iex.code(), e.getMessage(), e);
+            } catch (Exception ex) {
+                throw new RuntimeException("IgniteException-derived class does not have required constructor: "

Review Comment:
   It can both return `null` and throw an exception (see `ExceptionUtils.ExceptionFactory#createCopy`).



##########
modules/client/src/main/java/org/apache/ignite/internal/client/ClientUtils.java:
##########
@@ -17,22 +17,58 @@
 
 package org.apache.ignite.internal.client;
 
-import static org.apache.ignite.internal.util.ExceptionUtils.sneakyThrow;
-
+import java.util.Objects;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ExecutionException;
 import org.apache.ignite.client.ClientOperationType;
 import org.apache.ignite.client.IgniteClientConfiguration;
 import org.apache.ignite.internal.client.proto.ClientOp;
+import org.apache.ignite.internal.lang.IgniteExceptionMapperUtil;
 import org.apache.ignite.internal.logger.IgniteLogger;
 import org.apache.ignite.internal.logger.Loggers;
 import org.apache.ignite.internal.util.ExceptionUtils;
+import org.apache.ignite.lang.IgniteCheckedException;
+import org.apache.ignite.lang.IgniteException;
 import org.apache.ignite.lang.LoggerFactory;
 
 /**
  * Client utilities.
  */
 public class ClientUtils {
+    /**
+     * Wraps an exception in an IgniteException, extracting trace identifier and error code when the specified exception or one of its
+     * causes is an IgniteException itself.
+     *
+     * @param e Internal exception.
+     * @return Public exception.
+     */
+    public static IgniteException ensurePublicException(Throwable e) {
+        Objects.requireNonNull(e);
+
+        e = ExceptionUtils.unwrapCause(e);
+
+        if (e instanceof IgniteException) {
+            IgniteException iex = (IgniteException) e;
+
+            try {
+                return ExceptionUtils.copyExceptionWithCause(e.getClass(), iex.traceId(), iex.code(), e.getMessage(), e);

Review Comment:
   Good point. Fixed.



##########
modules/client/src/main/java/org/apache/ignite/internal/client/ClientUtils.java:
##########
@@ -17,22 +17,58 @@
 
 package org.apache.ignite.internal.client;
 
-import static org.apache.ignite.internal.util.ExceptionUtils.sneakyThrow;
-
+import java.util.Objects;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ExecutionException;
 import org.apache.ignite.client.ClientOperationType;
 import org.apache.ignite.client.IgniteClientConfiguration;
 import org.apache.ignite.internal.client.proto.ClientOp;
+import org.apache.ignite.internal.lang.IgniteExceptionMapperUtil;
 import org.apache.ignite.internal.logger.IgniteLogger;
 import org.apache.ignite.internal.logger.Loggers;
 import org.apache.ignite.internal.util.ExceptionUtils;
+import org.apache.ignite.lang.IgniteCheckedException;
+import org.apache.ignite.lang.IgniteException;
 import org.apache.ignite.lang.LoggerFactory;
 
 /**
  * Client utilities.
  */
 public class ClientUtils {
+    /**
+     * Wraps an exception in an IgniteException, extracting trace identifier and error code when the specified exception or one of its
+     * causes is an IgniteException itself.
+     *
+     * @param e Internal exception.
+     * @return Public exception.
+     */
+    public static IgniteException ensurePublicException(Throwable e) {
+        Objects.requireNonNull(e);
+
+        e = ExceptionUtils.unwrapCause(e);
+
+        if (e instanceof IgniteException) {
+            IgniteException iex = (IgniteException) e;
+
+            try {
+                return ExceptionUtils.copyExceptionWithCause(e.getClass(), iex.traceId(), iex.code(), e.getMessage(), e);
+            } catch (Exception ex) {
+                throw new RuntimeException("IgniteException-derived class does not have required constructor: "
+                        + e.getClass().getName(), ex);
+            }
+        }
+
+        e = IgniteExceptionMapperUtil.mapToPublicException(e);
+
+        if (e instanceof IgniteCheckedException) {
+            IgniteCheckedException iex = (IgniteCheckedException) e;
+
+            return new IgniteException(iex.traceId(), iex.code(), e.getMessage(), e);
+        }
+
+        return (IgniteException) e;

Review Comment:
   Added a unit-test.



-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] IGNITE-20871 Fix stack trace in client exceptions [ignite-3]

Posted by "sk0x50 (via GitHub)" <gi...@apache.org>.
sk0x50 commented on PR #2845:
URL: https://github.com/apache/ignite-3/pull/2845#issuecomment-1819339273

   In general, the proposed patch is OK.


-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] IGNITE-20871 Fix stack trace in client exceptions [ignite-3]

Posted by "sk0x50 (via GitHub)" <gi...@apache.org>.
sk0x50 commented on code in PR #2845:
URL: https://github.com/apache/ignite-3/pull/2845#discussion_r1398787880


##########
modules/client/src/main/java/org/apache/ignite/internal/client/ClientUtils.java:
##########
@@ -17,22 +17,61 @@
 
 package org.apache.ignite.internal.client;
 
-import static org.apache.ignite.internal.util.ExceptionUtils.sneakyThrow;
+import static org.apache.ignite.lang.ErrorGroups.Common.INTERNAL_ERR;
 
+import java.util.Objects;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ExecutionException;
 import org.apache.ignite.client.ClientOperationType;
 import org.apache.ignite.client.IgniteClientConfiguration;
 import org.apache.ignite.internal.client.proto.ClientOp;
+import org.apache.ignite.internal.lang.IgniteExceptionMapperUtil;
 import org.apache.ignite.internal.logger.IgniteLogger;
 import org.apache.ignite.internal.logger.Loggers;
 import org.apache.ignite.internal.util.ExceptionUtils;
+import org.apache.ignite.lang.IgniteCheckedException;
+import org.apache.ignite.lang.IgniteException;
 import org.apache.ignite.lang.LoggerFactory;
 
 /**
  * Client utilities.
  */
 public class ClientUtils {
+    /**
+     * Wraps an exception in an IgniteException, extracting trace identifier and error code when the specified exception or one of its
+     * causes is an IgniteException itself.
+     *
+     * @param e Internal exception.
+     * @return Public exception.
+     */
+    public static IgniteException ensurePublicException(Throwable e) {
+        Objects.requireNonNull(e);
+
+        e = ExceptionUtils.unwrapCause(e);
+
+        if (e instanceof IgniteException) {
+            IgniteException iex = (IgniteException) e;
+
+            IgniteException copy = ExceptionUtils.copyExceptionWithCause(e.getClass(), iex.traceId(), iex.code(), e.getMessage(), e);
+            if (copy != null) {
+                return copy;
+            }
+
+            return new IgniteException(INTERNAL_ERR, "IgniteException-derived class does not have required constructor: "
+                    + e.getClass().getName(), iex);
+        }
+
+        e = IgniteExceptionMapperUtil.mapToPublicException(e);
+
+        if (e instanceof IgniteCheckedException) {
+            IgniteCheckedException iex = (IgniteCheckedException) e;
+
+            return new IgniteException(iex.traceId(), iex.code(), e.getMessage(), e);

Review Comment:
   I think that we need to use the same approach for handling `IgniteException` and `IgniteCheckedException` until the issue is discussed and approved on the dev list.



-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] IGNITE-20871 Fix stack trace in client exceptions [ignite-3]

Posted by "ptupitsyn (via GitHub)" <gi...@apache.org>.
ptupitsyn commented on code in PR #2845:
URL: https://github.com/apache/ignite-3/pull/2845#discussion_r1395878341


##########
modules/client/src/main/java/org/apache/ignite/internal/client/ClientUtils.java:
##########
@@ -17,22 +17,61 @@
 
 package org.apache.ignite.internal.client;
 
-import static org.apache.ignite.internal.util.ExceptionUtils.sneakyThrow;
+import static org.apache.ignite.lang.ErrorGroups.Common.INTERNAL_ERR;
 
+import java.util.Objects;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ExecutionException;
 import org.apache.ignite.client.ClientOperationType;
 import org.apache.ignite.client.IgniteClientConfiguration;
 import org.apache.ignite.internal.client.proto.ClientOp;
+import org.apache.ignite.internal.lang.IgniteExceptionMapperUtil;
 import org.apache.ignite.internal.logger.IgniteLogger;
 import org.apache.ignite.internal.logger.Loggers;
 import org.apache.ignite.internal.util.ExceptionUtils;
+import org.apache.ignite.lang.IgniteCheckedException;
+import org.apache.ignite.lang.IgniteException;
 import org.apache.ignite.lang.LoggerFactory;
 
 /**
  * Client utilities.
  */
 public class ClientUtils {
+    /**
+     * Wraps an exception in an IgniteException, extracting trace identifier and error code when the specified exception or one of its
+     * causes is an IgniteException itself.
+     *
+     * @param e Internal exception.
+     * @return Public exception.
+     */
+    public static IgniteException ensurePublicException(Throwable e) {
+        Objects.requireNonNull(e);
+
+        e = ExceptionUtils.unwrapCause(e);
+
+        if (e instanceof IgniteException) {
+            IgniteException iex = (IgniteException) e;
+
+            IgniteException copy = ExceptionUtils.copyExceptionWithCause(e.getClass(), iex.traceId(), iex.code(), e.getMessage(), e);
+            if (copy != null) {
+                return copy;
+            }
+
+            return new IgniteException(INTERNAL_ERR, "IgniteException-derived class does not have required constructor: "
+                    + e.getClass().getName(), iex);
+        }
+
+        e = IgniteExceptionMapperUtil.mapToPublicException(e);
+
+        if (e instanceof IgniteCheckedException) {
+            IgniteCheckedException iex = (IgniteCheckedException) e;
+
+            return new IgniteException(iex.traceId(), iex.code(), e.getMessage(), e);

Review Comment:
   I think we should use the same logic as above, with `copyExceptionWithCause` - in case we'll have exception classes derived from `IgniteCheckedException` later.



-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] IGNITE-20871 Fix stack trace in client exceptions [ignite-3]

Posted by "isapego (via GitHub)" <gi...@apache.org>.
isapego commented on code in PR #2845:
URL: https://github.com/apache/ignite-3/pull/2845#discussion_r1399236810


##########
modules/client/src/main/java/org/apache/ignite/internal/client/ClientUtils.java:
##########
@@ -17,22 +17,61 @@
 
 package org.apache.ignite.internal.client;
 
-import static org.apache.ignite.internal.util.ExceptionUtils.sneakyThrow;
+import static org.apache.ignite.lang.ErrorGroups.Common.INTERNAL_ERR;
 
+import java.util.Objects;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ExecutionException;
 import org.apache.ignite.client.ClientOperationType;
 import org.apache.ignite.client.IgniteClientConfiguration;
 import org.apache.ignite.internal.client.proto.ClientOp;
+import org.apache.ignite.internal.lang.IgniteExceptionMapperUtil;
 import org.apache.ignite.internal.logger.IgniteLogger;
 import org.apache.ignite.internal.logger.Loggers;
 import org.apache.ignite.internal.util.ExceptionUtils;
+import org.apache.ignite.lang.IgniteCheckedException;
+import org.apache.ignite.lang.IgniteException;
 import org.apache.ignite.lang.LoggerFactory;
 
 /**
  * Client utilities.
  */
 public class ClientUtils {
+    /**
+     * Wraps an exception in an IgniteException, extracting trace identifier and error code when the specified exception or one of its
+     * causes is an IgniteException itself.
+     *
+     * @param e Internal exception.
+     * @return Public exception.
+     */
+    public static IgniteException ensurePublicException(Throwable e) {
+        Objects.requireNonNull(e);
+
+        e = ExceptionUtils.unwrapCause(e);
+
+        if (e instanceof IgniteException) {
+            IgniteException iex = (IgniteException) e;
+
+            IgniteException copy = ExceptionUtils.copyExceptionWithCause(e.getClass(), iex.traceId(), iex.code(), e.getMessage(), e);
+            if (copy != null) {
+                return copy;
+            }
+
+            return new IgniteException(INTERNAL_ERR, "IgniteException-derived class does not have required constructor: "
+                    + e.getClass().getName(), iex);
+        }
+
+        e = IgniteExceptionMapperUtil.mapToPublicException(e);
+
+        if (e instanceof IgniteCheckedException) {
+            IgniteCheckedException iex = (IgniteCheckedException) e;
+
+            return new IgniteException(iex.traceId(), iex.code(), e.getMessage(), e);

Review Comment:
   Done. Please, take another look.



-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] IGNITE-20871 Fix stack trace in client exceptions [ignite-3]

Posted by "isapego (via GitHub)" <gi...@apache.org>.
isapego merged PR #2845:
URL: https://github.com/apache/ignite-3/pull/2845


-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] IGNITE-20871 Fix stack trace in client exceptions [ignite-3]

Posted by "sk0x50 (via GitHub)" <gi...@apache.org>.
sk0x50 commented on code in PR #2845:
URL: https://github.com/apache/ignite-3/pull/2845#discussion_r1399392103


##########
modules/client/src/main/java/org/apache/ignite/internal/client/ClientUtils.java:
##########
@@ -17,22 +17,92 @@
 
 package org.apache.ignite.internal.client;
 
-import static org.apache.ignite.internal.util.ExceptionUtils.sneakyThrow;
+import static org.apache.ignite.lang.ErrorGroups.Common.INTERNAL_ERR;
 
+import java.util.Objects;
+import java.util.UUID;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ExecutionException;
 import org.apache.ignite.client.ClientOperationType;
 import org.apache.ignite.client.IgniteClientConfiguration;
 import org.apache.ignite.internal.client.proto.ClientOp;
+import org.apache.ignite.internal.lang.IgniteExceptionMapperUtil;
 import org.apache.ignite.internal.logger.IgniteLogger;
 import org.apache.ignite.internal.logger.Loggers;
 import org.apache.ignite.internal.util.ExceptionUtils;
+import org.apache.ignite.lang.IgniteCheckedException;
+import org.apache.ignite.lang.IgniteException;
 import org.apache.ignite.lang.LoggerFactory;
 
 /**
  * Client utilities.
  */
 public class ClientUtils {
+    /**
+     * Wraps an exception in an IgniteException, extracting trace identifier and error code when the specified exception or one of its
+     * causes is an IgniteException itself.
+     *
+     * @param e Internal exception.
+     * @return Public exception.
+     */
+    public static Throwable ensurePublicException(Throwable e) {
+        Objects.requireNonNull(e);
+
+        e = ExceptionUtils.unwrapCause(e);
+
+        if (e instanceof IgniteException) {
+            return copyExceptionWithCauseIfPossible((IgniteException) e);
+        }
+
+        if (e instanceof IgniteCheckedException) {
+            return copyExceptionWithCauseIfPossible((IgniteCheckedException) e);
+        }
+
+        e = IgniteExceptionMapperUtil.mapToPublicException(e);
+
+        return new IgniteException(INTERNAL_ERR, e.getMessage(), e);
+    }
+
+    /**
+     * Try to copy exception using ExceptionUtils.copyExceptionWithCause and return new exception if it was not possible.
+     *
+     * @param e Exception.
+     * @return Properly copied exception or a new error, if exception can not be copied.
+     */
+    private static Throwable copyExceptionWithCauseIfPossible(IgniteException e) {

Review Comment:
   It can be optimized a bit using the `TraceableException` interface.



-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] IGNITE-20871 Fix stack trace in client exceptions [ignite-3]

Posted by "ptupitsyn (via GitHub)" <gi...@apache.org>.
ptupitsyn commented on code in PR #2845:
URL: https://github.com/apache/ignite-3/pull/2845#discussion_r1395651991


##########
modules/client/src/main/java/org/apache/ignite/internal/client/ClientUtils.java:
##########
@@ -17,22 +17,58 @@
 
 package org.apache.ignite.internal.client;
 
-import static org.apache.ignite.internal.util.ExceptionUtils.sneakyThrow;
-
+import java.util.Objects;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ExecutionException;
 import org.apache.ignite.client.ClientOperationType;
 import org.apache.ignite.client.IgniteClientConfiguration;
 import org.apache.ignite.internal.client.proto.ClientOp;
+import org.apache.ignite.internal.lang.IgniteExceptionMapperUtil;
 import org.apache.ignite.internal.logger.IgniteLogger;
 import org.apache.ignite.internal.logger.Loggers;
 import org.apache.ignite.internal.util.ExceptionUtils;
+import org.apache.ignite.lang.IgniteCheckedException;
+import org.apache.ignite.lang.IgniteException;
 import org.apache.ignite.lang.LoggerFactory;
 
 /**
  * Client utilities.
  */
 public class ClientUtils {
+    /**
+     * Wraps an exception in an IgniteException, extracting trace identifier and error code when the specified exception or one of its
+     * causes is an IgniteException itself.
+     *
+     * @param e Internal exception.
+     * @return Public exception.
+     */
+    public static IgniteException ensurePublicException(Throwable e) {
+        Objects.requireNonNull(e);
+
+        e = ExceptionUtils.unwrapCause(e);
+
+        if (e instanceof IgniteException) {
+            IgniteException iex = (IgniteException) e;
+
+            try {
+                return ExceptionUtils.copyExceptionWithCause(e.getClass(), iex.traceId(), iex.code(), e.getMessage(), e);

Review Comment:
   `copyExceptionWithCause` can return null - what happens in this case?



##########
modules/client/src/main/java/org/apache/ignite/internal/client/ClientUtils.java:
##########
@@ -17,22 +17,58 @@
 
 package org.apache.ignite.internal.client;
 
-import static org.apache.ignite.internal.util.ExceptionUtils.sneakyThrow;
-
+import java.util.Objects;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ExecutionException;
 import org.apache.ignite.client.ClientOperationType;
 import org.apache.ignite.client.IgniteClientConfiguration;
 import org.apache.ignite.internal.client.proto.ClientOp;
+import org.apache.ignite.internal.lang.IgniteExceptionMapperUtil;
 import org.apache.ignite.internal.logger.IgniteLogger;
 import org.apache.ignite.internal.logger.Loggers;
 import org.apache.ignite.internal.util.ExceptionUtils;
+import org.apache.ignite.lang.IgniteCheckedException;
+import org.apache.ignite.lang.IgniteException;
 import org.apache.ignite.lang.LoggerFactory;
 
 /**
  * Client utilities.
  */
 public class ClientUtils {
+    /**
+     * Wraps an exception in an IgniteException, extracting trace identifier and error code when the specified exception or one of its
+     * causes is an IgniteException itself.
+     *
+     * @param e Internal exception.
+     * @return Public exception.
+     */
+    public static IgniteException ensurePublicException(Throwable e) {
+        Objects.requireNonNull(e);
+
+        e = ExceptionUtils.unwrapCause(e);
+
+        if (e instanceof IgniteException) {
+            IgniteException iex = (IgniteException) e;
+
+            try {
+                return ExceptionUtils.copyExceptionWithCause(e.getClass(), iex.traceId(), iex.code(), e.getMessage(), e);
+            } catch (Exception ex) {
+                throw new RuntimeException("IgniteException-derived class does not have required constructor: "

Review Comment:
   `copyExceptionWithCause` returns null when there is no constructor, or am I mistaken?



##########
modules/client/src/main/java/org/apache/ignite/internal/client/ClientUtils.java:
##########
@@ -17,22 +17,58 @@
 
 package org.apache.ignite.internal.client;
 
-import static org.apache.ignite.internal.util.ExceptionUtils.sneakyThrow;
-
+import java.util.Objects;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ExecutionException;
 import org.apache.ignite.client.ClientOperationType;
 import org.apache.ignite.client.IgniteClientConfiguration;
 import org.apache.ignite.internal.client.proto.ClientOp;
+import org.apache.ignite.internal.lang.IgniteExceptionMapperUtil;
 import org.apache.ignite.internal.logger.IgniteLogger;
 import org.apache.ignite.internal.logger.Loggers;
 import org.apache.ignite.internal.util.ExceptionUtils;
+import org.apache.ignite.lang.IgniteCheckedException;
+import org.apache.ignite.lang.IgniteException;
 import org.apache.ignite.lang.LoggerFactory;
 
 /**
  * Client utilities.
  */
 public class ClientUtils {
+    /**
+     * Wraps an exception in an IgniteException, extracting trace identifier and error code when the specified exception or one of its
+     * causes is an IgniteException itself.
+     *
+     * @param e Internal exception.
+     * @return Public exception.
+     */
+    public static IgniteException ensurePublicException(Throwable e) {
+        Objects.requireNonNull(e);
+
+        e = ExceptionUtils.unwrapCause(e);
+
+        if (e instanceof IgniteException) {
+            IgniteException iex = (IgniteException) e;
+
+            try {
+                return ExceptionUtils.copyExceptionWithCause(e.getClass(), iex.traceId(), iex.code(), e.getMessage(), e);
+            } catch (Exception ex) {
+                throw new RuntimeException("IgniteException-derived class does not have required constructor: "
+                        + e.getClass().getName(), ex);
+            }
+        }
+
+        e = IgniteExceptionMapperUtil.mapToPublicException(e);
+
+        if (e instanceof IgniteCheckedException) {
+            IgniteCheckedException iex = (IgniteCheckedException) e;
+
+            return new IgniteException(iex.traceId(), iex.code(), e.getMessage(), e);
+        }
+
+        return (IgniteException) e;

Review Comment:
   `mapToPublicException` potentially returns `e` as is. We should always throw a new exception to ensure correct stack trace. 
   
   By the way, do we have a test for this part of the method?



-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] IGNITE-20871 Fix stack trace in client exceptions [ignite-3]

Posted by "isapego (via GitHub)" <gi...@apache.org>.
isapego commented on code in PR #2845:
URL: https://github.com/apache/ignite-3/pull/2845#discussion_r1395940727


##########
modules/client/src/main/java/org/apache/ignite/internal/client/ClientUtils.java:
##########
@@ -17,22 +17,61 @@
 
 package org.apache.ignite.internal.client;
 
-import static org.apache.ignite.internal.util.ExceptionUtils.sneakyThrow;
+import static org.apache.ignite.lang.ErrorGroups.Common.INTERNAL_ERR;
 
+import java.util.Objects;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ExecutionException;
 import org.apache.ignite.client.ClientOperationType;
 import org.apache.ignite.client.IgniteClientConfiguration;
 import org.apache.ignite.internal.client.proto.ClientOp;
+import org.apache.ignite.internal.lang.IgniteExceptionMapperUtil;
 import org.apache.ignite.internal.logger.IgniteLogger;
 import org.apache.ignite.internal.logger.Loggers;
 import org.apache.ignite.internal.util.ExceptionUtils;
+import org.apache.ignite.lang.IgniteCheckedException;
+import org.apache.ignite.lang.IgniteException;
 import org.apache.ignite.lang.LoggerFactory;
 
 /**
  * Client utilities.
  */
 public class ClientUtils {
+    /**
+     * Wraps an exception in an IgniteException, extracting trace identifier and error code when the specified exception or one of its
+     * causes is an IgniteException itself.
+     *
+     * @param e Internal exception.
+     * @return Public exception.
+     */
+    public static IgniteException ensurePublicException(Throwable e) {
+        Objects.requireNonNull(e);
+
+        e = ExceptionUtils.unwrapCause(e);
+
+        if (e instanceof IgniteException) {
+            IgniteException iex = (IgniteException) e;
+
+            IgniteException copy = ExceptionUtils.copyExceptionWithCause(e.getClass(), iex.traceId(), iex.code(), e.getMessage(), e);
+            if (copy != null) {
+                return copy;
+            }
+
+            return new IgniteException(INTERNAL_ERR, "IgniteException-derived class does not have required constructor: "
+                    + e.getClass().getName(), iex);
+        }
+
+        e = IgniteExceptionMapperUtil.mapToPublicException(e);
+
+        if (e instanceof IgniteCheckedException) {
+            IgniteCheckedException iex = (IgniteCheckedException) e;
+
+            return new IgniteException(iex.traceId(), iex.code(), e.getMessage(), e);

Review Comment:
   I discussed it with @sk0x50 and we both are not sure that this should be done. Correct me if I'm wrong, @sk0x50.



-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org