Skip to content

Commit 6e098b8

Browse files
authored
Merge pull request #143 from jglick/MissingContextVariableException
Mention step name in `MissingContextVariableException` detail message
2 parents 84d3816 + ac2c3b9 commit 6e098b8

File tree

3 files changed

+61
-3
lines changed

3 files changed

+61
-3
lines changed

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.jenkinsci.plugins.workflow.steps;
22

3+
import edu.umd.cs.findbugs.annotations.CheckForNull;
34
import edu.umd.cs.findbugs.annotations.NonNull;
45
import java.util.ArrayList;
56
import java.util.List;
@@ -16,9 +17,17 @@
1617
*/
1718
public class MissingContextVariableException extends Exception {
1819
private final @NonNull Class<?> type;
20+
private final @CheckForNull String functionName;
1921

22+
/** @deprecated use {@link #MissingContextVariableException(Class, StepDescriptor)} */
23+
@Deprecated
2024
public MissingContextVariableException(@NonNull Class<?> type) {
25+
this(type, null);
26+
}
27+
28+
public MissingContextVariableException(@NonNull Class<?> type, @CheckForNull StepDescriptor d) {
2129
this.type = type;
30+
functionName = d != null ? d.getFunctionName() : null;
2231
}
2332

2433
public Class<?> getType() {
@@ -30,7 +39,11 @@ public Class<?> getType() {
3039
boolean first = true;
3140
for (StepDescriptor p : getProviders()) {
3241
if (first) {
33-
b.append("\nPerhaps you forgot to surround the code with a step that provides this, such as: ");
42+
b.append("\nPerhaps you forgot to surround the ");
43+
if (functionName != null) {
44+
b.append(functionName).append(" ");
45+
}
46+
b.append("step with a step that provides this, such as: ");
3447
first = false;
3548
} else {
3649
b.append(", ");

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,9 @@ public final void checkContextAvailability(StepContext c) throws MissingContextV
261261
// TODO the order here is nondeterministic; should we pick the lexicographic first? Or extend MissingContextVariableException to take a Set<Class<?>> types?
262262
for (Class<?> type : getRequiredContext()) {
263263
Object v = c.get(type);
264-
if (v==null)
265-
throw new MissingContextVariableException(type);
264+
if (v == null) {
265+
throw new MissingContextVariableException(type, this);
266+
}
266267
}
267268
}
268269

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright 2023 CloudBees, Inc.
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 hudson.model.Result;
28+
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
29+
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
30+
import org.junit.Rule;
31+
import org.junit.Test;
32+
import org.jvnet.hudson.test.JenkinsRule;
33+
34+
public final class MissingContextVariableExceptionTest {
35+
36+
@Rule public JenkinsRule r = new JenkinsRule();
37+
38+
@Test public void message() throws Exception {
39+
WorkflowJob p = r.createProject(WorkflowJob.class, "p");
40+
p.setDefinition(new CpsFlowDefinition("sh 'oops'", true));
41+
r.assertLogContains("Perhaps you forgot to surround the sh step with a step that provides this, such as: node", r.buildAndAssertStatus(Result.FAILURE, p));
42+
}
43+
44+
}

0 commit comments

Comments
 (0)