Improve exception stack traces in OptimizedMessageNotifier.

This commit is contained in:
Greyson Parrelli
2020-07-21 15:10:23 -04:00
parent 813c820227
commit 0e4a19c368
2 changed files with 37 additions and 4 deletions

View File

@@ -627,4 +627,25 @@ public class Util {
return false;
}
}
/**
* Appends the stack trace of the provided throwable onto the provided primary exception. This is
* useful for when exceptions are thrown inside of asynchronous systems (like runnables in an
* executor) where you'd otherwise lose important parts of the stack trace. This lets you save a
* throwable at the entry point, and then combine it with any caught exceptions later.
*
* @return The provided primary exception, for convenience.
*/
public static RuntimeException appendStackTrace(@NonNull RuntimeException primary, @NonNull Throwable secondary) {
StackTraceElement[] now = primary.getStackTrace();
StackTraceElement[] then = secondary.getStackTrace();
StackTraceElement[] combined = new StackTraceElement[now.length + then.length];
System.arraycopy(now, 0, combined, 0, now.length);
System.arraycopy(then, 0, combined, now.length, then.length);
primary.setStackTrace(combined);
return primary;
}
}