Null safety, better exception handling, check strm

This commit is contained in:
comp500 2019-06-08 14:31:52 +01:00
parent c181a36edc
commit 8be5cb8e60
No known key found for this signature in database
GPG Key ID: 214C822FFEC586B5
4 changed files with 13 additions and 6 deletions

View File

@ -3,6 +3,7 @@ package link.infra.packwiz.installer;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileReader; import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.net.URI; import java.net.URI;
import java.nio.file.Paths; import java.nio.file.Paths;
@ -101,7 +102,11 @@ public class UpdateManager {
ui.submitProgress(new InstallProgress("Loading pack file...")); ui.submitProgress(new InstallProgress("Loading pack file..."));
HashInputStream packFileStream; HashInputStream packFileStream;
try { try {
packFileStream = new HashInputStream(HandlerManager.getFileInputStream(opts.downloadURI)); InputStream stream = HandlerManager.getFileInputStream(opts.downloadURI);
if (stream == null) {
throw new Exception("Pack file URI is invalid, is it supported?");
}
packFileStream = new HashInputStream(stream);
} catch (Exception e) { } catch (Exception e) {
ui.handleExceptionAndExit(e); ui.handleExceptionAndExit(e);
return; return;

View File

@ -70,10 +70,10 @@ public class RequestHandlerGithub extends RequestHandlerZip {
@Override @Override
public boolean matchesHandler(URI loc) { public boolean matchesHandler(URI loc) {
String scheme = loc.getScheme(); String scheme = loc.getScheme();
if (!(scheme.equals("http") || scheme.equals("https"))) { if (!("http".equals(scheme) || "https".equals(scheme))) {
return false; return false;
} }
if (!loc.getHost().equals("github.com")) { if (!"github.com".equals(loc.getHost())) {
return false; return false;
} }
// TODO: sanity checks, support for more github urls // TODO: sanity checks, support for more github urls

View File

@ -11,7 +11,7 @@ public class RequestHandlerHTTP implements IRequestHandler {
@Override @Override
public boolean matchesHandler(URI loc) { public boolean matchesHandler(URI loc) {
String scheme = loc.getScheme(); String scheme = loc.getScheme();
return scheme.equals("http") || scheme.equals("https"); return "http".equals(scheme) || "https".equals(scheme);
} }
@Override @Override

View File

@ -100,20 +100,22 @@ public class InstallWindow implements IUserInterface {
@Override @Override
public void handleException(Exception e) { public void handleException(Exception e) {
e.printStackTrace();
EventQueue.invokeLater(new Runnable() { EventQueue.invokeLater(new Runnable() {
public void run() { public void run() {
JOptionPane.showMessageDialog(null, e.getMessage(), title, JOptionPane.ERROR_MESSAGE); JOptionPane.showMessageDialog(null, "An error occurred: \n" + e.getClass().getCanonicalName() + ": " + e.getMessage(), title, JOptionPane.ERROR_MESSAGE);
} }
}); });
} }
@Override @Override
public void handleExceptionAndExit(Exception e) { public void handleExceptionAndExit(Exception e) {
e.printStackTrace();
// Used to prevent the done() handler of SwingWorker executing if the invokeLater hasn't happened yet // Used to prevent the done() handler of SwingWorker executing if the invokeLater hasn't happened yet
aboutToCrash.set(true); aboutToCrash.set(true);
EventQueue.invokeLater(new Runnable() { EventQueue.invokeLater(new Runnable() {
public void run() { public void run() {
JOptionPane.showMessageDialog(null, e.getMessage(), title, JOptionPane.ERROR_MESSAGE); JOptionPane.showMessageDialog(null, "A fatal error occurred: \n" + e.getClass().getCanonicalName() + ": " + e.getMessage(), title, JOptionPane.ERROR_MESSAGE);
System.exit(1); System.exit(1);
} }
}); });