GTK: improve error handling on invalid paths

Fixes #28
This commit is contained in:
Julian Ospald 2016-03-31 02:29:16 +02:00
parent dd013b7d7b
commit a2e6ced69a
No known key found for this signature in database
GPG Key ID: 511B62C09D50CD28

View File

@ -36,6 +36,15 @@ import Control.Concurrent.STM
newTVarIO newTVarIO
, readTVarIO , readTVarIO
) )
import Control.Exception
(
try
, SomeException
)
import Control.Monad
(
when
)
import Data.Foldable import Data.Foldable
( (
for_ for_
@ -67,6 +76,10 @@ import System.INotify
, EventVariety(..) , EventVariety(..)
, Event(..) , Event(..)
) )
import System.IO.Error
(
tryIOError
)
@ -196,9 +209,24 @@ refreshView mygui myview mfp =
case mfp of case mfp of
Just fp -> do Just fp -> do
let mdir = fromMaybe (fromJust $ P.parseAbs "/") (P.parseAbs fp) let mdir = fromMaybe (fromJust $ P.parseAbs "/") (P.parseAbs fp)
cdir <- HSFM.FileSystem.FileType.readFileWithFileInfo mdir -- readFileWithFileInfo can just outright fail...
refreshView' mygui myview cdir ecdir <- tryIOError (HSFM.FileSystem.FileType.readFileWithFileInfo mdir)
Nothing -> refreshView' mygui myview =<< getCurrentDir myview 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. -- |Refreshes the View based on the given directory.