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

[MNG-8066] Default exception handler does not handle recursion #1558

Merged
merged 2 commits into from
Jun 6, 2024
Merged
Changes from 1 commit
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 @@ -22,7 +22,10 @@
import java.net.ConnectException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Set;

import org.apache.maven.lifecycle.LifecycleExecutionException;
import org.apache.maven.model.building.ModelProblem;
Expand Down Expand Up @@ -87,13 +90,13 @@
*/
@Component(role = ExceptionHandler.class)
public class DefaultExceptionHandler implements ExceptionHandler {

@Override
public ExceptionSummary handleException(Throwable exception) {
return handle("", exception);
}

private ExceptionSummary handle(String message, Throwable exception) {
String reference = getReference(exception);
String reference = getReference(Collections.newSetFromMap(new IdentityHashMap<>()), exception);

List<ExceptionSummary> children = null;

Expand Down Expand Up @@ -153,8 +156,11 @@ private ExceptionSummary handle(ModelProblem problem, String projectId) {
}
}

private String getReference(Throwable exception) {
private String getReference(Set<Throwable> dejaVu, Throwable exception) {
String reference = "";
if (!dejaVu.add(exception)) {
return reference;
}

if (exception != null) {
if (exception instanceof MojoExecutionException) {
Expand Down Expand Up @@ -186,14 +192,14 @@ private String getReference(Throwable exception) {
}

if (StringUtils.isEmpty(reference)) {
reference = getReference(cause);
reference = getReference(dejaVu, cause);
}

if (StringUtils.isEmpty(reference)) {
reference = exception.getClass().getSimpleName();
}
} else if (exception instanceof LifecycleExecutionException) {
reference = getReference(exception.getCause());
reference = getReference(dejaVu, exception.getCause());
} else if (isNoteworthyException(exception)) {
reference = exception.getClass().getSimpleName();
}
Expand Down Expand Up @@ -222,7 +228,8 @@ private boolean isNoteworthyException(Throwable exception) {
private String getMessage(String message, Throwable exception) {
String fullMessage = (message != null) ? message : "";

// To break out of possible endless loop when getCause returns "this"
// To break out of possible endless loop when getCause returns "this", or dejaVu for n-level recursion (n>1)
Set<Throwable> dejaVu = Collections.newSetFromMap(new IdentityHashMap<>());
for (Throwable t = exception; t != null && t != t.getCause(); t = t.getCause()) {
String exceptionMessage = t.getMessage();

Expand All @@ -246,6 +253,11 @@ private String getMessage(String message, Throwable exception) {
} else if (!fullMessage.contains(exceptionMessage)) {
fullMessage = join(fullMessage, exceptionMessage);
}

if (!dejaVu.add(t)) {
fullMessage = join(fullMessage, "[CIRCULAR REFERENCE]");
break;
}
}

return fullMessage.trim();
Expand Down