diff options
| author | Joey Hess <joeyh@joeyh.name> | 2015-09-14 20:23:08 -0400 |
|---|---|---|
| committer | Joey Hess <joeyh@joeyh.name> | 2015-09-14 20:23:08 -0400 |
| commit | e5c398a0f6dfc65d56c2dcdf2e8bbf031579ef38 (patch) | |
| tree | c76125aaf059f4acaab6a32c3cfc223e5294c787 /src/Utility/Exception.hs | |
| parent | 0f9f05ae9e65182daa9bfc98a9932e2e1382e9b5 (diff) | |
| parent | fb7b1826870c8a0e01f88da74ff2fd98a0626d5b (diff) | |
Merge branch 'joeyconfig'
Diffstat (limited to 'src/Utility/Exception.hs')
| -rw-r--r-- | src/Utility/Exception.hs | 21 |
1 files changed, 15 insertions, 6 deletions
diff --git a/src/Utility/Exception.hs b/src/Utility/Exception.hs index 9d4236c4..13000e03 100644 --- a/src/Utility/Exception.hs +++ b/src/Utility/Exception.hs @@ -1,6 +1,6 @@ {- Simple IO exception handling (and some more) - - - Copyright 2011-2014 Joey Hess <id@joeyh.name> + - Copyright 2011-2015 Joey Hess <id@joeyh.name> - - License: BSD-2-clause -} @@ -20,6 +20,7 @@ module Utility.Exception ( catchNonAsync, tryNonAsync, tryWhenExists, + catchHardwareFault, ) where import Control.Monad.Catch as X hiding (Handler) @@ -27,7 +28,9 @@ import qualified Control.Monad.Catch as M import Control.Exception (IOException, AsyncException) import Control.Monad import Control.Monad.IO.Class (liftIO, MonadIO) -import System.IO.Error (isDoesNotExistError) +import System.IO.Error (isDoesNotExistError, ioeGetErrorType) +import GHC.IO.Exception (IOErrorType(..)) + import Utility.Data {- Catches IO errors and returns a Bool -} @@ -36,10 +39,7 @@ catchBoolIO = catchDefaultIO False {- Catches IO errors and returns a Maybe -} catchMaybeIO :: MonadCatch m => m a -> m (Maybe a) -catchMaybeIO a = do - catchDefaultIO Nothing $ do - v <- a - return (Just v) +catchMaybeIO a = catchDefaultIO Nothing $ a >>= (return . Just) {- Catches IO errors and returns a default value. -} catchDefaultIO :: MonadCatch m => a -> m a -> m a @@ -87,3 +87,12 @@ tryWhenExists :: MonadCatch m => m a -> m (Maybe a) tryWhenExists a = do v <- tryJust (guard . isDoesNotExistError) a return (eitherToMaybe v) + +{- Catches only exceptions caused by hardware faults. + - Ie, disk IO error. -} +catchHardwareFault :: MonadCatch m => m a -> (IOException -> m a) -> m a +catchHardwareFault a onhardwareerr = catchIO a onlyhw + where + onlyhw e + | ioeGetErrorType e == HardwareFault = onhardwareerr e + | otherwise = throwM e |
