2012-10-27 04:42:14 +00:00
|
|
|
{- Simple IO exception handling (and some more)
|
2012-02-03 20:47:24 +00:00
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2011-2014 Joey Hess <id@joeyh.name>
|
2012-02-03 20:47:24 +00:00
|
|
|
-
|
2014-05-10 14:01:27 +00:00
|
|
|
- License: BSD-2-clause
|
2012-02-03 20:47:24 +00:00
|
|
|
-}
|
|
|
|
|
2012-10-27 04:42:14 +00:00
|
|
|
{-# LANGUAGE ScopedTypeVariables #-}
|
|
|
|
|
unify exception handling into Utility.Exception
Removed old extensible-exceptions, only needed for very old ghc.
Made webdav use Utility.Exception, to work after some changes in DAV's
exception handling.
Removed Annex.Exception. Mostly this was trivial, but note that
tryAnnex is replaced with tryNonAsync and catchAnnex replaced with
catchNonAsync. In theory that could be a behavior change, since the former
caught all exceptions, and the latter don't catch async exceptions.
However, in practice, nothing in the Annex monad uses async exceptions.
Grepping for throwTo and killThread only find stuff in the assistant,
which does not seem related.
Command.Add.undo is changed to accept a SomeException, and things
that use it for rollback now catch non-async exceptions, rather than
only IOExceptions.
2014-08-08 01:55:44 +00:00
|
|
|
module Utility.Exception (
|
|
|
|
module X,
|
|
|
|
catchBoolIO,
|
|
|
|
catchMaybeIO,
|
|
|
|
catchDefaultIO,
|
|
|
|
catchMsgIO,
|
|
|
|
catchIO,
|
|
|
|
tryIO,
|
|
|
|
bracketIO,
|
|
|
|
catchNonAsync,
|
|
|
|
tryNonAsync,
|
|
|
|
tryWhenExists,
|
|
|
|
) where
|
2012-02-03 20:47:24 +00:00
|
|
|
|
unify exception handling into Utility.Exception
Removed old extensible-exceptions, only needed for very old ghc.
Made webdav use Utility.Exception, to work after some changes in DAV's
exception handling.
Removed Annex.Exception. Mostly this was trivial, but note that
tryAnnex is replaced with tryNonAsync and catchAnnex replaced with
catchNonAsync. In theory that could be a behavior change, since the former
caught all exceptions, and the latter don't catch async exceptions.
However, in practice, nothing in the Annex monad uses async exceptions.
Grepping for throwTo and killThread only find stuff in the assistant,
which does not seem related.
Command.Add.undo is changed to accept a SomeException, and things
that use it for rollback now catch non-async exceptions, rather than
only IOExceptions.
2014-08-08 01:55:44 +00:00
|
|
|
import Control.Monad.Catch as X hiding (Handler)
|
|
|
|
import qualified Control.Monad.Catch as M
|
2014-08-08 01:01:07 +00:00
|
|
|
import Control.Exception (IOException, AsyncException)
|
2013-05-21 17:03:46 +00:00
|
|
|
import Control.Monad
|
unify exception handling into Utility.Exception
Removed old extensible-exceptions, only needed for very old ghc.
Made webdav use Utility.Exception, to work after some changes in DAV's
exception handling.
Removed Annex.Exception. Mostly this was trivial, but note that
tryAnnex is replaced with tryNonAsync and catchAnnex replaced with
catchNonAsync. In theory that could be a behavior change, since the former
caught all exceptions, and the latter don't catch async exceptions.
However, in practice, nothing in the Annex monad uses async exceptions.
Grepping for throwTo and killThread only find stuff in the assistant,
which does not seem related.
Command.Add.undo is changed to accept a SomeException, and things
that use it for rollback now catch non-async exceptions, rather than
only IOExceptions.
2014-08-08 01:55:44 +00:00
|
|
|
import Control.Monad.IO.Class (liftIO, MonadIO)
|
2013-05-21 17:03:46 +00:00
|
|
|
import System.IO.Error (isDoesNotExistError)
|
2013-09-27 23:52:36 +00:00
|
|
|
import Utility.Data
|
2012-02-03 20:47:24 +00:00
|
|
|
|
|
|
|
{- Catches IO errors and returns a Bool -}
|
2014-08-08 01:01:07 +00:00
|
|
|
catchBoolIO :: MonadCatch m => m Bool -> m Bool
|
2014-04-26 23:25:05 +00:00
|
|
|
catchBoolIO = catchDefaultIO False
|
2012-02-03 20:47:24 +00:00
|
|
|
|
|
|
|
{- Catches IO errors and returns a Maybe -}
|
2014-08-08 01:01:07 +00:00
|
|
|
catchMaybeIO :: MonadCatch m => m a -> m (Maybe a)
|
|
|
|
catchMaybeIO a = do
|
|
|
|
catchDefaultIO Nothing $ do
|
|
|
|
v <- a
|
|
|
|
return (Just v)
|
2012-02-03 20:47:24 +00:00
|
|
|
|
|
|
|
{- Catches IO errors and returns a default value. -}
|
2014-08-08 01:01:07 +00:00
|
|
|
catchDefaultIO :: MonadCatch m => a -> m a -> m a
|
2012-09-17 04:18:07 +00:00
|
|
|
catchDefaultIO def a = catchIO a (const $ return def)
|
2012-02-03 20:47:24 +00:00
|
|
|
|
|
|
|
{- Catches IO errors and returns the error message. -}
|
2014-08-08 01:01:07 +00:00
|
|
|
catchMsgIO :: MonadCatch m => m a -> m (Either String a)
|
|
|
|
catchMsgIO a = do
|
|
|
|
v <- tryIO a
|
|
|
|
return $ either (Left . show) Right v
|
2012-02-03 20:47:24 +00:00
|
|
|
|
|
|
|
{- catch specialized for IO errors only -}
|
2014-08-08 01:01:07 +00:00
|
|
|
catchIO :: MonadCatch m => m a -> (IOException -> m a) -> m a
|
2014-08-10 23:40:21 +00:00
|
|
|
catchIO = M.catch
|
2012-02-03 20:47:24 +00:00
|
|
|
|
|
|
|
{- try specialized for IO errors only -}
|
2014-08-08 01:01:07 +00:00
|
|
|
tryIO :: MonadCatch m => m a -> m (Either IOException a)
|
2014-08-10 23:40:21 +00:00
|
|
|
tryIO = M.try
|
2012-10-27 04:42:14 +00:00
|
|
|
|
unify exception handling into Utility.Exception
Removed old extensible-exceptions, only needed for very old ghc.
Made webdav use Utility.Exception, to work after some changes in DAV's
exception handling.
Removed Annex.Exception. Mostly this was trivial, but note that
tryAnnex is replaced with tryNonAsync and catchAnnex replaced with
catchNonAsync. In theory that could be a behavior change, since the former
caught all exceptions, and the latter don't catch async exceptions.
However, in practice, nothing in the Annex monad uses async exceptions.
Grepping for throwTo and killThread only find stuff in the assistant,
which does not seem related.
Command.Add.undo is changed to accept a SomeException, and things
that use it for rollback now catch non-async exceptions, rather than
only IOExceptions.
2014-08-08 01:55:44 +00:00
|
|
|
{- bracket with setup and cleanup actions lifted to IO.
|
|
|
|
-
|
|
|
|
- Note that unlike catchIO and tryIO, this catches all exceptions. -}
|
|
|
|
bracketIO :: (MonadMask m, MonadIO m) => IO v -> (v -> IO b) -> (v -> m a) -> m a
|
|
|
|
bracketIO setup cleanup = bracket (liftIO setup) (liftIO . cleanup)
|
|
|
|
|
2012-10-27 04:42:14 +00:00
|
|
|
{- Catches all exceptions except for async exceptions.
|
|
|
|
- This is often better to use than catching them all, so that
|
|
|
|
- ThreadKilled and UserInterrupt get through.
|
|
|
|
-}
|
2014-08-08 01:01:07 +00:00
|
|
|
catchNonAsync :: MonadCatch m => m a -> (SomeException -> m a) -> m a
|
2012-10-27 04:42:14 +00:00
|
|
|
catchNonAsync a onerr = a `catches`
|
unify exception handling into Utility.Exception
Removed old extensible-exceptions, only needed for very old ghc.
Made webdav use Utility.Exception, to work after some changes in DAV's
exception handling.
Removed Annex.Exception. Mostly this was trivial, but note that
tryAnnex is replaced with tryNonAsync and catchAnnex replaced with
catchNonAsync. In theory that could be a behavior change, since the former
caught all exceptions, and the latter don't catch async exceptions.
However, in practice, nothing in the Annex monad uses async exceptions.
Grepping for throwTo and killThread only find stuff in the assistant,
which does not seem related.
Command.Add.undo is changed to accept a SomeException, and things
that use it for rollback now catch non-async exceptions, rather than
only IOExceptions.
2014-08-08 01:55:44 +00:00
|
|
|
[ M.Handler (\ (e :: AsyncException) -> throwM e)
|
|
|
|
, M.Handler (\ (e :: SomeException) -> onerr e)
|
2012-10-27 04:42:14 +00:00
|
|
|
]
|
|
|
|
|
2014-08-08 01:01:07 +00:00
|
|
|
tryNonAsync :: MonadCatch m => m a -> m (Either SomeException a)
|
|
|
|
tryNonAsync a = go `catchNonAsync` (return . Left)
|
|
|
|
where
|
|
|
|
go = do
|
|
|
|
v <- a
|
|
|
|
return (Right v)
|
2013-05-21 17:03:46 +00:00
|
|
|
|
|
|
|
{- Catches only DoesNotExist exceptions, and lets all others through. -}
|
2014-08-08 01:01:07 +00:00
|
|
|
tryWhenExists :: MonadCatch m => m a -> m (Maybe a)
|
|
|
|
tryWhenExists a = do
|
|
|
|
v <- tryJust (guard . isDoesNotExistError) a
|
|
|
|
return (eitherToMaybe v)
|