Skip to content

Commit 8fc040a

Browse files
committed
On download failure, re-throw with URL info
1 parent 2645140 commit 8fc040a

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

download-utils/src/java11/java/net/minecraftforge/util/download/DownloadUtils.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,8 @@ private static InputStream connect(String address) throws IOException, Interrupt
107107
public static String downloadString(String url) throws IOException {
108108
try (InputStream stream = connect(url)) {
109109
return new String(stream.readAllBytes(), StandardCharsets.UTF_8);
110-
} catch (IOException e) {
111-
throw e;
112110
} catch (Exception e) {
113-
throw new IOException(e);
111+
throw new DownloadFailedException(url, e);
114112
}
115113
}
116114

@@ -126,7 +124,6 @@ public static String downloadString(String url) throws IOException {
126124
return downloadString(url);
127125
} catch (IOException e) {
128126
if (!silent) {
129-
Log.warn("Failed to download " + url);
130127
e.printStackTrace(Log.WARN);
131128
}
132129

@@ -160,10 +157,8 @@ public static void downloadFile(boolean silent, File target, String url) throws
160157
Path path = target.toPath();
161158
Files.createDirectories(path.getParent());
162159
Files.copy(stream, path, StandardCopyOption.REPLACE_EXISTING);
163-
} catch (IOException e) {
164-
throw e;
165160
} catch (Exception e) {
166-
throw new IOException(e);
161+
throw new DownloadFailedException(url, e);
167162
}
168163
}
169164

@@ -180,11 +175,16 @@ public static boolean tryDownloadFile(boolean silent, File target, String url) {
180175
return true;
181176
} catch (IOException e) {
182177
if (!silent) {
183-
Log.warn("Failed to download " + url);
184178
e.printStackTrace(Log.WARN);
185179
}
186180

187181
return false;
188182
}
189183
}
184+
185+
private static final class DownloadFailedException extends IOException {
186+
public DownloadFailedException(String url, Throwable cause) {
187+
super("Failed to download " + url, cause);
188+
}
189+
}
190190
}

download-utils/src/main/java/net/minecraftforge/util/download/DownloadUtils.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ public static String downloadString(String url) throws IOException {
111111
}
112112

113113
return new String(out.toByteArray(), StandardCharsets.UTF_8);
114+
} catch (Exception e) {
115+
throw new DownloadFailedException(url, e);
114116
}
115117
}
116118

@@ -126,7 +128,6 @@ public static String downloadString(String url) throws IOException {
126128
return downloadString(url);
127129
} catch (IOException e) {
128130
if (!silent) {
129-
Log.warn("Failed to download " + url);
130131
e.printStackTrace(Log.WARN);
131132
}
132133

@@ -160,10 +161,8 @@ public static void downloadFile(boolean silent, File target, String url) throws
160161
Path path = target.toPath();
161162
Files.createDirectories(path.getParent());
162163
Files.copy(stream, path, StandardCopyOption.REPLACE_EXISTING);
163-
} catch (IOException e) {
164-
throw e;
165-
} catch (Exception e) {
166-
throw new IOException(e);
164+
} catch (DownloadFailedException e) {
165+
throw new IOException(url, e);
167166
}
168167
}
169168

@@ -180,11 +179,16 @@ public static boolean tryDownloadFile(boolean silent, File target, String url) {
180179
return true;
181180
} catch (IOException e) {
182181
if (!silent) {
183-
Log.warn("Failed to download " + url);
184182
e.printStackTrace(Log.WARN);
185183
}
186184

187185
return false;
188186
}
189187
}
188+
189+
private static final class DownloadFailedException extends IOException {
190+
public DownloadFailedException(String url, Throwable cause) {
191+
super("Failed to download " + url, cause);
192+
}
193+
}
190194
}

0 commit comments

Comments
 (0)