Skip to content
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
@@ -1,7 +1,7 @@
package org.datatransferproject.datatransfer.google.common;

import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;
import org.datatransferproject.types.common.ExceptionUtils;
import java.io.IOException;
import java.util.UUID;
import org.datatransferproject.spi.cloud.storage.JobStore;
Expand Down Expand Up @@ -30,7 +30,7 @@ public static ErrorDetail createErrorDetail(String idempotentId, String title, E
return ErrorDetail.builder()
.setId(idempotentId)
.setTitle(title)
.setException(Throwables.getStackTraceAsString(e))
.setException(ExceptionUtils.getStackTraceAsString(e))
.setCanSkip(canSkip).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package org.datatransferproject.launcher.monitor;

import static com.google.common.base.Strings.isNullOrEmpty;
import static com.google.common.base.Throwables.getStackTraceAsString;
import static org.datatransferproject.types.common.ExceptionUtils.getStackTraceAsString;

import java.util.UUID;
import org.datatransferproject.api.launcher.Monitor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import static java.lang.String.format;

import com.google.common.base.Joiner;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;
import org.datatransferproject.types.common.ExceptionUtils;
import java.util.UUID;
import org.datatransferproject.api.launcher.Monitor;
import org.datatransferproject.types.transfer.errors.ErrorDetail;
Expand Down Expand Up @@ -81,7 +81,7 @@ public <T extends Serializable> T executeOrThrowException(
ErrorDetail.builder()
.setId(idempotentId)
.setTitle(itemName)
.setException(Throwables.getStackTraceAsString(e))
.setException(ExceptionUtils.getStackTraceAsString(e))
.build();
errors.put(idempotentId, errorDetail);
recentErrors.put(idempotentId, errorDetail);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import static java.lang.String.format;

import com.google.common.base.Joiner;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;
import org.datatransferproject.types.common.ExceptionUtils;
import java.io.IOException;
import java.io.Serializable;
import java.time.Clock;
Expand Down Expand Up @@ -90,10 +90,10 @@ public <T extends Serializable> T executeOrThrowException(
errors.remove(idempotentId);
return result;
} catch (RetryException e) {
ErrorDetail.Builder errorDetailBuilder = ErrorDetail.builder();
errorDetailBuilder.setId(idempotentId)
ErrorDetail.Builder errorDetailBuilder = ErrorDetail.builder()
.setId(idempotentId)
.setTitle(itemName)
.setException(Throwables.getStackTraceAsString(e));
.setException(ExceptionUtils.getStackTraceAsString(e));
if(e.canSkip()){
ErrorDetail errorDetail = errorDetailBuilder.setCanSkip(true).build();
errors.put(idempotentId, errorDetail);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package org.datatransferproject.types.common;

/** Utility class for handling exceptions. */
public class ExceptionUtils {

/**
* Formats a Throwable's stack trace to collapse consecutive identical frames.
* This handles highly recursive calls (e.g. repeated copyHelper invocations)
* by logging the first occurrence and annotating how many times it was repeated.
*/
public static String getStackTraceAsString(Throwable t) {
if (t == null) {
return "";
}
StringBuilder sb = new StringBuilder();
sb.append(t.toString()).append('\n');
StackTraceElement[] trace = t.getStackTrace();
int duplicateCount = 0;
StackTraceElement lastElement = null;

for (StackTraceElement element : trace) {
if (lastElement != null && element.equals(lastElement)) {
duplicateCount++;
} else {
if (duplicateCount > 0) {
sb.append("\t... repeated ").append(duplicateCount).append(" times more\n");
duplicateCount = 0;
}
sb.append("\tat ").append(element.toString()).append('\n');
lastElement = element;
}
}
if (duplicateCount > 0) {
sb.append("\t... repeated ").append(duplicateCount).append(" times more\n");
}

// Now handle Caused by recursively
Throwable cause = t.getCause();
if (cause != null) {
appendCause(cause, trace, sb);
}

return sb.toString();
}

private static void appendCause(Throwable cause, StackTraceElement[] enclosingTrace, StringBuilder sb) {
sb.append("Caused by: ").append(cause.toString()).append('\n');
StackTraceElement[] trace = cause.getStackTrace();

// Find common suffix between trace and enclosingTrace
int m = trace.length - 1;
int n = enclosingTrace.length - 1;
while (m >= 0 && n >= 0 && trace[m].equals(enclosingTrace[n])) {
m--;
n--;
}
int framesInCommon = trace.length - 1 - m;

int duplicateCount = 0;
StackTraceElement lastElement = null;

for (int i = 0; i <= m; i++) {
StackTraceElement element = trace[i];
if (lastElement != null && element.equals(lastElement)) {
duplicateCount++;
} else {
if (duplicateCount > 0) {
sb.append("\t... repeated ").append(duplicateCount).append(" times more\n");
duplicateCount = 0;
}
sb.append("\tat ").append(element.toString()).append('\n');
lastElement = element;
}
}
if (duplicateCount > 0) {
sb.append("\t... repeated ").append(duplicateCount).append(" times more\n");
}
if (framesInCommon != 0) {
sb.append("\t... ").append(framesInCommon).append(" more\n");
}

Throwable nextCause = cause.getCause();
if (nextCause != null) {
appendCause(nextCause, trace, sb);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package org.datatransferproject.types.transfer.retry;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Throwables.getStackTraceAsString;
import static org.datatransferproject.types.common.ExceptionUtils.getStackTraceAsString;

import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Arrays;
Expand Down
Loading