You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sis.apache.org by de...@apache.org on 2022/09/28 15:39:02 UTC
[sis] 01/04: When unwrapping an exception, unwrap also the suppressed exceptions.
This is an automated email from the ASF dual-hosted git repository.
desruisseaux pushed a commit to branch geoapi-4.0
in repository https://gitbox.apache.org/repos/asf/sis.git
commit 68b8c2981f634843ce854c1f6bacd1cbbbc6a494
Author: Martin Desruisseaux <ma...@geomatys.com>
AuthorDate: Tue Sep 27 22:13:35 2022 +0200
When unwrapping an exception, unwrap also the suppressed exceptions.
---
.../main/java/org/apache/sis/util/Exceptions.java | 24 ++++++++++++++--------
.../sis/util/collection/BackingStoreException.java | 3 ++-
2 files changed, 18 insertions(+), 9 deletions(-)
diff --git a/core/sis-utility/src/main/java/org/apache/sis/util/Exceptions.java b/core/sis-utility/src/main/java/org/apache/sis/util/Exceptions.java
index e38fde2cef..e9a223dba6 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/util/Exceptions.java
+++ b/core/sis-utility/src/main/java/org/apache/sis/util/Exceptions.java
@@ -202,8 +202,8 @@ public final class Exceptions extends Static {
* because it is used in very specific contexts. Furthermore classes related to security manager are deprecated
* since Java 17.</div>
*
- * This method uses only the exception class and the absence of {@linkplain Exception#getSuppressed() suppressed
- * exceptions} as criterion; it does not verify if the exception messages are the same.
+ * This method uses only the exception class as criterion;
+ * it does not verify if the exception messages are the same.
*
* @param exception the exception to unwrap (may be {@code null}.
* @return the unwrapped exception (may be the given argument itself).
@@ -212,24 +212,32 @@ public final class Exceptions extends Static {
*/
public static Exception unwrap(Exception exception) {
if (exception != null) {
- while (exception.getSuppressed().length == 0 &&
- (exception instanceof InvocationTargetException ||
+ while (exception instanceof InvocationTargetException ||
exception instanceof ExecutionException ||
exception instanceof BackingStoreException ||
exception instanceof UncheckedIOException ||
- exception instanceof DirectoryIteratorException))
+ exception instanceof DirectoryIteratorException)
{
final Throwable cause = exception.getCause();
if (!(cause instanceof Exception)) break;
+ copySuppressed(exception, cause);
exception = (Exception) cause;
}
Throwable cause;
- while (exception.getSuppressed().length == 0 &&
- exception.getClass().isInstance(cause = exception.getCause()))
- {
+ while (exception.getClass().isInstance(cause = exception.getCause())) {
+ copySuppressed(exception, cause);
exception = (Exception) cause; // Should never fail because of isInstance(…) check.
}
}
return exception;
}
+
+ /**
+ * Unwraps and copies suppressed exceptions from the given source to the given target.
+ */
+ private static void copySuppressed(final Exception source, final Throwable target) {
+ for (final Throwable suppressed : source.getSuppressed()) {
+ target.addSuppressed(suppressed instanceof Exception ? unwrap((Exception) suppressed) : suppressed);
+ }
+ }
}
diff --git a/core/sis-utility/src/main/java/org/apache/sis/util/collection/BackingStoreException.java b/core/sis-utility/src/main/java/org/apache/sis/util/collection/BackingStoreException.java
index 7ecefbbb90..a57b3d3472 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/util/collection/BackingStoreException.java
+++ b/core/sis-utility/src/main/java/org/apache/sis/util/collection/BackingStoreException.java
@@ -18,6 +18,7 @@ package org.apache.sis.util.collection;
import java.io.IOException;
import java.sql.SQLException;
+import org.apache.sis.util.Exceptions;
/**
@@ -153,7 +154,7 @@ public class BackingStoreException extends RuntimeException {
*/
private void copySuppressed(final Throwable cause) {
for (final Throwable s : getSuppressed()) {
- cause.addSuppressed(s);
+ cause.addSuppressed(s instanceof Exception ? Exceptions.unwrap((Exception) s) : s);
}
}
}