From fcf4ca7697b4a5293c95b221669f202621b178f7 Mon Sep 17 00:00:00 2001 From: Jesse Glick Date: Tue, 7 Feb 2017 13:21:22 -0500 Subject: [PATCH] [FIXED JENKINS-41825] Display an informative message, rather than a Groovy exception, when View.getItems fails. --- .../resources/hudson/model/View/main.groovy | 4 ++- .../hudson/model/View/main.properties | 23 ++++++++++++ test/src/test/java/hudson/model/ViewTest.java | 35 ++++++++++++++++++- 3 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 core/src/main/resources/hudson/model/View/main.properties diff --git a/core/src/main/resources/hudson/model/View/main.groovy b/core/src/main/resources/hudson/model/View/main.groovy index 449e992fd18f..52a25f3a6028 100644 --- a/core/src/main/resources/hudson/model/View/main.groovy +++ b/core/src/main/resources/hudson/model/View/main.groovy @@ -3,7 +3,9 @@ package hudson.model.View; t=namespace(lib.JenkinsTagLib) st=namespace("jelly:stapler") -if (items.isEmpty()) { +if (items == null) { + p(_('broken')) +} else if (items.isEmpty()) { if (app.items.size() != 0) { set("views",my.owner.views); set("currentView",my); diff --git a/core/src/main/resources/hudson/model/View/main.properties b/core/src/main/resources/hudson/model/View/main.properties new file mode 100644 index 000000000000..c974575281b6 --- /dev/null +++ b/core/src/main/resources/hudson/model/View/main.properties @@ -0,0 +1,23 @@ +# The MIT License +# +# Copyright 2017 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. + +broken=An error occurred when retrieving jobs for this view. Please consult the Jenkins logs for details. diff --git a/test/src/test/java/hudson/model/ViewTest.java b/test/src/test/java/hudson/model/ViewTest.java index 82314e984118..e4de723f7edf 100644 --- a/test/src/test/java/hudson/model/ViewTest.java +++ b/test/src/test/java/hudson/model/ViewTest.java @@ -53,6 +53,9 @@ import java.io.File; import java.io.IOException; import java.util.Arrays; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.LogRecord; import jenkins.model.ProjectNamingStrategy; import jenkins.security.NotReallyRoleSensitiveCallable; import static org.junit.Assert.*; @@ -61,6 +64,7 @@ import org.junit.Test; import org.jvnet.hudson.test.JenkinsRule; import org.jvnet.hudson.test.JenkinsRule.WebClient; +import org.jvnet.hudson.test.LoggerRule; import org.jvnet.hudson.test.MockAuthorizationStrategy; import org.jvnet.hudson.test.MockFolder; import org.jvnet.hudson.test.TestExtension; @@ -73,6 +77,8 @@ public class ViewTest { @Rule public JenkinsRule j = new JenkinsRule(); + @Rule + public LoggerRule logging = new LoggerRule(); @Issue("JENKINS-7100") @Test public void xHudsonHeader() throws Exception { @@ -506,7 +512,34 @@ public Void call() throws Exception { private void assertCheckJobName(ViewGroup context, String name, FormValidation.Kind expected) { assertEquals(expected, context.getPrimaryView().doCheckJobName(name).kind); } - + + @Issue("JENKINS-41825") + @Test + public void brokenGetItems() throws Exception { + logging.capture(100).record("", Level.INFO); + j.jenkins.addView(new BrokenView()); + j.createWebClient().goTo("view/broken/"); + boolean found = false; + LOGS: for (LogRecord record : logging.getRecords()) { + for (Throwable t = record.getThrown(); t != null; t = t.getCause()) { + if (t instanceof IllegalStateException && BrokenView.ERR.equals(t.getMessage())) { + found = true; + break LOGS; + } + } + } + assertTrue(found); + } + private static class BrokenView extends ListView { + static final String ERR = "oops I cannot retrieve items"; + BrokenView() { + super("broken"); + } + @Override + public List getItems() { + throw new IllegalStateException(ERR); + } + } @Test @Issue("JENKINS-36908")