From 98341067124ef6f970025ebb779f2b36f3a9b9c0 Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Fri, 24 Jul 2020 14:23:38 +0200 Subject: [PATCH] Breaking the cycle with HttpServerErrorException --- .../internal/tech/BuildUnstableException.java | 18 +++++++++++++++--- .../internal/tech/ExecutionResult.java | 16 ++++++++++++++-- .../spring/SpringBatchFlowRunnerTests.java | 8 +++++++- 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/releaser-core/src/main/java/releaser/internal/tech/BuildUnstableException.java b/releaser-core/src/main/java/releaser/internal/tech/BuildUnstableException.java index 68f51834..858115b0 100644 --- a/releaser-core/src/main/java/releaser/internal/tech/BuildUnstableException.java +++ b/releaser-core/src/main/java/releaser/internal/tech/BuildUnstableException.java @@ -19,12 +19,15 @@ package releaser.internal.tech; import java.io.Serializable; import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.web.client.HttpServerErrorException; + /** * Exception to be thrown if one wants to continue with the build and throw this exception * at the end of the release. @@ -52,13 +55,14 @@ public class BuildUnstableException extends RuntimeException implements Serializ private final List exceptions = new ArrayList<>(); public BuildUnstableException(Throwable cause) { - super(cause); + super(breakReferenceChain(cause)); log.warn("\n\n" + DESCRIPTION, cause); - this.exceptions.add(cause); + this.exceptions.add((breakReferenceChain(cause))); } public BuildUnstableException(String message, List throwables) { - this(throwables); + this(throwables.stream().map(BuildUnstableException::breakReferenceChain) + .collect(Collectors.toList())); log.warn("\n\n" + DESCRIPTION + message + " with causes " + throwables); } @@ -66,6 +70,14 @@ public class BuildUnstableException extends RuntimeException implements Serializ public BuildUnstableException() { } + private static Throwable breakReferenceChain(Throwable cause) { + if (cause instanceof HttpServerErrorException) { + return new RuntimeException( + "[Breaking self reference chain] " + cause.toString()); + } + return cause; + } + @JsonCreator public BuildUnstableException(@JsonProperty List throwables) { this.exceptions.addAll(throwables); diff --git a/releaser-core/src/main/java/releaser/internal/tech/ExecutionResult.java b/releaser-core/src/main/java/releaser/internal/tech/ExecutionResult.java index 1368448d..08b7af63 100644 --- a/releaser-core/src/main/java/releaser/internal/tech/ExecutionResult.java +++ b/releaser-core/src/main/java/releaser/internal/tech/ExecutionResult.java @@ -19,9 +19,12 @@ package releaser.internal.tech; import java.io.Serializable; import java.util.LinkedList; import java.util.List; +import java.util.stream.Collectors; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import org.springframework.web.client.HttpServerErrorException; + /** * Task execution result. Contains a list of exceptions thrown while running the task. */ @@ -57,11 +60,20 @@ public class ExecutionResult implements Serializable { } public static ExecutionResult failure(Exception throwable) { - return new ExecutionResult(throwable); + return new ExecutionResult(breakReferenceChain(throwable)); } public static ExecutionResult failure(List 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) { + return new RuntimeException( + "[Breaking self reference chain] " + cause.toString()); + } + return cause; } public static ExecutionResult unstable(Exception ex) { diff --git a/releaser-spring/src/test/java/releaser/internal/spring/SpringBatchFlowRunnerTests.java b/releaser-spring/src/test/java/releaser/internal/spring/SpringBatchFlowRunnerTests.java index c9cb9370..99ecd885 100644 --- a/releaser-spring/src/test/java/releaser/internal/spring/SpringBatchFlowRunnerTests.java +++ b/releaser-spring/src/test/java/releaser/internal/spring/SpringBatchFlowRunnerTests.java @@ -17,6 +17,7 @@ package releaser.internal.spring; import java.io.File; +import java.nio.charset.Charset; import org.assertj.core.api.BDDAssertions; import org.junit.jupiter.api.Test; @@ -35,7 +36,10 @@ import org.springframework.boot.SpringBootConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.annotation.Bean; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; import org.springframework.test.context.ActiveProfiles; +import org.springframework.web.client.HttpServerErrorException; @SpringBootTest @ActiveProfiles("batch") @@ -135,7 +139,9 @@ class MyProjectPostReleaseTask implements ProjectPostReleaseReleaserTask { @Override public ExecutionResult runTask(Arguments args) { - return ExecutionResult.unstable(new RuntimeException("Some instability")); + return ExecutionResult.unstable( + HttpServerErrorException.create(HttpStatus.INTERNAL_SERVER_ERROR, "foo", + new HttpHeaders(), "".getBytes(), Charset.defaultCharset())); } @Override