Skip to content

Commit

Permalink
Improve exception stack traces in OptimizedMessageNotifier.
Browse files Browse the repository at this point in the history
  • Loading branch information
greyson-signal committed Jul 21, 2020
1 parent 813c820 commit 0e4a19c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.util.LeakyBucketLimiter;
import org.thoughtcrime.securesms.util.Util;
import org.thoughtcrime.securesms.util.concurrent.SignalExecutors;

/**
Expand Down Expand Up @@ -51,26 +52,37 @@ public void cancelDelayedNotifications() {

@Override
public void updateNotification(@NonNull Context context) {
limiter.run(() -> wrapped.updateNotification(context));
runOnLimiter(() -> wrapped.updateNotification(context));
}

@Override
public void updateNotification(@NonNull Context context, long threadId) {
limiter.run(() -> wrapped.updateNotification(context, threadId));
runOnLimiter(() -> wrapped.updateNotification(context, threadId));
}

@Override
public void updateNotification(@NonNull Context context, long threadId, boolean signal) {
limiter.run(() -> wrapped.updateNotification(context, threadId, signal));
runOnLimiter(() -> wrapped.updateNotification(context, threadId, signal));
}

@Override
public void updateNotification(@NonNull Context context, long threadId, boolean signal, int reminderCount) {
limiter.run(() -> wrapped.updateNotification(context, threadId, signal, reminderCount));
runOnLimiter(() -> wrapped.updateNotification(context, threadId, signal, reminderCount));
}

@Override
public void clearReminder(@NonNull Context context) {
wrapped.clearReminder(context);
}

private void runOnLimiter(@NonNull Runnable runnable) {
Throwable prettyException = new Throwable();
limiter.run(() -> {
try {
runnable.run();
} catch (RuntimeException e) {
throw Util.appendStackTrace(e, prettyException);
}
});
}
}
21 changes: 21 additions & 0 deletions app/src/main/java/org/thoughtcrime/securesms/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -627,4 +627,25 @@ public static boolean isLong(String value) {
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;
}
}

0 comments on commit 0e4a19c

Please sign in to comment.