Skip to content

Commit 8e91388

Browse files
committed
Include URL in loadJSON and loadJSONHTML exception messages
Includes the basename of the original exception and the message of the original exception so that the exception message retains the information from the original exception. Without the original exception message and exception class, it can be difficult to understand the cause of the message. Was it a connection timeout or a connection reset or a hostname not found or something else?
1 parent 08260e8 commit 8e91388

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

core/src/main/java/hudson/PluginManager.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2177,15 +2177,13 @@ private FormValidation checkUpdatesServer() throws Exception {
21772177
FormValidation v = site.updateDirectlyNow();
21782178
if (v.kind != FormValidation.Kind.OK) {
21792179
// Stop with an error
2180-
LOGGER.log(Level.SEVERE, "Failed to check updates server: " + site.getUrl());
21812180
return v;
21822181
}
21832182
}
21842183
for (DownloadService.Downloadable d : DownloadService.Downloadable.all()) {
21852184
FormValidation v = d.updateNow();
21862185
if (v.kind != FormValidation.Kind.OK) {
21872186
// Stop with an error
2188-
LOGGER.log(Level.SEVERE, "Failed to update downloadable: " + d.getUrl());
21892187
return v;
21902188
}
21912189
}

core/src/main/java/hudson/model/DownloadService.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,13 @@ public static String loadJSON(URL src) throws IOException {
128128
} else {
129129
throw new IOException("Could not find JSON in " + src);
130130
}
131+
} catch (IOException ioe) {
132+
// Include basename of exception and original message text
133+
// in the new message so that the person reading the
134+
// message understands the exception type and the text
135+
// provided by the original exception.
136+
String ioeMessage = ioe.getClass().getSimpleName() + ": " + ioe.getMessage();
137+
throw new IOException("Could not load JSON from " + src + " due to " + ioeMessage);
131138
}
132139
}
133140

@@ -154,6 +161,13 @@ public static String loadJSONHTML(URL src) throws IOException {
154161
} else {
155162
throw new IOException("Could not find JSON in " + src);
156163
}
164+
} catch (IOException ioe) {
165+
// Include basename of exception and original message text
166+
// in the new message so that the person reading the
167+
// message understands the exception type and the text
168+
// provided by the original exception.
169+
String ioeMessage = ioe.getClass().getSimpleName() + ": " + ioe.getMessage();
170+
throw new IOException("Could not load JSON as HTML from " + src + " due to " + ioeMessage);
157171
}
158172
}
159173

test/src/test/java/hudson/model/DownloadServiceTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
package hudson.model;
22

3+
import static org.hamcrest.MatcherAssert.assertThat;
4+
import static org.hamcrest.Matchers.containsString;
35
import static org.junit.jupiter.api.Assertions.assertEquals;
46
import static org.junit.jupiter.api.Assertions.assertNotNull;
7+
import static org.junit.jupiter.api.Assertions.assertThrows;
58

69
import edu.umd.cs.findbugs.annotations.NonNull;
710
import hudson.model.DownloadService.Downloadable;
811
import hudson.tasks.Ant.AntInstaller;
912
import hudson.tasks.Maven;
1013
import hudson.tools.DownloadFromUrlInstaller;
1114
import hudson.tools.ToolInstallation;
15+
import java.io.IOException;
16+
import java.net.URI;
1217
import java.net.URL;
1318
import java.util.ArrayList;
1419
import java.util.List;
@@ -38,6 +43,26 @@ private static void assertRoots(String expected, URL resource) throws Exception
3843
assertEquals(expected, new TreeSet<>(keySet).toString());
3944
}
4045

46+
@WithoutJenkins
47+
@Test
48+
void testLoadJSONException() throws Exception {
49+
String badHostname = "bad.updates.jenkins.io"; // Non-existent host
50+
URL badURL = new URI("https://" + badHostname + "/update-center.json").toURL();
51+
IOException e = assertThrows(IOException.class, () -> DownloadService.loadJSON(badURL));
52+
assertThat(e.getMessage(), containsString(badURL.toString()));
53+
assertThat(e.getMessage(), containsString("due to UnknownHostException:"));
54+
}
55+
56+
@WithoutJenkins
57+
@Test
58+
void testLoadJSONHTMLException() throws Exception {
59+
String badHostname = "bad.updates.jenkins.io"; // Non-existent host
60+
URL badURL = new URI("https://" + badHostname + "/update-center.json").toURL();
61+
IOException e = assertThrows(IOException.class, () -> DownloadService.loadJSONHTML(badURL));
62+
assertThat(e.getMessage(), containsString(badURL.toString()));
63+
assertThat(e.getMessage(), containsString("due to UnknownHostException:"));
64+
}
65+
4166
@Test
4267
void testReduceFunctionWithMavenJsons() throws Exception {
4368
URL resource1 = DownloadServiceTest.class.getResource("hudson.tasks.Maven.MavenInstaller1.json");

0 commit comments

Comments
 (0)