diff options
author | Richard Hartmann <richih@debian.org> | 2015-12-16 07:23:53 +0100 |
---|---|---|
committer | Richard Hartmann <richih@debian.org> | 2015-12-16 07:23:53 +0100 |
commit | 53679fdc47d9470c2540fbe38d5de38c91c3bb01 (patch) | |
tree | e3151adb4d04e905dbb930b67cdb959fac5ea513 /Utility/Exception.hs | |
parent | c94e89d797b15833875ebeb6adcaa6e130315f8e (diff) | |
parent | 259c511a17669d043a573344e59eae9e9956265c (diff) | |
download | git-repair-53679fdc47d9470c2540fbe38d5de38c91c3bb01.tar.gz |
Merge tag '1.20151215' into debian
tagging package git-repair version 1.20151215
Diffstat (limited to 'Utility/Exception.hs')
-rw-r--r-- | Utility/Exception.hs | 23 |
1 files changed, 17 insertions, 6 deletions
diff --git a/Utility/Exception.hs b/Utility/Exception.hs index ef3ab1d..8b110ae 100644 --- a/Utility/Exception.hs +++ b/Utility/Exception.hs @@ -1,11 +1,12 @@ {- Simple IO exception handling (and some more) - - - Copyright 2011-2014 Joey Hess <joey@kitenet.net> + - Copyright 2011-2015 Joey Hess <id@joeyh.name> - - License: BSD-2-clause -} {-# LANGUAGE ScopedTypeVariables #-} +{-# OPTIONS_GHC -fno-warn-tabs #-} module Utility.Exception ( module X, @@ -19,6 +20,8 @@ module Utility.Exception ( catchNonAsync, tryNonAsync, tryWhenExists, + catchIOErrorType, + IOErrorType(..) ) where import Control.Monad.Catch as X hiding (Handler) @@ -26,7 +29,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 -} @@ -35,10 +40,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 @@ -86,3 +88,12 @@ tryWhenExists :: MonadCatch m => m a -> m (Maybe a) tryWhenExists a = do v <- tryJust (guard . isDoesNotExistError) a return (eitherToMaybe v) + +{- Catches only IO exceptions of a particular type. + - Ie, use HardwareFault to catch disk IO errors. -} +catchIOErrorType :: MonadCatch m => IOErrorType -> (IOException -> m a) -> m a -> m a +catchIOErrorType errtype onmatchingerr a = catchIO a onlymatching + where + onlymatching e + | ioeGetErrorType e == errtype = onmatchingerr e + | otherwise = throwM e |