From 1711bd35325e4ab4b1984e6544135394cecff0df Mon Sep 17 00:00:00 2001 From: Ben Soares Date: Tue, 14 May 2024 15:26:51 +0100 Subject: [PATCH] JAL-4414 Add a FileNotFoundException when identifying URL file formats and catch to change error message appropriately --- src/jalview/bin/Commands.java | 29 +++++++++++++++++++---------- src/jalview/bin/Jalview.java | 13 ++++++++++--- src/jalview/gui/CutAndPasteTransfer.java | 4 ++++ src/jalview/gui/Desktop.java | 8 ++++++-- src/jalview/io/FileParse.java | 23 ++++++++++++++++++++--- src/jalview/io/IdentifyFile.java | 19 +++++++++++++++++-- test/jalview/io/IdentifyFileTest.java | 7 +++++-- 7 files changed, 81 insertions(+), 22 deletions(-) diff --git a/src/jalview/bin/Commands.java b/src/jalview/bin/Commands.java index b6a5a25..5ef32c8 100644 --- a/src/jalview/bin/Commands.java +++ b/src/jalview/bin/Commands.java @@ -22,6 +22,7 @@ package jalview.bin; import java.awt.Color; import java.io.File; +import java.io.FileNotFoundException; import java.io.IOException; import java.net.URISyntaxException; import java.util.ArrayList; @@ -286,6 +287,10 @@ public class Commands try { format = new IdentifyFile().identify(openFile, protocol); + } catch (FileNotFoundException e0) + { + addError((protocol == DataSourceType.URL ? "File at URL" : "File") + + " '" + openFile + "' not found"); } catch (FileFormatException e1) { addError("Unknown file format for '" + openFile + "'"); @@ -1301,18 +1306,22 @@ public class Commands private void colourAlignFrame(AlignFrame af, ColourSchemeI cs) { - try { - SwingUtilities.invokeAndWait(new Runnable() + try { - @Override - public void run() + SwingUtilities.invokeAndWait(new Runnable() { - // Note that cs == null removes colour scheme from af - af.changeColour(cs); - } - }); } catch (Exception x) { - Console.trace("Interrupted whilst waiting for colorAlignFrame action",x); - + @Override + public void run() + { + // Note that cs == null removes colour scheme from af + af.changeColour(cs); + } + }); + } catch (Exception x) + { + Console.trace("Interrupted whilst waiting for colorAlignFrame action", + x); + } } diff --git a/src/jalview/bin/Jalview.java b/src/jalview/bin/Jalview.java index e343b0f..d8e149d 100755 --- a/src/jalview/bin/Jalview.java +++ b/src/jalview/bin/Jalview.java @@ -23,6 +23,7 @@ package jalview.bin; import java.awt.Color; import java.io.BufferedReader; import java.io.File; +import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; @@ -938,9 +939,12 @@ public class Jalview implements JalviewObjectI try { format = new IdentifyFile().identify(file, protocol); - } catch (FileFormatException e1) + } catch (FileNotFoundException e) { - // TODO ? + Console.error("File at '" + file + "' not found", e); + } catch (FileFormatException e) + { + Console.error("File '" + file + "' format not recognised", e); } AlignFrame af = fileLoader.LoadFileWaitTillLoaded(file, protocol, @@ -1211,9 +1215,12 @@ public class Jalview implements JalviewObjectI try { format = new IdentifyFile().identify(file, protocol); + } catch (FileNotFoundException e) + { + Console.error("File at '" + file + "' not found", e); } catch (FileFormatException e) { - // TODO what? + Console.error("File '" + file + "' format not recognised", e); } } diff --git a/src/jalview/gui/CutAndPasteTransfer.java b/src/jalview/gui/CutAndPasteTransfer.java index bc3c0d2..91f8189 100644 --- a/src/jalview/gui/CutAndPasteTransfer.java +++ b/src/jalview/gui/CutAndPasteTransfer.java @@ -28,6 +28,7 @@ import java.awt.datatransfer.Transferable; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; +import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; @@ -226,6 +227,9 @@ public class CutAndPasteTransfer extends GCutAndPasteTransfer try { format = new IdentifyFile().identify(text, DataSourceType.PASTE); + } catch (FileNotFoundException e0) + { + // this won't happen } catch (FileFormatException e1) { // leave as null diff --git a/src/jalview/gui/Desktop.java b/src/jalview/gui/Desktop.java index bbd4dae..4c30a06 100644 --- a/src/jalview/gui/Desktop.java +++ b/src/jalview/gui/Desktop.java @@ -54,6 +54,7 @@ import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import java.beans.PropertyVetoException; import java.io.File; +import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.lang.reflect.Field; @@ -1400,10 +1401,13 @@ public class Desktop extends jalview.jbgui.GDesktop try { format = new IdentifyFile().identify(url, DataSourceType.URL); + } catch (FileNotFoundException e) + { + jalview.bin.Console.error("URL '" + url + "' not found", e); } catch (FileFormatException e) { - // TODO revise error handling, distinguish between - // URL not found and response not valid + jalview.bin.Console.error( + "File at URL '" + url + "' format not recognised", e); } if (format == null) diff --git a/src/jalview/io/FileParse.java b/src/jalview/io/FileParse.java index 76aa2fa..ca0588a 100755 --- a/src/jalview/io/FileParse.java +++ b/src/jalview/io/FileParse.java @@ -426,7 +426,7 @@ public class FileParse * @throws IOException */ public FileParse(Object file, DataSourceType sourceType) - throws MalformedURLException, IOException + throws MalformedURLException, FileNotFoundException, IOException { if (file instanceof File) { @@ -439,11 +439,12 @@ public class FileParse } private void parse(File file, String fileStr, DataSourceType sourceType, - boolean isFileObject) throws IOException + boolean isFileObject) throws FileNotFoundException, IOException { bytes = Platform.getFileBytes(file); dataSourceType = sourceType; error = false; + boolean filenotfound = false; if (sourceType == DataSourceType.FILE) { @@ -502,6 +503,11 @@ public class FileParse String suffixLess = extractSuffix(fileStr); if (suffixLess == null) { + if (e instanceof FileNotFoundException) + { + errormessage = "File at URL '" + fileStr + "' not found"; + filenotfound = true; + } throw (e); } else @@ -511,7 +517,12 @@ public class FileParse checkURLSource(suffixLess); } catch (IOException e2) { - errormessage = "BAD URL WITH OR WITHOUT SUFFIX"; + errormessage = "BAD URL WITH OR WITHOUT SUFFIX '" + fileStr + + "'"; + if (e instanceof FileNotFoundException) + { + filenotfound = true; + } throw (e); // just pass back original - everything was wrong. } } @@ -560,6 +571,12 @@ public class FileParse if (dataIn == null || error) { // pass up the reason why we have no source to read from + if (filenotfound) + { + throw new FileNotFoundException(MessageManager + .formatMessage("label.url_not_found", new String[] + { errormessage })); + } throw new IOException(MessageManager.formatMessage( "exception.failed_to_read_data_from_source", new String[] { errormessage })); diff --git a/src/jalview/io/IdentifyFile.java b/src/jalview/io/IdentifyFile.java index 00e2872..8251c22 100755 --- a/src/jalview/io/IdentifyFile.java +++ b/src/jalview/io/IdentifyFile.java @@ -21,6 +21,7 @@ package jalview.io; import java.io.File; +import java.io.FileNotFoundException; import java.io.IOException; import java.util.Locale; @@ -38,7 +39,7 @@ public class IdentifyFile private static final String XMLHEADER = ""; public FileFormatI identify(Object file, DataSourceType protocol) - throws FileFormatException + throws FileFormatException, FileNotFoundException { // BH 2018 return (file instanceof File ? identify((File) file, protocol) @@ -83,10 +84,11 @@ public class IdentifyFile * @throws FileFormatException */ public FileFormatI identify(String file, DataSourceType sourceType) - throws FileFormatException + throws FileFormatException, FileNotFoundException { String emessage = "UNIDENTIFIED FILE PARSING ERROR"; FileParse parser = null; + FileNotFoundException fnf = null; try { parser = new FileParse(file, sourceType); @@ -94,6 +96,11 @@ public class IdentifyFile { return identify(parser); } + } catch (FileNotFoundException e) + { + fnf = e; + emessage = "Could not find '" + file + "'"; + Console.error("Could not find '" + file + "'", e); } catch (IOException e) { Console.error("Error whilst trying to read " + file, e); @@ -106,6 +113,10 @@ public class IdentifyFile { throw new FileFormatException(parser.errormessage); } + if (fnf != null) + { + throw (fnf); + } throw new FileFormatException(emessage); } @@ -497,6 +508,10 @@ public class IdentifyFile try { type = ider.identify(args[i], DataSourceType.FILE); + } catch (FileNotFoundException e) + { + Console.error(String.format("Error '%s' fetching file %s", args[i], + e.getMessage())); } catch (FileFormatException e) { Console.error( diff --git a/test/jalview/io/IdentifyFileTest.java b/test/jalview/io/IdentifyFileTest.java index 68c099e..6ee3f0e 100644 --- a/test/jalview/io/IdentifyFileTest.java +++ b/test/jalview/io/IdentifyFileTest.java @@ -24,6 +24,8 @@ import static org.testng.AssertJUnit.assertFalse; import static org.testng.AssertJUnit.assertSame; import static org.testng.AssertJUnit.assertTrue; +import java.io.FileNotFoundException; + import org.testng.Assert; import org.testng.annotations.BeforeClass; import org.testng.annotations.DataProvider; @@ -43,7 +45,7 @@ public class IdentifyFileTest @Test(groups = { "Functional" }, dataProvider = "identifyFiles") public void testIdentify(String data, FileFormatI expectedFileType) - throws FileFormatException + throws FileFormatException, FileNotFoundException { DataSourceType protocol = DataSourceType.FILE; IdentifyFile ider = new IdentifyFile(); @@ -58,7 +60,8 @@ public class IdentifyFileTest * @throws FileFormatException */ @Test(groups = "Functional") - public void testIdentify_featureFile() throws FileFormatException + public void testIdentify_featureFile() + throws FileFormatException, FileNotFoundException { IdentifyFile ider = new IdentifyFile(); -- 1.7.10.2