Skip to content

Commit b8c3fd0

Browse files
committed
Print full exception stacktrace + causes on errors
Helps to debug errors that did not have their causes printed.
1 parent 0508b34 commit b8c3fd0

File tree

1 file changed

+42
-16
lines changed
  • src/main/java/net/minecraftforge/java_provisioner

1 file changed

+42
-16
lines changed

src/main/java/net/minecraftforge/java_provisioner/Disco.java

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import java.io.FileReader;
1313
import java.io.IOException;
1414
import java.io.InputStream;
15+
import java.io.PrintWriter;
16+
import java.io.StringWriter;
1517
import java.nio.file.Files;
1618
import java.nio.file.StandardCopyOption;
1719
import java.nio.file.StandardOpenOption;
@@ -117,10 +119,22 @@ protected void debug(String message) {
117119
System.out.println(message);
118120
}
119121

122+
protected final void debug(Throwable exception) {
123+
StringWriter string = new StringWriter();
124+
exception.printStackTrace(new PrintWriter(string, true));
125+
debug(string.toString());
126+
}
127+
120128
protected void error(String message) {
121129
System.err.println(message);
122130
}
123131

132+
protected final void error(Throwable exception) {
133+
StringWriter string = new StringWriter();
134+
exception.printStackTrace(new PrintWriter(string, true));
135+
error(string.toString());
136+
}
137+
124138
public List<Package> getPackages() {
125139
File tmp = new File(cache, "packages.json");
126140
List<Package> ret = readJson(tmp, new TypeToken<List<Package>>(){});
@@ -143,15 +157,17 @@ public List<Package> getPackages() {
143157
try {
144158
data = DownloadUtils.downloadString(url);
145159
} catch (IOException e) {
146-
error("Failed to download package list from " + url + ": " + e);
160+
error("Failed to download package list from " + url);
161+
error(e);
147162
return Collections.emptyList();
148163
}
149164

150165
Response<Package> resp;
151166
try {
152167
resp = Response.of(data, Package.class);
153168
} catch (Exception e) {
154-
error("Failed to parse package list from " + url + ": " + e);
169+
error("Failed to parse package list from " + url);
170+
error(e);
155171
return Collections.emptyList();
156172
}
157173

@@ -163,7 +179,8 @@ public List<Package> getPackages() {
163179
try {
164180
writeJson(tmp, resp.entries(), List.class);
165181
} catch (IOException e) {
166-
error("Failed to write package list to " + tmp.getAbsolutePath() + ": " + e);
182+
error("Failed to write package list to " + tmp.getAbsolutePath());
183+
error(e);
167184
return Collections.emptyList();
168185
}
169186

@@ -235,15 +252,17 @@ public List<Package> getPackages(int version, OS os, Distro distro, Arch arch) {
235252
try {
236253
data = DownloadUtils.downloadString(url);
237254
} catch (IOException e) {
238-
error("Failed to download package info from " + url + " : " + e);
255+
error("Failed to download package info from " + url);
256+
error(e);
239257
return null;
240258
}
241259

242260
Response<PackageInfo> resp;
243261
try {
244262
resp = Response.of(data, PackageInfo.class);
245263
} catch (Exception e) {
246-
error("Failed to parse package info from " + url + " : " + e);
264+
error("Failed to parse package info from " + url);
265+
error(e);
247266
return null;
248267
}
249268

@@ -258,7 +277,8 @@ public List<Package> getPackages(int version, OS os, Distro distro, Arch arch) {
258277
try {
259278
writeJson(tmp, ret, DownloadInfo.class);
260279
} catch (IOException e) {
261-
error("Failed to write package info to " + tmp.getAbsolutePath() + " : " + e);
280+
error("Failed to write package info to " + tmp.getAbsolutePath());
281+
error(e);
262282
return null;
263283
}
264284

@@ -290,14 +310,16 @@ public List<Package> getPackages(int version, OS os, Distro distro, Arch arch) {
290310
else
291311
debug("Unknown Checksum " + checksum);
292312
} catch (IOException e) {
293-
error("Failed to download checksum from " + info.checksum_uri + " : " + e);
313+
error("Failed to download checksum from " + info.checksum_uri);
314+
error(e);
294315
}
295316
}
296317

297318
if (info.direct_download_uri != null)
298319
download = info.direct_download_uri;
299320
} catch (Exception e) {
300-
debug("Failed to download package info for \"" + pkg.filename + "\" (" + pkg.id + ") , assuming redirect link is valid : " + e);
321+
debug("Failed to download package info for \"" + pkg.filename + "\" (" + pkg.id + ") , assuming redirect link is valid");
322+
debug(e);
301323
}
302324

303325
File archive = new File(cache, pkg.filename);
@@ -313,8 +335,8 @@ public List<Package> getPackages(int version, OS os, Distro distro, Arch arch) {
313335
try {
314336
DownloadUtils.downloadFile(archive, download);
315337
} catch (Exception e) {
316-
String message = "Failed to download " + pkg.filename + " from " + download;
317-
error(message);
338+
error("Failed to download " + pkg.filename + " from " + download);
339+
error(e);
318340
return null;
319341
}
320342
}
@@ -335,8 +357,8 @@ public List<Package> getPackages(int version, OS os, Distro distro, Arch arch) {
335357
return null;
336358
}
337359
} catch (Exception e) {
338-
String message = "Failed to calculate " + func.name() + " checksum";
339-
error(message + " : " + e);
360+
error("Failed to calculate " + func.name() + " checksum");
361+
error(e);
340362
return null;
341363
}
342364
}
@@ -438,7 +460,8 @@ private void extractZip(String exeName, File archive, File target) {
438460
return;
439461
}
440462
} catch (IOException e) {
441-
error(" Failed to extract zip file: " + archive.getName() + " : " + e);
463+
error(" Failed to extract zip file: " + archive.getName());
464+
error(e);
442465
}
443466
}
444467

@@ -560,7 +583,8 @@ private void extractTar(String exeName, File archive, File target, OS os, boolea
560583
}
561584
}
562585
} catch (IOException e) {
563-
error(" Failed to read tar file: " + archive + " : " + e);
586+
error(" Failed to read tar file: " + archive);
587+
error(e);
564588
return;
565589
}
566590

@@ -577,7 +601,8 @@ private void extractTar(String exeName, File archive, File target, OS os, boolea
577601
return;
578602
}
579603
} catch (IOException e) {
580-
error(" Failed to extract: " + archive + " : " + e);
604+
error(" Failed to extract: " + archive);
605+
error(e);
581606
}
582607
}
583608

@@ -595,7 +620,8 @@ private void extractTar(String exeName, File archive, File target, OS os, boolea
595620
try (FileReader reader = new FileReader(input)) {
596621
return GSON.fromJson(new JsonReader(reader), type);
597622
} catch (Exception e) {
598-
error("Failed to read cache file: " + input + " : " + e);
623+
error("Failed to read cache file: " + input);
624+
error(e);
599625
return null;
600626
}
601627
}

0 commit comments

Comments
 (0)