Skip to content

Commit

Permalink
Merge pull request #51 from dwnusbaum/JENKINS-60354
Browse files Browse the repository at this point in the history
[JENKINS-60354] Add flag to FlowInterruptedException to indicate non-interruptions
  • Loading branch information
dwnusbaum authored Jan 3, 2020
2 parents 2afa8d8 + 5cb7d36 commit 61bfada
Showing 1 changed file with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@

/**
* Special exception that can be thrown out of {@link StepContext#onFailure} to indicate that the flow was aborted from the inside.
* (This could be caught like any other exception and rethrown or ignored. It only takes effect if thrown all the way up.)
* (This could be caught like any other exception and rethrown or ignored. It only takes effect if thrown all the way up.
* Consumers, such as steps, may find {@link #isActualInterruption} useful in deciding whether to ignore or rethrow the exception.)
* <p>No stack trace is printed (except by {@link #getCause} and/or {@link #getSuppressed} if present),
* and you can control the {@link Result} and {@link CauseOfInterruption}.
* <p>Analogous to {@link Executor#interrupt(Result, CauseOfInterruption...)} but does not assume we are running inside an executor thread.
Expand All @@ -53,8 +54,18 @@
*/
public final class FlowInterruptedException extends InterruptedException {

private static final long serialVersionUID = 630482382622970136L;

private final @Nonnull Result result;
private final @Nonnull List<CauseOfInterruption> causes;
/**
* If true, this exception represents an actual build interruption, rather than a general error with a result and
* no stack trace.
* Used by steps like {@code RetryStep} to decide whether to handle or rethrow a {@link FlowInterruptedException}.
* Non-null, except momentarily during deserialization before {@link #readResolve} sets the field to {@code true}
* for old instances serialized before this field was added.
*/
private Boolean actualInterruption = true;

/**
* Creates a new exception.
Expand All @@ -64,6 +75,19 @@ public final class FlowInterruptedException extends InterruptedException {
public FlowInterruptedException(@Nonnull Result result, @Nonnull CauseOfInterruption... causes) {
this.result = result;
this.causes = Arrays.asList(causes);
this.actualInterruption = true;
}

/**
* Creates a new exception.
* @param result the desired result for the flow, typically {@link Result#ABORTED}
* @param causes any indications
* @param actualInterruption true if this is an actual build interruption (e.g. the user wants to abort the build)
*/
public FlowInterruptedException(@Nonnull Result result, boolean actualInterruption, @Nonnull CauseOfInterruption... causes) {
this.result = result;
this.causes = Arrays.asList(causes);
this.actualInterruption = actualInterruption;
}

public @Nonnull Result getResult() {
Expand All @@ -74,6 +98,17 @@ public FlowInterruptedException(@Nonnull Result result, @Nonnull CauseOfInterrup
return causes;
}

public boolean isActualInterruption() {
return actualInterruption;
}

private Object readResolve() {
if (actualInterruption == null) {
actualInterruption = true;
}
return this;
}

/**
* If a build catches this exception, it should use this method to report it.
*/
Expand Down

0 comments on commit 61bfada

Please sign in to comment.