Skip to content

Instantly share code, notes, and snippets.

@Romeh
Created December 3, 2017 17:44
Show Gist options
  • Save Romeh/5407bfabdd63f1528edcb921e8739d12 to your computer and use it in GitHub Desktop.
Save Romeh/5407bfabdd63f1528edcb921e8739d12 to your computer and use it in GitHub Desktop.
/**
* An exception thrown to signal that a retry operation (executed via {@link RetryRule}) has retried more than the
* allowed number of times, and has still failed.
*/
public final class RetryException extends RuntimeException {
private RetryException(@NotNull String message) {
super(message);
}
/**
* @param errors the errors for each attempt at running this test-case
*/
@NotNull
public static RetryException from(@NotNull Throwable[] errors) {
final StringBuilder msg = new StringBuilder("Invoked methods still failed after " + errors.length + " attempts.");
for (int i = 0; i < errors.length; i++) {
final Throwable error = errors[i];
msg.append('\n');
msg.append("Attempt #").append(i).append(" threw exception:");
msg.append(stackTraceAsString(error));
}
return new RetryException(msg.toString());
}
@NotNull
private static String stackTraceAsString(@NotNull Throwable t) {
final StringWriter errors = new StringWriter();
t.printStackTrace(new PrintWriter(errors));
return errors.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment