From 803cc369438f7850e6245ee2e4253dc480546bf4 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Thu, 11 Aug 2016 17:45:36 -0400 Subject: catch IO exceptions --- Storage/LocalFiles.hs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'Storage') diff --git a/Storage/LocalFiles.hs b/Storage/LocalFiles.hs index a9496da..d339774 100644 --- a/Storage/LocalFiles.hs +++ b/Storage/LocalFiles.hs @@ -20,6 +20,7 @@ import System.Posix import System.FilePath import Raaz.Core.Encode import Control.DeepSeq +import Control.Exception localFiles :: Storage localFiles = Storage @@ -28,7 +29,7 @@ localFiles = Storage } store :: StorableObjectIdent -> Shard -> IO StoreResult -store i s = do +store i s = onError (StoreFailure . show) $ do dir <- shardDir createDirectoryIfMissing True dir fd <- openFd (dir shardFile i) WriteOnly (Just 0o666) @@ -39,7 +40,7 @@ store i s = do return StoreSuccess retrieve :: ShardNum -> StorableObjectIdent -> IO RetrieveResult -retrieve n i = do +retrieve n i = onError (RetrieveFailure . show) $ do dir <- shardDir fd <- openFd (dir shardFile i) ReadOnly Nothing defaultFileFlags h <- fdToHandle fd @@ -47,6 +48,13 @@ retrieve n i = do b `deepseq` hClose h return $ RetrieveSuccess $ Shard n (StorableObject b) +onError :: (IOException -> a) -> IO a -> IO a +onError f a = do + v <- try a + return $ case v of + Left e -> f e + Right r -> r + shardDir :: IO FilePath shardDir = do u <- getUserEntryForID =<< getEffectiveUserID -- cgit v1.2.3