From a2e6ced69a3a478972b01a540589e5491d73bb3e Mon Sep 17 00:00:00 2001 From: Julian Ospald Date: Thu, 31 Mar 2016 02:29:16 +0200 Subject: [PATCH] GTK: improve error handling on invalid paths Fixes #28 --- src/HSFM/GUI/Gtk/MyView.hs | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/src/HSFM/GUI/Gtk/MyView.hs b/src/HSFM/GUI/Gtk/MyView.hs index 1b064db..2a25271 100644 --- a/src/HSFM/GUI/Gtk/MyView.hs +++ b/src/HSFM/GUI/Gtk/MyView.hs @@ -36,6 +36,15 @@ import Control.Concurrent.STM newTVarIO , readTVarIO ) +import Control.Exception + ( + try + , SomeException + ) +import Control.Monad + ( + when + ) import Data.Foldable ( for_ @@ -67,6 +76,10 @@ import System.INotify , EventVariety(..) , Event(..) ) +import System.IO.Error + ( + tryIOError + ) @@ -196,9 +209,24 @@ refreshView mygui myview mfp = case mfp of Just fp -> do let mdir = fromMaybe (fromJust $ P.parseAbs "/") (P.parseAbs fp) - cdir <- HSFM.FileSystem.FileType.readFileWithFileInfo mdir - refreshView' mygui myview cdir - Nothing -> refreshView' mygui myview =<< getCurrentDir myview + -- readFileWithFileInfo can just outright fail... + ecdir <- tryIOError (HSFM.FileSystem.FileType.readFileWithFileInfo mdir) + case ecdir of + Right cdir -> do + -- ...or return an `AnchordFile` with a Failed constructor, + -- both of which need to be handled here + if (failed . file $ cdir) + then refreshView mygui myview =<< getAlternativeDir + else refreshView' mygui myview cdir + Left e -> refreshView mygui myview =<< getAlternativeDir + Nothing -> refreshView mygui myview =<< getAlternativeDir + where + getAlternativeDir = do + ecd <- try (getCurrentDir myview) :: IO (Either SomeException + (AnchoredFile FileInfo)) + case (ecd) of + Right dir -> return (Just $ fullPathS dir) + Left _ -> return (Just "/") -- |Refreshes the View based on the given directory.