LIB: improve exception handling

This commit is contained in:
hasufell 2016-04-06 04:27:02 +02:00
parent 038b0d0377
commit 17407860f4
2 changed files with 16 additions and 1 deletions

View File

@ -199,6 +199,15 @@ catchErrno en a1 a2 =
else ioError e
-- |Execute the given action and retrow IO exceptions that have the given errno.
rethrowErrnoAs :: Errno -- ^ errno to catch
-> FmIOException -- ^ rethrow as
-> IO a -- ^ action to try
-> IO a
rethrowErrnoAs en fmex action = catchErrno en action (throw fmex)
-- |Like `catchIOError`, with arguments swapped.
handleIOError :: (IOError -> IO a) -> IO a -> IO a
handleIOError a1 a2 = catchIOError a2 a1

View File

@ -52,6 +52,10 @@ import Data.Time.Clock.POSIX
, posixSecondsToUTCTime
)
import Data.Time()
import Foreign.C.Error
(
eACCES
)
import HPath
(
Abs
@ -60,6 +64,7 @@ import HPath
, pattern Path
)
import qualified HPath as P
import HSFM.FileSystem.Errors
import HSFM.Utils.MyPrelude
import Prelude hiding(readFile)
import System.IO.Error
@ -608,7 +613,8 @@ getDirsFiles' :: (Path Fn -> [Path Fn] -> [Path Fn]) -- ^ filter function
-> Path Abs -- ^ dir to read
-> IO [Path Fn]
getDirsFiles' filterf fp =
bracket (PFD.openDirStream . P.toFilePath $ fp)
rethrowErrnoAs eACCES (Can'tOpenDirectory . P.fpToString . P.fromAbs $ fp)
$ bracket (PFD.openDirStream . P.toFilePath $ fp)
PFD.closeDirStream
$ \dirstream ->
let mdirs :: [Path Fn] -> IO [Path Fn]