Skip to content

Commit 51043f7

Browse files
committed
refactor: (WIP) returning errorOnApply and errorOnAudit
1 parent 61225a9 commit 51043f7

11 files changed

Lines changed: 106 additions & 84 deletions

File tree

core/flamingock-core-commons/src/main/java/io/flamingock/internal/common/core/error/FlamingockException.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@
2222
public class FlamingockException extends RuntimeException {
2323

2424

25-
public FlamingockException() {
26-
super();
27-
}
28-
2925
public FlamingockException(Throwable cause) {
3026
super(cause);
3127
}
@@ -38,12 +34,4 @@ public FlamingockException(String formattedMessage, Object... args) {
3834
super(String.format(formattedMessage, args));
3935
}
4036

41-
public FlamingockException(Throwable cause, String formattedMessage, Object... args) {
42-
this(String.format(formattedMessage, args), cause);
43-
}
44-
45-
public FlamingockException(Throwable cause, String message) {
46-
super(message, cause);
47-
}
48-
4937
}

core/flamingock-core/src/main/java/io/flamingock/internal/core/pipeline/execution/StageExecutionException.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,17 @@
1919

2020
public class StageExecutionException extends FlamingockException {
2121

22-
private final StageSummary summary;
23-
24-
25-
public StageExecutionException(StageSummary summary) {
26-
super("\n\n" + summary.getPretty() + "\n");
27-
this.summary = summary;
22+
public static StageExecutionException fromExisting(Throwable exception, StageSummary summary) {
23+
Throwable cause = exception.getCause();
24+
return (exception instanceof FlamingockException) && cause != null
25+
? new StageExecutionException(cause, summary)
26+
: new StageExecutionException(exception, summary);
2827
}
2928

30-
public StageExecutionException(Throwable throwable, StageSummary summary) {
31-
super(throwable);
29+
private final StageSummary summary;
30+
31+
private StageExecutionException(Throwable cause, StageSummary summary) {
32+
super(cause);
3233
this.summary = summary;
3334
}
3435

core/flamingock-core/src/main/java/io/flamingock/internal/core/pipeline/execution/StageExecutor.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import io.flamingock.internal.core.store.lock.Lock;
2424
import io.flamingock.internal.core.targets.TargetSystemManager;
2525
import io.flamingock.internal.core.task.executable.ExecutableTask;
26+
import io.flamingock.internal.core.task.navigation.FailedChangeProcessResult;
2627
import io.flamingock.internal.core.task.navigation.navigator.ChangeProcessResult;
2728
import io.flamingock.internal.core.task.navigation.navigator.ChangeProcessStrategy;
2829
import io.flamingock.internal.core.task.navigation.navigator.ChangeProcessStrategyFactory;
@@ -91,27 +92,25 @@ public Output executeStage(ExecutableStage executableStage,
9192
})
9293
.filter(ChangeProcessResult::isFailed)
9394
.findFirst()
95+
.map(processResult -> (FailedChangeProcessResult)processResult)
9496
.ifPresent(failedResult -> {
9597
Duration stageDuration = Duration.between(stageStart, LocalDateTime.now());
9698
logger.debug("Stage execution failed [stage={} duration={} failed_change={}]",
9799
stageName, formatDuration(stageDuration), failedResult.getChangeId());
98-
throw new StageExecutionException(summary);
100+
throw StageExecutionException.fromExisting(failedResult.getException(), summary);
99101
});
100102

101103
Duration stageDuration = Duration.between(stageStart, LocalDateTime.now());
102104
logger.info("Stage execution completed successfully [stage={} duration={} tasks={}]",
103105
stageName, formatDuration(stageDuration), taskCount);
104106

105107
} catch (StageExecutionException stageExecutionException) {
106-
Duration stageDuration = Duration.between(stageStart, LocalDateTime.now());
107-
logger.debug("Stage execution failed [stage={} duration={}]",
108-
stageName, formatDuration(stageDuration));
109108
throw stageExecutionException;
110109
} catch (Throwable throwable) {
111110
Duration stageDuration = Duration.between(stageStart, LocalDateTime.now());
112111
logger.debug("Stage execution failed with unexpected error [stage={} duration={} error={}]",
113112
stageName, formatDuration(stageDuration), throwable.getMessage(), throwable);
114-
throw new StageExecutionException(throwable, summary);
113+
throw StageExecutionException.fromExisting(throwable, summary);
115114
}
116115

117116
return new Output(summary);

core/flamingock-core/src/main/java/io/flamingock/internal/core/runner/PipelineExecutionException.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,17 @@
1919

2020
public class PipelineExecutionException extends FlamingockException {
2121

22-
private final PipelineSummary summary;
22+
public static PipelineExecutionException fromExisting(Throwable exception, PipelineSummary summary) {
23+
Throwable cause = exception.getCause();
24+
return (exception instanceof FlamingockException) && cause != null
25+
? new PipelineExecutionException(cause, summary)
26+
: new PipelineExecutionException(exception, summary);
27+
}
2328

29+
private final PipelineSummary summary;
2430

25-
public PipelineExecutionException(PipelineSummary summary) {
26-
super("\n\n" + summary.getPretty() + "\n");
27-
this.summary = summary;
28-
}
2931

30-
public PipelineExecutionException(Throwable throwable, PipelineSummary summary) {
32+
private PipelineExecutionException(Throwable throwable, PipelineSummary summary) {
3133
super(throwable);
3234
this.summary = summary;
3335
}

core/flamingock-core/src/main/java/io/flamingock/internal/core/runner/PipelineRunner.java

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,7 @@
1515
*/
1616
package io.flamingock.internal.core.runner;
1717

18-
import io.flamingock.internal.util.id.RunnerId;
1918
import io.flamingock.internal.common.core.error.FlamingockException;
20-
import io.flamingock.internal.core.plan.ExecutionPlan;
21-
import io.flamingock.internal.core.plan.ExecutionPlanner;
22-
import io.flamingock.internal.core.store.lock.Lock;
23-
import io.flamingock.internal.core.store.lock.LockException;
2419
import io.flamingock.internal.core.event.EventPublisher;
2520
import io.flamingock.internal.core.event.model.impl.PipelineCompletedEvent;
2621
import io.flamingock.internal.core.event.model.impl.PipelineFailedEvent;
@@ -29,13 +24,18 @@
2924
import io.flamingock.internal.core.event.model.impl.StageFailedEvent;
3025
import io.flamingock.internal.core.event.model.impl.StageStartedEvent;
3126
import io.flamingock.internal.core.pipeline.execution.ExecutableStage;
32-
import io.flamingock.internal.core.pipeline.loaded.stage.AbstractLoadedStage;
33-
import io.flamingock.internal.core.pipeline.loaded.LoadedPipeline;
3427
import io.flamingock.internal.core.pipeline.execution.ExecutionContext;
3528
import io.flamingock.internal.core.pipeline.execution.OrphanExecutionContext;
3629
import io.flamingock.internal.core.pipeline.execution.StageExecutionException;
3730
import io.flamingock.internal.core.pipeline.execution.StageExecutor;
3831
import io.flamingock.internal.core.pipeline.execution.StageSummary;
32+
import io.flamingock.internal.core.pipeline.loaded.LoadedPipeline;
33+
import io.flamingock.internal.core.pipeline.loaded.stage.AbstractLoadedStage;
34+
import io.flamingock.internal.core.plan.ExecutionPlan;
35+
import io.flamingock.internal.core.plan.ExecutionPlanner;
36+
import io.flamingock.internal.core.store.lock.Lock;
37+
import io.flamingock.internal.core.store.lock.LockException;
38+
import io.flamingock.internal.util.id.RunnerId;
3939
import io.flamingock.internal.util.log.FlamingockLoggerFactory;
4040
import org.slf4j.Logger;
4141

@@ -82,6 +82,15 @@ public PipelineRunner(RunnerId runnerId,
8282
this.finalizer = finalizer;
8383
}
8484

85+
private static List<AbstractLoadedStage> validateAndGetExecutableStages(LoadedPipeline pipeline) {
86+
pipeline.validate();
87+
List<AbstractLoadedStage> stages = new ArrayList<>();
88+
if (pipeline.getSystemStage().isPresent()) {
89+
stages.add(pipeline.getSystemStage().get());
90+
}
91+
stages.addAll(pipeline.getStages());
92+
return stages;
93+
}
8594

8695
private void run(LoadedPipeline pipeline) throws FlamingockException {
8796

@@ -93,7 +102,7 @@ private void run(LoadedPipeline pipeline) throws FlamingockException {
93102
// Validate execution plan for manual intervention requirements
94103
// This centralized validation ensures both community and cloud paths are validated
95104
execution.validate();
96-
105+
97106
if (pipelineSummary == null) {
98107
pipelineSummary = new PipelineSummary(execution.getPipeline());
99108
}
@@ -121,7 +130,7 @@ private void run(LoadedPipeline pipeline) throws FlamingockException {
121130
//if it's a StageExecutionException, we can safely assume the stage started its
122131
//execution, therefor the pipelinesSummary is initialised
123132
requireNonNull(pipelineSummary).merge(e.getSummary());
124-
throw new PipelineExecutionException(pipelineSummary);
133+
throw PipelineExecutionException.fromExisting(e.getCause(), pipelineSummary);
125134
}
126135
} while (true);
127136

@@ -131,17 +140,6 @@ private void run(LoadedPipeline pipeline) throws FlamingockException {
131140
eventPublisher.publish(new PipelineCompletedEvent());
132141
}
133142

134-
private static List<AbstractLoadedStage> validateAndGetExecutableStages(LoadedPipeline pipeline) {
135-
pipeline.validate();
136-
List<AbstractLoadedStage> stages = new ArrayList<>();
137-
if(pipeline.getSystemStage().isPresent()) {
138-
stages.add(pipeline.getSystemStage().get());
139-
}
140-
stages.addAll(pipeline.getStages());
141-
return stages;
142-
}
143-
144-
145143
private StageSummary runStage(String executionId, Lock lock, ExecutableStage executableStage) {
146144
try {
147145
return startStage(executionId, lock, executableStage);
@@ -164,12 +162,24 @@ private StageSummary startStage(String executionId, Lock lock, ExecutableStage e
164162
return executionOutput.getSummary();
165163
}
166164

167-
private FlamingockException processAndGetFlamingockException(Throwable generalException) throws FlamingockException {
168-
FlamingockException exception = generalException instanceof FlamingockException ? (FlamingockException) generalException : new FlamingockException(generalException);
165+
private FlamingockException processAndGetFlamingockException(Throwable exception) throws FlamingockException {
166+
FlamingockException flamingockException;
167+
if (exception instanceof PipelineExecutionException) {
168+
PipelineExecutionException pipelineException = (PipelineExecutionException) exception;
169+
if (pipelineException.getCause() instanceof FlamingockException) {
170+
flamingockException = (FlamingockException) pipelineException.getCause();
171+
} else {
172+
flamingockException = (PipelineExecutionException) exception;
173+
}
174+
} else if (exception instanceof FlamingockException) {
175+
flamingockException = (FlamingockException) exception;
176+
} else {
177+
flamingockException = new FlamingockException(exception);
178+
}
169179
logger.debug("Error executing the process. ABORTED OPERATION", exception);
170-
eventPublisher.publish(new StageFailedEvent(exception));
171-
eventPublisher.publish(new PipelineFailedEvent(exception));
172-
return exception;
180+
eventPublisher.publish(new StageFailedEvent(flamingockException));
181+
eventPublisher.publish(new PipelineFailedEvent(flamingockException));
182+
return flamingockException;
173183
}
174184

175185
@Override

core/flamingock-core/src/main/java/io/flamingock/internal/core/runtime/MissingInjectedParameterException.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class MissingInjectedParameterException extends FlamingockException {
2424
private final String name;
2525

2626
public MissingInjectedParameterException(Class<?> wrongParameter, String name) {
27-
super();
27+
super(buildMessage(wrongParameter, name));
2828
this.wrongParameter = wrongParameter;
2929
this.name = name;
3030
}
@@ -37,12 +37,11 @@ public String getName() {
3737
return name;
3838
}
3939

40-
@Override
41-
public String getMessage() {
40+
private static String buildMessage(Class<?> wrongParameter, String name) {
4241

4342

4443
StringBuilder sb = new StringBuilder("Wrong parameter[")
45-
.append(getWrongParameter().getSimpleName())
44+
.append(wrongParameter.getSimpleName())
4645
.append("]");
4746
if (name != null) {
4847
sb.append(" with name: ")

core/flamingock-core/src/main/java/io/flamingock/internal/core/store/lock/LockException.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,4 @@ public LockException(String s) {
2626
super(s);
2727
}
2828

29-
public LockException() {
30-
super();
31-
}
3229
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package io.flamingock.internal.core.task.executable;
2+
3+
import io.flamingock.internal.common.core.error.FlamingockException;
4+
5+
public class ChangeExecutionException extends FlamingockException {
6+
public ChangeExecutionException(String changeId,
7+
Throwable cause) {
8+
super(buildMessage(changeId, cause));
9+
}
10+
11+
private static String buildMessage(String changeId, Throwable cause) {
12+
return String.format(
13+
"error trying to apply change with id[%s]: %s",
14+
changeId,
15+
cause.getMessage());
16+
}
17+
}

core/flamingock-core/src/main/java/io/flamingock/internal/core/task/executable/ReflectionExecutableTask.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package io.flamingock.internal.core.task.executable;
1717

1818
import io.flamingock.internal.core.runtime.ExecutionRuntime;
19+
import io.flamingock.internal.core.runtime.MissingInjectedParameterException;
1920
import io.flamingock.internal.core.task.loaded.AbstractReflectionLoadedTask;
2021
import io.flamingock.internal.common.core.recovery.action.ChangeAction;
2122

@@ -75,7 +76,11 @@ public void execute(ExecutionRuntime executionRuntime) {
7576

7677
protected void executeInternal(ExecutionRuntime executionRuntime, Method method ) {
7778
Object instance = executionRuntime.getInstance(descriptor.getConstructor());
78-
executionRuntime.executeMethodWithInjectedDependencies(instance, method);
79+
try {
80+
executionRuntime.executeMethodWithInjectedDependencies(instance, method);
81+
} catch (Throwable ex) {
82+
throw new ChangeExecutionException(this.getId(), ex);
83+
}
7984
}
8085

8186
@Override

core/flamingock-core/src/main/java/io/flamingock/internal/core/task/executable/TemplateExecutableTask.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,19 @@ public TemplateExecutableTask(String stageName,
3939

4040
@Override
4141
protected void executeInternal(ExecutionRuntime executionRuntime, Method method ) {
42-
logger.debug("Starting execution of change[{}] with template: {}", descriptor.getId(), descriptor.getTemplateClass());
43-
logger.debug("change[{}] transactional: {}", descriptor.getId(), descriptor.isTransactional());
44-
Object instance = executionRuntime.getInstance(descriptor.getConstructor());
45-
ChangeTemplate<?,?,?> changeTemplateInstance = (ChangeTemplate<?,?,?>) instance;
46-
changeTemplateInstance.setTransactional(descriptor.isTransactional());
47-
setExecutionData(executionRuntime, changeTemplateInstance, "Configuration");
48-
setExecutionData(executionRuntime, changeTemplateInstance, "ApplyPayload");
49-
setExecutionData(executionRuntime, changeTemplateInstance, "RollbackPayload");
50-
executionRuntime.executeMethodWithInjectedDependencies(instance, method);
42+
try {
43+
logger.debug("Starting execution of change[{}] with template: {}", descriptor.getId(), descriptor.getTemplateClass());
44+
logger.debug("change[{}] transactional: {}", descriptor.getId(), descriptor.isTransactional());
45+
Object instance = executionRuntime.getInstance(descriptor.getConstructor());
46+
ChangeTemplate<?,?,?> changeTemplateInstance = (ChangeTemplate<?,?,?>) instance;
47+
changeTemplateInstance.setTransactional(descriptor.isTransactional());
48+
setExecutionData(executionRuntime, changeTemplateInstance, "Configuration");
49+
setExecutionData(executionRuntime, changeTemplateInstance, "ApplyPayload");
50+
setExecutionData(executionRuntime, changeTemplateInstance, "RollbackPayload");
51+
executionRuntime.executeMethodWithInjectedDependencies(instance, method);
52+
} catch (Throwable ex) {
53+
throw new ChangeExecutionException(this.getId(), ex);
54+
}
5155
}
5256

5357

0 commit comments

Comments
 (0)