Skip to content

Commit

Permalink
Clean up exception handling. Fixes yarl#176
Browse files Browse the repository at this point in the history
  • Loading branch information
tfmorris committed Jul 11, 2023
1 parent ae10043 commit ff422c9
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
6 changes: 6 additions & 0 deletions src/pattypan/PattypanException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package pattypan;

public class PattypanException extends Exception {
public PattypanException(String empty_path) {
}
}
6 changes: 3 additions & 3 deletions src/pattypan/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ public static String getFileChecksum(MessageDigest digest, File file) throws IOE
return sb.toString();
}

public static String readUrl(String urlString) throws Exception {
public static String readUrl(String urlString) throws IOException {
try {
HttpClient client = HttpClient.newHttpClient();

Expand All @@ -277,8 +277,8 @@ public static String readUrl(String urlString) throws Exception {

var response = client.send(request, BodyHandlers.ofString());
return response.body();
} catch (Exception e) {
throw new Exception("Error while getting JSON from " + urlString);
} catch (IOException | InterruptedException e) {
throw new IOException("Error while getting JSON from " + urlString, e);
}
}

Expand Down
27 changes: 14 additions & 13 deletions src/pattypan/panes/LoadPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.read.biff.BiffException;
import pattypan.PattypanException;
import pattypan.Session;
import pattypan.Settings;
import pattypan.UploadElement;
Expand Down Expand Up @@ -152,23 +153,23 @@ private void addFilesToUpload(ArrayList<Map<String, String>> descriptions, Templ

try {
if (description.get("path").isEmpty()) {
throw new Exception("empty path");
throw new PattypanException("empty path");
}
if (description.get("name").isEmpty()) {
throw new Exception("empty name");
throw new PattypanException("empty name");
}
if (description.get("path").startsWith("https://") || description.get("path").startsWith("http://")) {
if (!Util.validUrl(description.get("path"))) {
throw new Exception("invalid URL");
throw new PattypanException("invalid URL");
}

// when uploaded from URL the extension is not automatically added
if (!Util.hasValidFileExtension(description.get("name"))) {
throw new Exception("filename does not include a valid file extension");
throw new PattypanException("filename does not include a valid file extension");
}
} else {
if (!checkIfFileExist(description.get("path"))) {
throw new Exception("file not found");
throw new PattypanException("file not found");
}
}

Expand All @@ -177,7 +178,7 @@ private void addFilesToUpload(ArrayList<Map<String, String>> descriptions, Templ
}

if (Util.hasInvalidFilenameCharacters(description.get("name"))) {
throw new Exception(description.get("name") + ": filename shouldn't contain invalid characters (#, ], {, etc)");
throw new PattypanException(description.get("name") + ": filename shouldn't contain invalid characters (#, ], {, etc)");
}

Set<String> keys = Util.getKeysByValue(description, "");
Expand All @@ -195,12 +196,12 @@ private void addFilesToUpload(ArrayList<Map<String, String>> descriptions, Templ
}

if (wikicode.isEmpty()) {
throw new Exception("Error: empty template!");
throw new PattypanException("Error: empty template!");
}

Session.FILES_TO_UPLOAD.add(new UploadElement(description, wikicode));

} catch (Exception ex) {
} catch (Throwable ex) {
errors.add(namePath + " " + ex.getMessage());
}
}
Expand Down Expand Up @@ -243,10 +244,10 @@ private void readHeaders(Sheet sheet) throws Exception {
}

if (cols.isEmpty()) {
throw new Exception("Header error: columns not found!");
throw new PattypanException("Header error: columns not found!");
}
if (!cols.contains("path") || !cols.contains("name")) {
throw new Exception("Header error: found " + cols.size() + " headers but 'path' and/or 'name' headers are missing");
throw new PattypanException("Header error: found " + cols.size() + " headers but 'path' and/or 'name' headers are missing");
}
}

Expand Down Expand Up @@ -298,7 +299,7 @@ private void loadSpreadsheet(File file) {
addInfo("File error: file needs to be saved in binnary format. Please save your file in \"Excel 97-2003 format\"");
} catch (InvalidReferenceException ex) {
addInfo("File error: variables mismatch. Column headers variables must match wikitemplate variables.");
} catch (Exception ex) {
} catch (Throwable ex) {
addInfo(ex.getMessage());
}
}
Expand Down Expand Up @@ -347,7 +348,7 @@ private void readSpreadSheet() throws BiffException, IOException, Exception {
readHeaders(dataSheet);
addFilesToUpload(readDescriptions(dataSheet), readTemplate(templateSheet));
} catch (IndexOutOfBoundsException ex) {
throw new Exception("Error: your spreadsheet should have minimum two tabs.");
throw new PattypanException("Error: your spreadsheet should have minimum two tabs.");
}

reloadButton.setDisable(false);
Expand All @@ -365,7 +366,7 @@ private Template readTemplate(Sheet sheet) throws Exception {
String text = sheet.getCell(0, 0).getContents();
return new Template("wikitemplate", new StringReader(text), cfg);
} catch (ArrayIndexOutOfBoundsException ex) {
throw new Exception("Error: template in spreadsheet looks empty. Check if wikitemplate is present in second tab of your spreadsheet (first row and first column).");
throw new PattypanException("Error: template in spreadsheet looks empty. Check if wikitemplate is present in second tab of your spreadsheet (first row and first column).");
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/pattypan/panes/StartPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ private void checkVersion() {
}
} catch (UnknownHostException ex) {
Session.LOGGER.log(Level.INFO, "No internet connection found");
} catch (Exception ex) {
} catch (Throwable ex) {
Session.LOGGER.log(Level.SEVERE, null, ex);
}
}
Expand Down

0 comments on commit ff422c9

Please sign in to comment.