implment catchHardwareFault

This commit is contained in:
Joey Hess 2015-05-27 16:36:54 -04:00
parent 30960c0465
commit ff629a1ec0

View file

@ -1,6 +1,6 @@
{- Simple IO exception handling (and some more) {- 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 - License: BSD-2-clause
-} -}
@ -20,6 +20,7 @@ module Utility.Exception (
catchNonAsync, catchNonAsync,
tryNonAsync, tryNonAsync,
tryWhenExists, tryWhenExists,
catchHardwareFault,
) where ) where
import Control.Monad.Catch as X hiding (Handler) 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.Exception (IOException, AsyncException)
import Control.Monad import Control.Monad
import Control.Monad.IO.Class (liftIO, MonadIO) 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 import Utility.Data
{- Catches IO errors and returns a Bool -} {- Catches IO errors and returns a Bool -}
@ -87,3 +90,12 @@ tryWhenExists :: MonadCatch m => m a -> m (Maybe a)
tryWhenExists a = do tryWhenExists a = do
v <- tryJust (guard . isDoesNotExistError) a v <- tryJust (guard . isDoesNotExistError) a
return (eitherToMaybe v) 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