Skip to content

Commit 3e456cc

Browse files
authored
Merge pull request #236 from damianszczepanik/FlowInterruptedException
FlowInterruptedException::getMessage returns exception details
2 parents 664bdaf + 49bdb36 commit 3e456cc

File tree

3 files changed

+85
-1
lines changed

3 files changed

+85
-1
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
target
22
work
3-
*.iml
3+
*.iml
4+
.idea

src/main/java/org/jenkinsci/plugins/workflow/steps/FlowInterruptedException.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
import java.util.LinkedHashSet;
3737
import java.util.List;
3838
import java.util.Set;
39+
import java.util.stream.Collectors;
40+
3941
import edu.umd.cs.findbugs.annotations.CheckForNull;
4042
import edu.umd.cs.findbugs.annotations.NonNull;
4143
import jenkins.model.CauseOfInterruption;
@@ -109,6 +111,13 @@ private Object readResolve() {
109111
return this;
110112
}
111113

114+
@Override
115+
public String getMessage() {
116+
return causes.stream()
117+
.map(CauseOfInterruption::getShortDescription)
118+
.collect(Collectors.joining( ", " ));
119+
}
120+
112121
/**
113122
* If a build catches this exception, it should use this method to report it.
114123
*/
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright 2025 Damian Szczepanik
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
package org.jenkinsci.plugins.workflow.steps;
26+
27+
import static org.hamcrest.MatcherAssert.assertThat;
28+
import static org.hamcrest.Matchers.containsString;
29+
import static org.hamcrest.Matchers.equalTo;
30+
31+
import hudson.model.Result;
32+
import hudson.model.Run;
33+
import jenkins.model.CauseOfInterruption;
34+
import org.jenkinsci.plugins.workflow.job.properties.DisableConcurrentBuildsJobProperty;
35+
import org.junit.Assert;
36+
import org.junit.Test;
37+
import org.mockito.Mock;
38+
import org.mockito.Mockito;
39+
40+
/**
41+
* Tests some specific {@link FlowInterruptedException} APIs
42+
*/
43+
public class FlowInterruptedExceptionTest {
44+
45+
@Test
46+
public void getMessageReturnsCauses() {
47+
// given
48+
Result result = Result.ABORTED;
49+
CauseOfInterruption cause1 = new ExceptionCause(new IllegalStateException("something went wrong"));
50+
CauseOfInterruption cause2 = new CauseOfInterruption.UserInterruption("admin");
51+
CauseOfInterruption[] causes = {cause1, cause2};
52+
53+
// when
54+
FlowInterruptedException exception = new FlowInterruptedException(result, true, causes);
55+
56+
// then
57+
assertThat(exception.getMessage(), equalTo(cause1.getShortDescription() + ", " + cause2.getShortDescription()));
58+
}
59+
60+
@Test
61+
public void toStringContainsCauses() {
62+
// given
63+
Result result = Result.FAILURE;
64+
Run run = Mockito.mock(Run.class);
65+
Mockito.when(run.getDisplayName()).thenReturn("fracture.account");
66+
CauseOfInterruption cause = new DisableConcurrentBuildsJobProperty.CancelledCause(run);
67+
68+
// when
69+
FlowInterruptedException exception = new FlowInterruptedException(result, true, cause);
70+
71+
// then
72+
assertThat(exception.toString(), containsString(cause.getShortDescription()));
73+
}
74+
}

0 commit comments

Comments
 (0)