Skip to content

Commit b226238

Browse files
authored
feat: improve workflow error reporting (#1613)
1 parent 532b480 commit b226238

File tree

5 files changed

+78
-103
lines changed

5 files changed

+78
-103
lines changed
Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,28 @@
11
package io.javaoperatorsdk.operator;
22

3-
import java.util.Arrays;
43
import java.util.Collections;
5-
import java.util.List;
4+
import java.util.Map;
5+
import java.util.stream.Collectors;
66

77
public class AggregatedOperatorException extends OperatorException {
88

9-
private final List<Exception> causes;
9+
private final Map<String, Exception> causes;
1010

11-
public AggregatedOperatorException(String message, Exception... exceptions) {
12-
super(message, exceptions != null && exceptions.length > 0 ? exceptions[0] : null);
13-
this.causes = exceptions != null ? Arrays.asList(exceptions) : Collections.emptyList();
11+
public AggregatedOperatorException(String message, Map<String, Exception> exceptions) {
12+
super(message);
13+
this.causes =
14+
exceptions != null ? Collections.unmodifiableMap(exceptions) : Collections.emptyMap();
1415
}
1516

16-
public AggregatedOperatorException(String message, List<Exception> exceptions) {
17-
super(message, exceptions != null && !exceptions.isEmpty() ? exceptions.get(0) : null);
18-
this.causes = exceptions != null ? exceptions : Collections.emptyList();
19-
}
20-
21-
public List<Exception> getAggregatedExceptions() {
17+
@SuppressWarnings("unused")
18+
public Map<String, Exception> getAggregatedExceptions() {
2219
return causes;
2320
}
2421

2522
@Override
26-
public String toString() {
27-
return "AggregatedOperatorException{" +
28-
"causes=" + causes +
29-
'}';
23+
public String getMessage() {
24+
return super.getMessage() + " " + causes.entrySet().stream()
25+
.map(entry -> entry.getKey() + " -> " + entry.getValue())
26+
.collect(Collectors.joining("\n - ", "Details:\n", ""));
3027
}
3128
}

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/workflow/WorkflowCleanupExecutor.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.List;
44
import java.util.Map;
5+
import java.util.Map.Entry;
56
import java.util.Optional;
67
import java.util.Set;
78
import java.util.concurrent.ConcurrentHashMap;
@@ -176,17 +177,14 @@ private boolean hasErroredDependent(DependentResourceNode dependentResourceNode)
176177
}
177178

178179
private WorkflowCleanupResult createCleanupResult() {
179-
var result = new WorkflowCleanupResult();
180-
result.setErroredDependents(exceptionsDuringExecution
181-
.entrySet().stream()
182-
.collect(Collectors.toMap(e -> e.getKey().getDependentResource(), Map.Entry::getValue)));
183-
184-
result.setPostConditionNotMetDependents(
185-
postDeleteConditionNotMet.stream().map(DependentResourceNode::getDependentResource)
186-
.collect(Collectors.toList()));
187-
result.setDeleteCalledOnDependents(
188-
deleteCalled.stream().map(DependentResourceNode::getDependentResource)
189-
.collect(Collectors.toList()));
190-
return result;
180+
final var erroredDependents = exceptionsDuringExecution.entrySet().stream()
181+
.collect(Collectors.toMap(e -> e.getKey().getDependentResource(), Entry::getValue));
182+
final var postConditionNotMet = postDeleteConditionNotMet.stream()
183+
.map(DependentResourceNode::getDependentResource)
184+
.collect(Collectors.toList());
185+
final var deleteCalled =
186+
this.deleteCalled.stream().map(DependentResourceNode::getDependentResource)
187+
.collect(Collectors.toList());
188+
return new WorkflowCleanupResult(erroredDependents, postConditionNotMet, deleteCalled);
191189
}
192190
}
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,32 @@
11
package io.javaoperatorsdk.operator.processing.dependent.workflow;
22

3-
import java.util.ArrayList;
4-
import java.util.HashMap;
53
import java.util.List;
64
import java.util.Map;
75

8-
import io.javaoperatorsdk.operator.AggregatedOperatorException;
96
import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResource;
107

118
@SuppressWarnings("rawtypes")
12-
public class WorkflowCleanupResult {
9+
public class WorkflowCleanupResult extends WorkflowResult {
1310

14-
private List<DependentResource> deleteCalledOnDependents = new ArrayList<>();
15-
private List<DependentResource> postConditionNotMetDependents = new ArrayList<>();
16-
private Map<DependentResource, Exception> erroredDependents = new HashMap<>();
11+
private final List<DependentResource> deleteCalledOnDependents;
12+
private final List<DependentResource> postConditionNotMetDependents;
1713

18-
public List<DependentResource> getDeleteCalledOnDependents() {
19-
return deleteCalledOnDependents;
14+
WorkflowCleanupResult(Map<DependentResource, Exception> erroredDependents,
15+
List<DependentResource> postConditionNotMet, List<DependentResource> deleteCalled) {
16+
super(erroredDependents);
17+
this.deleteCalledOnDependents = deleteCalled;
18+
this.postConditionNotMetDependents = postConditionNotMet;
2019
}
2120

22-
public WorkflowCleanupResult setDeleteCalledOnDependents(
23-
List<DependentResource> deletedDependents) {
24-
this.deleteCalledOnDependents = deletedDependents;
25-
return this;
21+
public List<DependentResource> getDeleteCalledOnDependents() {
22+
return deleteCalledOnDependents;
2623
}
2724

2825
public List<DependentResource> getPostConditionNotMetDependents() {
2926
return postConditionNotMetDependents;
3027
}
3128

32-
public WorkflowCleanupResult setPostConditionNotMetDependents(
33-
List<DependentResource> postConditionNotMetDependents) {
34-
this.postConditionNotMetDependents = postConditionNotMetDependents;
35-
return this;
36-
}
37-
38-
public Map<DependentResource, Exception> getErroredDependents() {
39-
return erroredDependents;
40-
}
41-
42-
public WorkflowCleanupResult setErroredDependents(
43-
Map<DependentResource, Exception> erroredDependents) {
44-
this.erroredDependents = erroredDependents;
45-
return this;
46-
}
47-
4829
public boolean allPostConditionsMet() {
4930
return postConditionNotMetDependents.isEmpty();
5031
}
51-
52-
public boolean erroredDependentsExists() {
53-
return !erroredDependents.isEmpty();
54-
}
55-
56-
public void throwAggregateExceptionIfErrorsPresent() {
57-
if (erroredDependentsExists()) {
58-
throw new AggregatedOperatorException("Exception(s) during workflow execution.",
59-
new ArrayList<>(erroredDependents.values()));
60-
}
61-
}
62-
63-
6432
}
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,28 @@
11
package io.javaoperatorsdk.operator.processing.dependent.workflow;
22

3-
import java.util.ArrayList;
43
import java.util.List;
54
import java.util.Map;
65

7-
import io.javaoperatorsdk.operator.AggregatedOperatorException;
86
import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResource;
97
import io.javaoperatorsdk.operator.api.reconciler.dependent.ReconcileResult;
108

119
@SuppressWarnings("rawtypes")
12-
public class WorkflowReconcileResult {
10+
public class WorkflowReconcileResult extends WorkflowResult {
1311

1412
private final List<DependentResource> reconciledDependents;
1513
private final List<DependentResource> notReadyDependents;
16-
private final Map<DependentResource, Exception> erroredDependents;
1714
private final Map<DependentResource, ReconcileResult> reconcileResults;
1815

1916
public WorkflowReconcileResult(List<DependentResource> reconciledDependents,
2017
List<DependentResource> notReadyDependents,
2118
Map<DependentResource, Exception> erroredDependents,
2219
Map<DependentResource, ReconcileResult> reconcileResults) {
20+
super(erroredDependents);
2321
this.reconciledDependents = reconciledDependents;
2422
this.notReadyDependents = notReadyDependents;
25-
this.erroredDependents = erroredDependents;
2623
this.reconcileResults = reconcileResults;
2724
}
2825

29-
public Map<DependentResource, Exception> getErroredDependents() {
30-
return erroredDependents;
31-
}
32-
3326
public List<DependentResource> getReconciledDependents() {
3427
return reconciledDependents;
3528
}
@@ -43,31 +36,7 @@ public Map<DependentResource, ReconcileResult> getReconcileResults() {
4336
return reconcileResults;
4437
}
4538

46-
public void throwAggregateExceptionIfErrorsPresent() {
47-
if (!erroredDependents.isEmpty()) {
48-
throw createFinalException();
49-
}
50-
}
51-
52-
private AggregatedOperatorException createFinalException() {
53-
return new AggregatedOperatorException("Exception during workflow.",
54-
new ArrayList<>(erroredDependents.values()));
55-
}
56-
5739
public boolean allDependentResourcesReady() {
5840
return notReadyDependents.isEmpty();
5941
}
60-
61-
/**
62-
* @deprecated Use {@link #erroredDependentsExist()} instead
63-
*/
64-
@Deprecated(forRemoval = true)
65-
public boolean erroredDependentsExists() {
66-
return !erroredDependents.isEmpty();
67-
}
68-
69-
@SuppressWarnings("unused")
70-
public boolean erroredDependentsExist() {
71-
return !erroredDependents.isEmpty();
72-
}
7342
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package io.javaoperatorsdk.operator.processing.dependent.workflow;
2+
3+
import java.util.Map;
4+
import java.util.Map.Entry;
5+
import java.util.stream.Collectors;
6+
7+
import io.javaoperatorsdk.operator.AggregatedOperatorException;
8+
import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResource;
9+
10+
@SuppressWarnings("rawtypes")
11+
class WorkflowResult {
12+
13+
private final Map<DependentResource, Exception> erroredDependents;
14+
15+
WorkflowResult(Map<DependentResource, Exception> erroredDependents) {
16+
this.erroredDependents = erroredDependents;
17+
}
18+
19+
public Map<DependentResource, Exception> getErroredDependents() {
20+
return erroredDependents;
21+
}
22+
23+
/**
24+
* @deprecated Use {@link #erroredDependentsExist()} instead
25+
*/
26+
@Deprecated(forRemoval = true)
27+
public boolean erroredDependentsExists() {
28+
return !erroredDependents.isEmpty();
29+
}
30+
31+
@SuppressWarnings("unused")
32+
public boolean erroredDependentsExist() {
33+
return !erroredDependents.isEmpty();
34+
}
35+
36+
public void throwAggregateExceptionIfErrorsPresent() {
37+
if (erroredDependentsExist()) {
38+
throw new AggregatedOperatorException("Exception(s) during workflow execution.",
39+
erroredDependents.entrySet().stream()
40+
.collect(Collectors.toMap(e -> e.getKey().getClass().getName(), Entry::getValue)));
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)