Breaking the exception cycle

This commit is contained in:
Marcin Grzejszczak
2020-11-09 10:48:39 +01:00
parent 3d76f129d3
commit e9d0c7502e
3 changed files with 42 additions and 18 deletions

View File

@@ -64,22 +64,30 @@ public class SaganUpdater {
originalVersion, currentVersion, projects); originalVersion, currentVersion, projects);
if (updateReleaseException == null) { if (updateReleaseException == null) {
log.info("Updating Sagan releases with \n\n{}", update); log.info("Updating Sagan releases with \n\n{}", update);
Project project = this.saganClient.updateRelease(currentVersion.projectName, try {
Collections.singletonList(update)); Project project = this.saganClient.updateRelease(
Optional<ProjectVersion> projectVersion = latestVersion(currentVersion, currentVersion.projectName, Collections.singletonList(update));
project); Optional<ProjectVersion> projectVersion = latestVersion(currentVersion,
log.info("Found the following latest project version [{}]", projectVersion); project);
boolean present = projectVersion.isPresent(); log.info("Found the following latest project version [{}]",
if (present && currentVersionNewerOrEqual(currentVersion, projectVersion)) { projectVersion);
updateDocumentationIfNecessary(projectFile, project); boolean present = projectVersion.isPresent();
if (present
&& currentVersionNewerOrEqual(currentVersion, projectVersion)) {
updateDocumentationIfNecessary(projectFile, project);
}
else {
log.info(present
? "Latest version [" + projectVersion.get() + "] present and "
+ "the current version [" + currentVersion
+ "] is older than that one. " + "Will do nothing."
: "No latest version found. Will do nothing.");
return ExecutionResult.skipped();
}
} }
else { catch (Exception ex) {
log.info(present log.warn("Exception occurred while trying to update sagan release", ex);
? "Latest version [" + projectVersion.get() + "] present and " updateReleaseException = ex;
+ "the current version [" + currentVersion
+ "] is older than that one. " + "Will do nothing."
: "No latest version found. Will do nothing.");
return ExecutionResult.skipped();
} }
} }
return updateReleaseException == null ? ExecutionResult.success() return updateReleaseException == null ? ExecutionResult.success()

View File

@@ -19,9 +19,13 @@ package releaser.internal.tech;
import java.io.Serializable; import java.io.Serializable;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
/** /**
* Task execution result. Contains a list of exceptions thrown while running the task. * Task execution result. Contains a list of exceptions thrown while running the task.
*/ */
@@ -57,11 +61,22 @@ public class ExecutionResult implements Serializable {
} }
public static ExecutionResult failure(Exception throwable) { public static ExecutionResult failure(Exception throwable) {
return new ExecutionResult(throwable); return new ExecutionResult(breakReferenceChain(throwable));
} }
public static ExecutionResult failure(List<Exception> throwables) { public static ExecutionResult failure(List<Exception> throwables) {
return new ExecutionResult(throwables); return new ExecutionResult(throwables.stream()
.map(ExecutionResult::breakReferenceChain).collect(Collectors.toList()));
}
private static Exception breakReferenceChain(Exception cause) {
if (cause instanceof HttpServerErrorException
|| cause instanceof HttpClientErrorException) {
System.out.println("Breaking the reference chain. . . i think");
return new RuntimeException(
"[Breaking self reference chain] " + cause.toString());
}
return cause;
} }
public static ExecutionResult unstable(Exception ex) { public static ExecutionResult unstable(Exception ex) {

View File

@@ -49,6 +49,7 @@ import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.Step; import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepExecution; import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.StepExecutionListener; import org.springframework.batch.core.StepExecutionListener;
import org.springframework.batch.core.UnexpectedJobExecutionException;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.job.builder.FlowBuilder; import org.springframework.batch.core.job.builder.FlowBuilder;
@@ -423,7 +424,7 @@ class SpringBatchFlowRunner implements FlowRunner, Closeable {
List<Exception> thrownExceptions = exceptionsThrownBySteps(execution); List<Exception> thrownExceptions = exceptionsThrownBySteps(execution);
return new ExecutionResult(thrownExceptions); return new ExecutionResult(thrownExceptions);
} }
catch (JobExecutionException ex) { catch (JobExecutionException | UnexpectedJobExecutionException ex) {
return ExecutionResult.failure(ex); return ExecutionResult.failure(ex);
} }
} }