From ff422c96d4c2166739514f1ee232b40280dfbf57 Mon Sep 17 00:00:00 2001 From: Tom Morris Date: Tue, 11 Jul 2023 13:52:32 -0400 Subject: [PATCH] Clean up exception handling. Fixes #176 --- src/pattypan/PattypanException.java | 6 ++++++ src/pattypan/Util.java | 6 +++--- src/pattypan/panes/LoadPane.java | 27 ++++++++++++++------------- src/pattypan/panes/StartPane.java | 2 +- 4 files changed, 24 insertions(+), 17 deletions(-) create mode 100644 src/pattypan/PattypanException.java diff --git a/src/pattypan/PattypanException.java b/src/pattypan/PattypanException.java new file mode 100644 index 0000000..db2e355 --- /dev/null +++ b/src/pattypan/PattypanException.java @@ -0,0 +1,6 @@ +package pattypan; + +public class PattypanException extends Exception { + public PattypanException(String empty_path) { + } +} diff --git a/src/pattypan/Util.java b/src/pattypan/Util.java index e793e3f..16113c5 100644 --- a/src/pattypan/Util.java +++ b/src/pattypan/Util.java @@ -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(); @@ -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); } } diff --git a/src/pattypan/panes/LoadPane.java b/src/pattypan/panes/LoadPane.java index cbf9b70..245a179 100644 --- a/src/pattypan/panes/LoadPane.java +++ b/src/pattypan/panes/LoadPane.java @@ -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; @@ -152,23 +153,23 @@ private void addFilesToUpload(ArrayList> 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"); } } @@ -177,7 +178,7 @@ private void addFilesToUpload(ArrayList> 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 keys = Util.getKeysByValue(description, ""); @@ -195,12 +196,12 @@ private void addFilesToUpload(ArrayList> 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()); } } @@ -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"); } } @@ -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()); } } @@ -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); @@ -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)."); } } diff --git a/src/pattypan/panes/StartPane.java b/src/pattypan/panes/StartPane.java index dbd84aa..655d3ad 100644 --- a/src/pattypan/panes/StartPane.java +++ b/src/pattypan/panes/StartPane.java @@ -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); } }