Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ public String executeStep(Step step)
stepExecution.getExecutionContext().put("batch.restart", true);
}

// Preserve the step's exit description (e.g., the stack trace recorded by
// AbstractStep when a step fails) so it survives the flow's final
// updateJobExecutionStatus() call, which only carries the FlowExecutionStatus
// name and would otherwise discard it.
exitStatus = exitStatus.addExitDescription(stepExecution.getExitStatus().getExitDescription());

return stepExecution.getExitStatus().getExitCode();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2023 the original author or authors.
* Copyright 2010-2026 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,6 +16,7 @@
package org.springframework.batch.core.job.flow;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -91,6 +92,35 @@ void testStepFailure() throws Exception {
assertEquals(BatchStatus.FAILED, execution.getStatus());
}

@Test // gh-3740
void testStepFailureExitDescriptionPropagatedToJobExecution() throws Exception {
final String failureDescription = "boom: simulated step failure";
SimpleFlow flow = new SimpleFlow("job");
List<StateTransition> transitions = new ArrayList<>();
StepState step = new StepState(new StepSupport("step") {
@Override
public void execute(StepExecution stepExecution)
throws JobInterruptedException, UnexpectedJobExecutionException {
stepExecution.setExitStatus(ExitStatus.FAILED.addExitDescription(failureDescription));
stepExecution.setStatus(BatchStatus.FAILED);
}
});
transitions.add(StateTransition.createStateTransition(step, ExitStatus.FAILED.getExitCode(), "end0"));
transitions.add(StateTransition.createStateTransition(step, ExitStatus.COMPLETED.getExitCode(), "end1"));
transitions.add(StateTransition.createEndStateTransition(new EndState(FlowExecutionStatus.FAILED, "end0")));
transitions.add(StateTransition.createEndStateTransition(new EndState(FlowExecutionStatus.COMPLETED, "end1")));
flow.setStateTransitions(transitions);
job.setFlow(flow);
job.afterPropertiesSet();
job.execute(execution);

assertEquals(BatchStatus.FAILED, execution.getStatus());
assertEquals(ExitStatus.FAILED.getExitCode(), execution.getExitStatus().getExitCode());
assertTrue(execution.getExitStatus().getExitDescription().contains(failureDescription),
"Expected JobExecution exit description to contain step failure description but was: '"
+ execution.getExitStatus().getExitDescription() + "'");
}

@Test
void testStepStatusUnknown() throws Exception {
SimpleFlow flow = new SimpleFlow("job");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void testLaunchFailedJob() throws Exception {
JobExecution jobExecution = jobOperator.start(job,
new JobParametersBuilder().addLong("run.id", 2L).toJobParameters());
assertEquals(BatchStatus.FAILED, jobExecution.getStatus());
assertEquals(ExitStatus.FAILED, jobExecution.getExitStatus());
assertEquals(ExitStatus.FAILED.getExitCode(), jobExecution.getExitStatus().getExitCode());
}

}