Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

W-17092311: SuppressedMuleException improvements. #1458

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
*/
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;
import static java.lang.System.lineSeparator;
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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -394,7 +395,7 @@ public static List<Throwable> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
*/
package org.mule.runtime.privileged.exception;

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;
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
Expand All @@ -34,7 +36,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 {

Expand All @@ -50,7 +52,7 @@ public class SuppressedMuleException extends MuleException {
*/
protected SuppressedMuleException(Throwable throwable, MuleException causeToSuppress) {
super(requireNonNull(throwable, "Exception cannot be null"));
this.suppressedException = requireNonNull(causeToSuppress, "Cannot suppress a null cause");
this.suppressedException = requireNonNull((causeToSuppress), "Cannot suppress a null cause");
addSuppressionToMuleExceptionInfo(causeToSuppress);
}

Expand All @@ -62,15 +64,16 @@ 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) {
nestedCause = nestedCause.getCause();
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())));
}
}
}
Expand All @@ -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;
}

Expand All @@ -98,17 +101,17 @@ public MuleException getSuppressedException() {
* 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<? extends MuleException> 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<? extends MuleException> 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;
}
Expand All @@ -118,13 +121,48 @@ public static Throwable suppressIfPresent(Throwable exception, Class<? extends M

@Override
public String getVerboseMessage() {
// SuppressedException is meant to marks as suppressed another exception that is part of the cause tree.
// SuppressedException is meant to mark as suppressed another exception that is part of the cause tree.
return "Suppressed: " + super.getSummaryMessage();
}

@Override
public String getSummaryMessage() {
// SuppressedException is meant to marks as suppressed another exception that is part of the cause tree.
// SuppressedException is meant to mark as suppressed another exception that is part of the cause tree.
return "Suppressed: " + super.getSummaryMessage();
}

public static class SuppressedMuleExceptionInfo extends MuleException {

private final MuleException suppressedException;

public SuppressedMuleExceptionInfo(MuleException suppressedException) {
this.suppressedException = suppressedException;
this.setMessage(getRootMuleException(suppressedException).getMessage());
}

@Override
public MuleExceptionInfo 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();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ public void whenSuppressionIsAddedThenPreviousSuppressionsAreAdded() {
ExceptionWithAdditionalInfo.class);
List<MuleException> 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
Expand All @@ -115,7 +115,7 @@ public void selfCausedExceptionInMainLoopMustBeResolved() {
assertThat(suppressedSelfCausedException, is(instanceOf(SuppressedMuleException.class)));
List<MuleException> suppressions = ((MuleException) suppressedSelfCausedException).getExceptionInfo().getSuppressedCauses();
assertThat(suppressions,
contains(sameInstance(((SuppressedMuleException) suppressedSelfCausedException).getSuppressedException())));
contains(sameInstance(((SuppressedMuleException) suppressedSelfCausedException).obtainSuppressedException())));
}

@Test
Expand All @@ -125,7 +125,7 @@ public void selfCausedExceptionInSecondaryLoopMustBeResolved() {
assertThat(suppressedSelfCausedException, is(instanceOf(SuppressedMuleException.class)));
List<MuleException> suppressions = ((MuleException) suppressedSelfCausedException).getExceptionInfo().getSuppressedCauses();
assertThat(suppressions,
contains(sameInstance(((SuppressedMuleException) suppressedSelfCausedException).getSuppressedException())));
contains(sameInstance(((SuppressedMuleException) suppressedSelfCausedException).obtainSuppressedException())));
}

@Test
Expand Down