Breaking the cycle with HttpServerErrorException

This commit is contained in:
Marcin Grzejszczak
2020-07-24 14:23:38 +02:00
parent f18fa1df81
commit 9834106712
3 changed files with 36 additions and 6 deletions

View File

@@ -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<Throwable> 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<Throwable> 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<Throwable> throwables) {
this.exceptions.addAll(throwables);

View File

@@ -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<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) {
return new RuntimeException(
"[Breaking self reference chain] " + cause.toString());
}
return cause;
}
public static ExecutionResult unstable(Exception ex) {

View File

@@ -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