Skip to content

Commit 521ecfd

Browse files
authored
Better error response from doBuildWithParameters (#10954)
1 parent 1fc1b4a commit 521ecfd

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

core/src/main/java/jenkins/model/ParameterizedJobMixIn.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
package jenkins.model;
2626

27+
import static jakarta.servlet.http.HttpServletResponse.SC_BAD_REQUEST;
2728
import static jakarta.servlet.http.HttpServletResponse.SC_CONFLICT;
2829
import static jakarta.servlet.http.HttpServletResponse.SC_CREATED;
2930

@@ -201,7 +202,7 @@ public final void doBuild(StaplerRequest2 req, StaplerResponse2 rsp, @QueryParam
201202
}
202203

203204
if (!asJob().isBuildable()) {
204-
throw HttpResponses.error(SC_CONFLICT, new IOException(asJob().getFullName() + " is not buildable"));
205+
throw HttpResponses.errorWithoutStack(SC_CONFLICT, asJob().getFullName() + " is not buildable");
205206
}
206207

207208
// if a build is parameterized, let that take over
@@ -238,12 +239,12 @@ public final void doBuildWithParameters(StaplerRequest2 req, StaplerResponse2 rs
238239

239240
ParametersDefinitionProperty pp = asJob().getProperty(ParametersDefinitionProperty.class);
240241
if (!asJob().isBuildable()) {
241-
throw HttpResponses.error(SC_CONFLICT, new IOException(asJob().getFullName() + " is not buildable!"));
242+
throw HttpResponses.errorWithoutStack(SC_CONFLICT, asJob().getFullName() + " is not buildable");
242243
}
243244
if (pp != null) {
244245
pp.buildWithParameters(req, rsp, delay);
245246
} else {
246-
throw new IllegalStateException("This build is not parameterized!");
247+
throw HttpResponses.errorWithoutStack(SC_BAD_REQUEST, asJob().getFullName() + " is not parameterized");
247248
}
248249
}
249250

test/src/test/java/jenkins/model/ParameterizedJobMixInTest.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,22 @@ void doBuildWithParameters_shouldFailWhenInvokingDisabledProject() throws Except
8181

8282
FailingHttpStatusCodeException fex = assertThrows(
8383
FailingHttpStatusCodeException.class,
84-
() -> webClient.getPage(webClient.addCrumb(new WebRequest(new URL(j.getURL(), project.getUrl() + "build?delay=0"), HttpMethod.POST))),
84+
() -> webClient.getPage(webClient.addCrumb(new WebRequest(new URL(j.getURL(), project.getUrl() + "buildWithParameters?FOO=x"), HttpMethod.POST))),
8585
"should fail when invoking disabled project");
86-
assertThat("Should fail with conflict", fex.getStatusCode(), is(409));
86+
assertThat("Should fail with conflict", fex.getStatusCode(), is(HttpServletResponse.SC_CONFLICT));
87+
}
88+
89+
@Test
90+
void doBuildWithParameters_shouldFailWhenInvokingNonParameterizedProject() throws Exception {
91+
final FreeStyleProject project = j.createFreeStyleProject();
92+
93+
final JenkinsRule.WebClient webClient = j.createWebClient();
94+
95+
FailingHttpStatusCodeException fex = assertThrows(
96+
FailingHttpStatusCodeException.class,
97+
() -> webClient.getPage(webClient.addCrumb(new WebRequest(new URL(j.getURL(), project.getUrl() + "buildWithParameters?FOO=x"), HttpMethod.POST))),
98+
"should fail when invoking non-parameterized project");
99+
assertThat("Should fail with bad request", fex.getStatusCode(), is(HttpServletResponse.SC_BAD_REQUEST));
87100
}
88101

89102
@Test

0 commit comments

Comments
 (0)