From bce92bac96ea15f206dd139158b03ff78b7f146d Mon Sep 17 00:00:00 2001 From: Ivan Fritzler Date: Tue, 29 Oct 2024 15:49:14 -0300 Subject: [PATCH 1/3] W-17092311: SuppressedMuleException improvements. --- .../api/exception/ExceptionHelper.java | 7 +++-- .../api/exception/MuleExceptionInfo.java | 2 +- .../exception/SuppressedMuleException.java | 1 + .../exception/SuppressedMuleException.java | 28 +++++++++++++++---- .../SuppressedMuleExceptionTestCase.java | 8 +++--- 5 files changed, 33 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/mule/runtime/api/exception/ExceptionHelper.java b/src/main/java/org/mule/runtime/api/exception/ExceptionHelper.java index 2e78db9ad..5c231c73c 100644 --- a/src/main/java/org/mule/runtime/api/exception/ExceptionHelper.java +++ b/src/main/java/org/mule/runtime/api/exception/ExceptionHelper.java @@ -6,6 +6,8 @@ */ package org.mule.runtime.api.exception; +import static org.mule.runtime.api.exception.MuleException.MULE_VERBOSE_EXCEPTIONS; + import static java.lang.Math.min; import static java.lang.String.format; import static java.lang.System.arraycopy; @@ -13,7 +15,6 @@ import static java.lang.Thread.currentThread; import static java.util.Optional.empty; import static java.util.Optional.of; -import static org.mule.runtime.api.exception.MuleException.MULE_VERBOSE_EXCEPTIONS; import org.mule.runtime.api.legacy.exception.ExceptionReader; import org.mule.runtime.api.util.collection.SmallMap; @@ -142,7 +143,7 @@ public static MuleException getRootMuleException(Throwable t) { if (cause instanceof MuleException) { muleExceptionInfo.putAll(((MuleException) cause).getInfo()); if (cause instanceof SuppressedMuleException) { - suppressedMuleException = ((SuppressedMuleException) cause).getSuppressedException(); + suppressedMuleException = ((SuppressedMuleException) cause).obtainSuppressedException(); } else { exception = (MuleException) cause; } @@ -394,7 +395,7 @@ public static List getExceptionsAsList(Throwable t) { MuleException suppressedCause = null; while (cause != null && cause != suppressedCause) { if (cause instanceof SuppressedMuleException) { - suppressedCause = ((SuppressedMuleException) cause).getSuppressedException(); + suppressedCause = ((SuppressedMuleException) cause).obtainSuppressedException(); } else { exceptions.add(cause); } diff --git a/src/main/java/org/mule/runtime/api/exception/MuleExceptionInfo.java b/src/main/java/org/mule/runtime/api/exception/MuleExceptionInfo.java index 811572aef..064950d4a 100644 --- a/src/main/java/org/mule/runtime/api/exception/MuleExceptionInfo.java +++ b/src/main/java/org/mule/runtime/api/exception/MuleExceptionInfo.java @@ -89,7 +89,7 @@ private void writeSuppressedCauses(StringBuilder buffer) { private void writeCause(StringBuilder buffer, MuleException causedByException) { ErrorType causedByErrorType = causedByException.getExceptionInfo().getErrorType(); if (causedByErrorType != null) { - buffer.append(causedByErrorType.toString()).append(": "); + buffer.append(causedByErrorType).append(": "); } buffer.append(causedByException.getMessage()); } diff --git a/src/main/java/org/mule/runtime/internal/exception/SuppressedMuleException.java b/src/main/java/org/mule/runtime/internal/exception/SuppressedMuleException.java index 31bd27cef..65ab38647 100644 --- a/src/main/java/org/mule/runtime/internal/exception/SuppressedMuleException.java +++ b/src/main/java/org/mule/runtime/internal/exception/SuppressedMuleException.java @@ -11,6 +11,7 @@ /** * @see org.mule.runtime.privileged.exception.SuppressedMuleException * @since 1.2.3, 1.3 + * @deprecated Replaced by {@link org.mule.runtime.privileged.exception.SuppressedMuleException}. */ public class SuppressedMuleException extends org.mule.runtime.privileged.exception.SuppressedMuleException { diff --git a/src/main/java/org/mule/runtime/privileged/exception/SuppressedMuleException.java b/src/main/java/org/mule/runtime/privileged/exception/SuppressedMuleException.java index 3fc7fa05d..1a1666754 100644 --- a/src/main/java/org/mule/runtime/privileged/exception/SuppressedMuleException.java +++ b/src/main/java/org/mule/runtime/privileged/exception/SuppressedMuleException.java @@ -7,6 +7,7 @@ package org.mule.runtime.privileged.exception; import org.mule.runtime.api.exception.MuleException; +import org.mule.runtime.api.exception.MuleExceptionInfo; import org.mule.runtime.api.message.Error; import static java.util.Objects.requireNonNull; @@ -34,7 +35,7 @@ * will resolve to an {@link Error} with {@link Error#getErrorType()} returning customErrorType (discarding the underlying * ConnectionException's error type}). * - * @since 1.2.3, 1.3 + * @since 1.2.3, 1.3 // This is not the right version. */ public class SuppressedMuleException extends MuleException { @@ -49,7 +50,8 @@ public class SuppressedMuleException extends MuleException { * @param causeToSuppress The cause that wants to be suppressed. Cannot be null. */ protected SuppressedMuleException(Throwable throwable, MuleException causeToSuppress) { - super(requireNonNull(throwable, "Exception cannot be null")); + // Avoid returning suppressed exception as part of the cause three when possible. + super(requireNonNull(throwable.equals(causeToSuppress) ? throwable.getCause() : throwable, "Exception cannot be null")); this.suppressedException = requireNonNull(causeToSuppress, "Cannot suppress a null cause"); addSuppressionToMuleExceptionInfo(causeToSuppress); } @@ -62,7 +64,7 @@ protected SuppressedMuleException(Throwable throwable, MuleException causeToSupp * @param causeToSuppress Exception that is being suppressed. */ private void addSuppressionToMuleExceptionInfo(MuleException causeToSuppress) { - this.getExceptionInfo().addSuppressedCause(causeToSuppress); + this.getExceptionInfo().addSuppressedCause(new SuppressedMuleExceptionInfo(causeToSuppress)); this.addAllInfo(causeToSuppress.getAdditionalInfo()); Throwable nestedCause = causeToSuppress; while (nestedCause.getCause() != null && nestedCause.getCause() != nestedCause) { @@ -70,7 +72,8 @@ private void addSuppressionToMuleExceptionInfo(MuleException causeToSuppress) { if (nestedCause instanceof MuleException) { this.addAllInfo(((MuleException) nestedCause).getAdditionalInfo()); if (nestedCause instanceof SuppressedMuleException) { - this.getExceptionInfo().addSuppressedCause(((SuppressedMuleException) nestedCause).getSuppressedException()); + this.getExceptionInfo().addSuppressedCause(new SuppressedMuleExceptionInfo((((SuppressedMuleException) nestedCause) + .obtainSuppressedException()))); } } } @@ -89,7 +92,7 @@ public Throwable unwrap() { /** * @return {@link MuleException} that has been suppressed by this {@link SuppressedMuleException}. */ - public MuleException getSuppressedException() { + public MuleException obtainSuppressedException() { return suppressedException; } @@ -127,4 +130,19 @@ public String getSummaryMessage() { // SuppressedException is meant to marks as suppressed another exception that is part of the cause tree. return "Suppressed: " + super.getSummaryMessage(); } + + public static class SuppressedMuleExceptionInfo extends MuleException { + + private final MuleException externalImplementation; + + public SuppressedMuleExceptionInfo(MuleException externalImplementation) { + this.externalImplementation = externalImplementation; + } + + @Override + public MuleExceptionInfo getExceptionInfo() { + return externalImplementation.getExceptionInfo(); + } + } + } diff --git a/src/test/java/org/mule/runtime/api/test/internal/exception/SuppressedMuleExceptionTestCase.java b/src/test/java/org/mule/runtime/api/test/internal/exception/SuppressedMuleExceptionTestCase.java index e9bfbf891..39c2afea0 100644 --- a/src/test/java/org/mule/runtime/api/test/internal/exception/SuppressedMuleExceptionTestCase.java +++ b/src/test/java/org/mule/runtime/api/test/internal/exception/SuppressedMuleExceptionTestCase.java @@ -104,8 +104,8 @@ public void whenSuppressionIsAddedThenPreviousSuppressionsAreAdded() { ExceptionWithAdditionalInfo.class); List suppressions = ((MuleException) suppressedAnotherTestException).getExceptionInfo().getSuppressedCauses(); assertThat(suppressions, - contains(sameInstance(((SuppressedMuleException) suppressedAnotherTestException).getSuppressedException()), - sameInstance(((SuppressedMuleException) suppressedTestException).getSuppressedException()))); + contains(sameInstance(((SuppressedMuleException) suppressedAnotherTestException).obtainSuppressedException()), + sameInstance(((SuppressedMuleException) suppressedTestException).obtainSuppressedException()))); } @Test @@ -115,7 +115,7 @@ public void selfCausedExceptionInMainLoopMustBeResolved() { assertThat(suppressedSelfCausedException, is(instanceOf(SuppressedMuleException.class))); List suppressions = ((MuleException) suppressedSelfCausedException).getExceptionInfo().getSuppressedCauses(); assertThat(suppressions, - contains(sameInstance(((SuppressedMuleException) suppressedSelfCausedException).getSuppressedException()))); + contains(sameInstance(((SuppressedMuleException) suppressedSelfCausedException).obtainSuppressedException()))); } @Test @@ -125,7 +125,7 @@ public void selfCausedExceptionInSecondaryLoopMustBeResolved() { assertThat(suppressedSelfCausedException, is(instanceOf(SuppressedMuleException.class))); List suppressions = ((MuleException) suppressedSelfCausedException).getExceptionInfo().getSuppressedCauses(); assertThat(suppressions, - contains(sameInstance(((SuppressedMuleException) suppressedSelfCausedException).getSuppressedException()))); + contains(sameInstance(((SuppressedMuleException) suppressedSelfCausedException).obtainSuppressedException()))); } @Test From 7627fe6303289d940d32e8ed15189d61ec0af19b Mon Sep 17 00:00:00 2001 From: Ivan Fritzler Date: Thu, 31 Oct 2024 09:23:38 -0300 Subject: [PATCH 2/3] Self review --- .../exception/SuppressedMuleException.java | 33 +++++++++++++++---- 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/mule/runtime/privileged/exception/SuppressedMuleException.java b/src/main/java/org/mule/runtime/privileged/exception/SuppressedMuleException.java index 1a1666754..2a192ad65 100644 --- a/src/main/java/org/mule/runtime/privileged/exception/SuppressedMuleException.java +++ b/src/main/java/org/mule/runtime/privileged/exception/SuppressedMuleException.java @@ -6,12 +6,13 @@ */ package org.mule.runtime.privileged.exception; +import static java.util.Objects.requireNonNull; +import static org.mule.runtime.api.exception.ExceptionHelper.getRootMuleException; + import org.mule.runtime.api.exception.MuleException; import org.mule.runtime.api.exception.MuleExceptionInfo; import org.mule.runtime.api.message.Error; -import static java.util.Objects.requireNonNull; - /** * Wraps a provided exception, suppressing a {@link MuleException} that is part of it's cause tree, meaning that the Mule Runtime * will not take it into account for the error handling. The suppressed cause and all its nested {@link Exception#getCause()} will @@ -133,15 +134,35 @@ public String getSummaryMessage() { public static class SuppressedMuleExceptionInfo extends MuleException { - private final MuleException externalImplementation; + private final MuleException suppressedException; - public SuppressedMuleExceptionInfo(MuleException externalImplementation) { - this.externalImplementation = externalImplementation; + public SuppressedMuleExceptionInfo(MuleException suppressedException) { + this.suppressedException = suppressedException; + this.setMessage(getRootMuleException(suppressedException).getMessage()); } @Override public MuleExceptionInfo getExceptionInfo() { - return externalImplementation.getExceptionInfo(); + return suppressedException.getExceptionInfo(); + } + + public MuleException obtainSuppressedException() { + return suppressedException; + } + + @Override + public String getSummaryMessage() { + return suppressedException.getSummaryMessage(); + } + + @Override + public String getVerboseMessage() { + return suppressedException.getVerboseMessage(); + } + + @Override + public String getDetailedMessage() { + return suppressedException.getDetailedMessage(); } } From c7d81574338143dd2eb990847132cbc2dd57e821 Mon Sep 17 00:00:00 2001 From: Ivan Fritzler Date: Fri, 1 Nov 2024 10:02:10 -0300 Subject: [PATCH 3/3] Self review --- .../exception/SuppressedMuleException.java | 27 +++++++++---------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/mule/runtime/privileged/exception/SuppressedMuleException.java b/src/main/java/org/mule/runtime/privileged/exception/SuppressedMuleException.java index 2a192ad65..2e56abaa5 100644 --- a/src/main/java/org/mule/runtime/privileged/exception/SuppressedMuleException.java +++ b/src/main/java/org/mule/runtime/privileged/exception/SuppressedMuleException.java @@ -6,8 +6,8 @@ */ package org.mule.runtime.privileged.exception; -import static java.util.Objects.requireNonNull; import static org.mule.runtime.api.exception.ExceptionHelper.getRootMuleException; +import static java.util.Objects.requireNonNull; import org.mule.runtime.api.exception.MuleException; import org.mule.runtime.api.exception.MuleExceptionInfo; @@ -51,9 +51,8 @@ public class SuppressedMuleException extends MuleException { * @param causeToSuppress The cause that wants to be suppressed. Cannot be null. */ protected SuppressedMuleException(Throwable throwable, MuleException causeToSuppress) { - // Avoid returning suppressed exception as part of the cause three when possible. - super(requireNonNull(throwable.equals(causeToSuppress) ? throwable.getCause() : throwable, "Exception cannot be null")); - this.suppressedException = requireNonNull(causeToSuppress, "Cannot suppress a null cause"); + super(requireNonNull(throwable, "Exception cannot be null")); + this.suppressedException = requireNonNull((causeToSuppress), "Cannot suppress a null cause"); addSuppressionToMuleExceptionInfo(causeToSuppress); } @@ -102,17 +101,17 @@ public MuleException obtainSuppressedException() { * The search will stop if a {@link SuppressedMuleException} is found, making no suppression. * * @param exception Exception that will be wrapped, suppressing the exception itself or one of it's causes. - * @param causeToSuppress Class of the {@link MuleException} that has to be suppressed. + * @param classToSuppress Class of the {@link MuleException} that has to be suppressed. * @return {@link SuppressedMuleException} or provided exception if no cause to suppress is found. */ - public static Throwable suppressIfPresent(Throwable exception, Class causeToSuppress) { - Throwable cause = exception; - while (cause != null && !(cause instanceof SuppressedMuleException)) { - if (causeToSuppress.isInstance(cause)) { - return new SuppressedMuleException(exception, (MuleException) cause); + public static Throwable suppressIfPresent(Throwable exception, Class classToSuppress) { + Throwable exceptionCause = exception; + while (exceptionCause != null && !(exceptionCause instanceof SuppressedMuleException)) { + if (classToSuppress.isInstance(exceptionCause)) { + return new SuppressedMuleException(exception, (MuleException) exceptionCause); } - if (cause.getCause() != cause) { - cause = cause.getCause(); + if (exceptionCause.getCause() != exceptionCause) { + exceptionCause = exceptionCause.getCause(); } else { break; } @@ -122,13 +121,13 @@ public static Throwable suppressIfPresent(Throwable exception, Class