Skip to content

Commit

Permalink
Mention step name in MissingContextVariableException detail message
Browse files Browse the repository at this point in the history
  • Loading branch information
jglick committed Nov 15, 2023
1 parent 2aa692a commit ac2c3b9
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jenkinsci.plugins.workflow.steps;

import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -16,9 +17,17 @@
*/
public class MissingContextVariableException extends Exception {
private final @NonNull Class<?> type;
private final @CheckForNull String functionName;

/** @deprecated use {@link #MissingContextVariableException(Class, StepDescriptor)} */
@Deprecated
public MissingContextVariableException(@NonNull Class<?> type) {
this(type, null);
}

Check warning on line 26 in src/main/java/org/jenkinsci/plugins/workflow/steps/MissingContextVariableException.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 25-26 are not covered by tests

public MissingContextVariableException(@NonNull Class<?> type, @CheckForNull StepDescriptor d) {
this.type = type;
functionName = d != null ? d.getFunctionName() : null;

Check warning on line 30 in src/main/java/org/jenkinsci/plugins/workflow/steps/MissingContextVariableException.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 30 is only partially covered, one branch is missing
}

public Class<?> getType() {
Expand All @@ -30,7 +39,11 @@ public Class<?> getType() {
boolean first = true;
for (StepDescriptor p : getProviders()) {
if (first) {
b.append("\nPerhaps you forgot to surround the code with a step that provides this, such as: ");
b.append("\nPerhaps you forgot to surround the ");
if (functionName != null) {

Check warning on line 43 in src/main/java/org/jenkinsci/plugins/workflow/steps/MissingContextVariableException.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 43 is only partially covered, one branch is missing
b.append(functionName).append(" ");
}
b.append("step with a step that provides this, such as: ");
first = false;
} else {
b.append(", ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,9 @@ public final void checkContextAvailability(StepContext c) throws MissingContextV
// TODO the order here is nondeterministic; should we pick the lexicographic first? Or extend MissingContextVariableException to take a Set<Class<?>> types?
for (Class<?> type : getRequiredContext()) {
Object v = c.get(type);
if (v==null)
throw new MissingContextVariableException(type);
if (v == null) {
throw new MissingContextVariableException(type, this);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* The MIT License
*
* Copyright 2023 CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package org.jenkinsci.plugins.workflow.steps;

import hudson.model.Result;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

public final class MissingContextVariableExceptionTest {

@Rule public JenkinsRule r = new JenkinsRule();

@Test public void message() throws Exception {
WorkflowJob p = r.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition("sh 'oops'", true));
r.assertLogContains("Perhaps you forgot to surround the sh step with a step that provides this, such as: node", r.buildAndAssertStatus(Result.FAILURE, p));
}

}

0 comments on commit ac2c3b9

Please sign in to comment.