Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle failures in publish invocation #91

Merged
merged 3 commits into from
Mar 3, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 56 additions & 22 deletions src/main/java/io/jenkins/plugins/checks/steps/WithChecksStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,23 @@ public String getDisplayName() {
}
}

private static class WithChecksPublishException extends Exception {

public static final long serialVersionUID = 1L;

WithChecksPublishException(final Throwable cause) {
super(cause);
}

WithChecksPublishException(final String msg) {
super(msg);
}

WithChecksPublishException(final String msg, final Throwable e) {
super(msg, e);
}
}

/**
* The step's execution to actually inject the {@link ChecksInfo} into the closure.
*/
Expand Down Expand Up @@ -108,15 +125,20 @@ ChecksInfo extractChecksInfo() {

@Override
public void stop(final Throwable cause) {
if (publish(getContext(), new ChecksDetails.ChecksDetailsBuilder()
.withName(step.getName())
.withStatus(ChecksStatus.COMPLETED)
.withConclusion(ChecksConclusion.CANCELED))) {
getContext().onFailure(cause);
try {
publish(getContext(), new ChecksDetails.ChecksDetailsBuilder()
.withName(step.getName())
.withStatus(ChecksStatus.COMPLETED)
.withConclusion(ChecksConclusion.CANCELED));
}
catch (WithChecksPublishException e) {
cause.addSuppressed(e);
}
getContext().onFailure(cause);
}

private boolean publish(final StepContext context, final ChecksDetails.ChecksDetailsBuilder builder) {
@SuppressWarnings("IllegalCatch")
private void publish(final StepContext context, final ChecksDetails.ChecksDetailsBuilder builder) throws WithChecksPublishException {
TaskListener listener = TaskListener.NULL;
try {
listener = fixNull(context.get(TaskListener.class), TaskListener.NULL);
Expand All @@ -133,25 +155,27 @@ private boolean publish(final StepContext context, final ChecksDetails.ChecksDet
run = context.get(Run.class);
}
catch (IOException | InterruptedException e) {
String msg = "Failed getting Run from the context on the start of withChecks step: " + e;
pluginLogger.log(msg.replaceAll("\r\n", ""));
SYSTEM_LOGGER.log(Level.WARNING, msg.replaceAll("\r\n", ""));
context.onFailure(new IllegalStateException(msg));
return false;
String msg = "Failed getting Run from the context on the start of withChecks step";
pluginLogger.log((msg + ": " + e).replaceAll("\r\n", ""));
SYSTEM_LOGGER.log(Level.WARNING, msg, e);
throw new WithChecksPublishException(msg, e);
}

if (run == null) {
String msg = "No Run found in the context.";
pluginLogger.log(msg);
SYSTEM_LOGGER.log(Level.WARNING, msg);
context.onFailure(new IllegalStateException(msg));
return false;
throw new WithChecksPublishException(msg);
}

ChecksPublisherFactory.fromRun(run, listener)
.publish(builder.withDetailsURL(DisplayURLProvider.get().getRunURL(run))
.build());
return true;
try {
ChecksPublisherFactory.fromRun(run, listener)
.publish(builder.withDetailsURL(DisplayURLProvider.get().getRunURL(run))
.build());
}
catch (RuntimeException e) {
throw new WithChecksPublishException(e);
}
}

static class WithChecksCallBack extends BodyExecutionCallback {
Expand All @@ -169,10 +193,15 @@ static class WithChecksCallBack extends BodyExecutionCallback {

@Override
public void onStart(final StepContext context) {
execution.publish(context, new ChecksDetails.ChecksDetailsBuilder()
.withName(info.getName())
.withStatus(ChecksStatus.IN_PROGRESS)
.withConclusion(ChecksConclusion.NONE));
try {
execution.publish(context, new ChecksDetails.ChecksDetailsBuilder()
.withName(info.getName())
.withStatus(ChecksStatus.IN_PROGRESS)
.withConclusion(ChecksConclusion.NONE));
}
catch (WithChecksPublishException e) {
context.onFailure(e);
}
}

@Override
Expand Down Expand Up @@ -206,7 +235,12 @@ public void onFailure(final StepContext context, final Throwable t) {
.withTitle("Failed")
.withText(t.toString()).build());
}
execution.publish(context, builder);
try {
execution.publish(context, builder);
}
catch (WithChecksPublishException e) {
t.addSuppressed(e);
}
context.onFailure(t);
}
}
Expand Down