/*
* Copyright 2012-2015 Ray Holder
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package me.hao0.antares.common.retry;
import javax.annotation.Nonnull;
import javax.annotation.concurrent.Immutable;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* An exception indicating that none of the attempts of the {@link Retryer}
* succeeded. If the last {@link Attempt} resulted in an Exception, it is set as
* the cause of the {@link RetryException}.
*
* @author JB
*/
@Immutable
public final class RetryException extends Exception {
private final int numberOfFailedAttempts;
private final Attempt<?> lastFailedAttempt;
/**
* If the last {@link Attempt} had an Exception, ensure it is available in
* the stack trace.
*
* @param numberOfFailedAttempts times we've tried and failed
* @param lastFailedAttempt what happened the last time we failed
*/
public RetryException(int numberOfFailedAttempts, @Nonnull Attempt<?> lastFailedAttempt) {
this("Retrying failed to complete successfully after " + numberOfFailedAttempts + " attempts.", numberOfFailedAttempts, lastFailedAttempt);
}
/**
* If the last {@link Attempt} had an Exception, ensure it is available in
* the stack trace.
*
* @param message Exception description to be added to the stack trace
* @param numberOfFailedAttempts times we've tried and failed
* @param lastFailedAttempt what happened the last time we failed
*/
public RetryException(String message, int numberOfFailedAttempts, Attempt<?> lastFailedAttempt) {
super(message, checkNotNull(lastFailedAttempt, "Last attempt was null").hasException() ? lastFailedAttempt.getExceptionCause() : null);
this.numberOfFailedAttempts = numberOfFailedAttempts;
this.lastFailedAttempt = lastFailedAttempt;
}
/**
* Returns the number of failed attempts
*
* @return the number of failed attempts
*/
public int getNumberOfFailedAttempts() {
return numberOfFailedAttempts;
}
/**
* Returns the last failed attempt
*
* @return the last failed attempt
*/
public Attempt<?> getLastFailedAttempt() {
return lastFailedAttempt;
}
}