generalized using the extensions package
This commit is contained in:
parent
2dd8dab314
commit
8e3d62dd5d
1 changed files with 29 additions and 20 deletions
|
@ -1,6 +1,6 @@
|
||||||
{- Simple IO exception handling (and some more)
|
{- Simple IO exception handling (and some more)
|
||||||
-
|
-
|
||||||
- Copyright 2011-2012 Joey Hess <joey@kitenet.net>
|
- Copyright 2011-2014 Joey Hess <joey@kitenet.net>
|
||||||
-
|
-
|
||||||
- License: BSD-2-clause
|
- License: BSD-2-clause
|
||||||
-}
|
-}
|
||||||
|
@ -9,51 +9,60 @@
|
||||||
|
|
||||||
module Utility.Exception where
|
module Utility.Exception where
|
||||||
|
|
||||||
import Control.Exception
|
import Control.Exception (IOException, AsyncException)
|
||||||
import qualified Control.Exception as E
|
import Control.Monad.Catch
|
||||||
import Control.Applicative
|
|
||||||
import Control.Monad
|
import Control.Monad
|
||||||
import System.IO.Error (isDoesNotExistError)
|
import System.IO.Error (isDoesNotExistError)
|
||||||
import Utility.Data
|
import Utility.Data
|
||||||
|
|
||||||
{- Catches IO errors and returns a Bool -}
|
{- Catches IO errors and returns a Bool -}
|
||||||
catchBoolIO :: IO Bool -> IO Bool
|
catchBoolIO :: MonadCatch m => m Bool -> m Bool
|
||||||
catchBoolIO = catchDefaultIO False
|
catchBoolIO = catchDefaultIO False
|
||||||
|
|
||||||
{- Catches IO errors and returns a Maybe -}
|
{- Catches IO errors and returns a Maybe -}
|
||||||
catchMaybeIO :: IO a -> IO (Maybe a)
|
catchMaybeIO :: MonadCatch m => m a -> m (Maybe a)
|
||||||
catchMaybeIO a = catchDefaultIO Nothing $ Just <$> a
|
catchMaybeIO a = do
|
||||||
|
catchDefaultIO Nothing $ do
|
||||||
|
v <- a
|
||||||
|
return (Just v)
|
||||||
|
|
||||||
{- Catches IO errors and returns a default value. -}
|
{- Catches IO errors and returns a default value. -}
|
||||||
catchDefaultIO :: a -> IO a -> IO a
|
catchDefaultIO :: MonadCatch m => a -> m a -> m a
|
||||||
catchDefaultIO def a = catchIO a (const $ return def)
|
catchDefaultIO def a = catchIO a (const $ return def)
|
||||||
|
|
||||||
{- Catches IO errors and returns the error message. -}
|
{- Catches IO errors and returns the error message. -}
|
||||||
catchMsgIO :: IO a -> IO (Either String a)
|
catchMsgIO :: MonadCatch m => m a -> m (Either String a)
|
||||||
catchMsgIO a = either (Left . show) Right <$> tryIO a
|
catchMsgIO a = do
|
||||||
|
v <- tryIO a
|
||||||
|
return $ either (Left . show) Right v
|
||||||
|
|
||||||
{- catch specialized for IO errors only -}
|
{- catch specialized for IO errors only -}
|
||||||
catchIO :: IO a -> (IOException -> IO a) -> IO a
|
catchIO :: MonadCatch m => m a -> (IOException -> m a) -> m a
|
||||||
catchIO = E.catch
|
catchIO = catch
|
||||||
|
|
||||||
{- try specialized for IO errors only -}
|
{- try specialized for IO errors only -}
|
||||||
tryIO :: IO a -> IO (Either IOException a)
|
tryIO :: MonadCatch m => m a -> m (Either IOException a)
|
||||||
tryIO = try
|
tryIO = try
|
||||||
|
|
||||||
{- Catches all exceptions except for async exceptions.
|
{- Catches all exceptions except for async exceptions.
|
||||||
- This is often better to use than catching them all, so that
|
- This is often better to use than catching them all, so that
|
||||||
- ThreadKilled and UserInterrupt get through.
|
- ThreadKilled and UserInterrupt get through.
|
||||||
-}
|
-}
|
||||||
catchNonAsync :: IO a -> (SomeException -> IO a) -> IO a
|
catchNonAsync :: MonadCatch m => m a -> (SomeException -> m a) -> m a
|
||||||
catchNonAsync a onerr = a `catches`
|
catchNonAsync a onerr = a `catches`
|
||||||
[ Handler (\ (e :: AsyncException) -> throw e)
|
[ Handler (\ (e :: AsyncException) -> throwM e)
|
||||||
, Handler (\ (e :: SomeException) -> onerr e)
|
, Handler (\ (e :: SomeException) -> onerr e)
|
||||||
]
|
]
|
||||||
|
|
||||||
tryNonAsync :: IO a -> IO (Either SomeException a)
|
tryNonAsync :: MonadCatch m => m a -> m (Either SomeException a)
|
||||||
tryNonAsync a = (Right <$> a) `catchNonAsync` (return . Left)
|
tryNonAsync a = go `catchNonAsync` (return . Left)
|
||||||
|
where
|
||||||
|
go = do
|
||||||
|
v <- a
|
||||||
|
return (Right v)
|
||||||
|
|
||||||
{- Catches only DoesNotExist exceptions, and lets all others through. -}
|
{- Catches only DoesNotExist exceptions, and lets all others through. -}
|
||||||
tryWhenExists :: IO a -> IO (Maybe a)
|
tryWhenExists :: MonadCatch m => m a -> m (Maybe a)
|
||||||
tryWhenExists a = eitherToMaybe <$>
|
tryWhenExists a = do
|
||||||
tryJust (guard . isDoesNotExistError) a
|
v <- tryJust (guard . isDoesNotExistError) a
|
||||||
|
return (eitherToMaybe v)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue