module Darcs.Util.Exception
( firstJustIO
, catchall
, clarifyErrors
, prettyException
, prettyError
, die
, handleOnly
, handleOnlyIOError
, catchDoesNotExistError
, handleDoesNotExistError
, ifIOError
, ifDoesNotExistError
) where
import Darcs.Prelude
import Control.Exception
( Exception(fromException)
, SomeException
, catch
, handle
, throwIO
)
import Data.Maybe ( isJust )
import System.Exit ( exitFailure )
import System.IO ( stderr, hPutStrLn )
import System.IO.Error
( ioeGetErrorString
, ioeGetFileName
, isDoesNotExistError
, isUserError
)
import Darcs.Util.Global ( debugMessage )
import Darcs.Util.SignalHandler ( catchNonSignal )
catchall :: IO a -> IO a -> IO a
IO a
a catchall :: forall a. IO a -> IO a -> IO a
`catchall` IO a
b = IO a
a IO a -> (SomeException -> IO a) -> IO a
forall a. IO a -> (SomeException -> IO a) -> IO a
`catchNonSignal` (\SomeException
e -> String -> IO ()
debugMessage (String
"catchall: "String -> String -> String
forall a. [a] -> [a] -> [a]
++SomeException -> String
forall a. Show a => a -> String
show SomeException
e) IO () -> IO a -> IO a
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> IO a
b)
firstJustM :: Monad m
=> [m (Maybe a)]
-> m (Maybe a)
firstJustM :: forall (m :: * -> *) a. Monad m => [m (Maybe a)] -> m (Maybe a)
firstJustM [] = Maybe a -> m (Maybe a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing
firstJustM (m (Maybe a)
e:[m (Maybe a)]
es) = m (Maybe a)
e m (Maybe a) -> (Maybe a -> m (Maybe a)) -> m (Maybe a)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (\Maybe a
v -> if Maybe a -> Bool
forall a. Maybe a -> Bool
isJust Maybe a
v then Maybe a -> m (Maybe a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
v else [m (Maybe a)] -> m (Maybe a)
forall (m :: * -> *) a. Monad m => [m (Maybe a)] -> m (Maybe a)
firstJustM [m (Maybe a)]
es)
firstJustIO :: [IO (Maybe a)]
-> IO (Maybe a)
firstJustIO :: forall a. [IO (Maybe a)] -> IO (Maybe a)
firstJustIO = [IO (Maybe a)] -> IO (Maybe a)
forall (m :: * -> *) a. Monad m => [m (Maybe a)] -> m (Maybe a)
firstJustM ([IO (Maybe a)] -> IO (Maybe a))
-> ([IO (Maybe a)] -> [IO (Maybe a)])
-> [IO (Maybe a)]
-> IO (Maybe a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (IO (Maybe a) -> IO (Maybe a)) -> [IO (Maybe a)] -> [IO (Maybe a)]
forall a b. (a -> b) -> [a] -> [b]
map (IO (Maybe a) -> IO (Maybe a) -> IO (Maybe a)
forall a. IO a -> IO a -> IO a
`catchall` Maybe a -> IO (Maybe a)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing)
clarifyErrors :: IO a
-> String
-> IO a
clarifyErrors :: forall a. IO a -> String -> IO a
clarifyErrors IO a
a String
e = IO a
a IO a -> (SomeException -> IO a) -> IO a
forall e a. Exception e => IO a -> (e -> IO a) -> IO a
`catch` (\SomeException
x -> String -> IO a
forall a. String -> IO a
die (String -> IO a) -> String -> IO a
forall a b. (a -> b) -> a -> b
$ [String] -> String
unlines [SomeException -> String
prettyException SomeException
x,String
e])
prettyException :: SomeException
-> String
prettyException :: SomeException -> String
prettyException SomeException
e | Just IOError
ioe <- SomeException -> Maybe IOError
forall e. Exception e => SomeException -> Maybe e
fromException SomeException
e, IOError -> Bool
isUserError IOError
ioe = IOError -> String
ioeGetErrorString IOError
ioe
prettyException SomeException
e | Just IOError
ioe <- SomeException -> Maybe IOError
forall e. Exception e => SomeException -> Maybe e
fromException SomeException
e, IOError -> Bool
isDoesNotExistError IOError
ioe =
case IOError -> Maybe String
ioeGetFileName IOError
ioe of
Just String
f -> String
f String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" does not exist"
Maybe String
Nothing -> SomeException -> String
forall a. Show a => a -> String
show SomeException
e
prettyException SomeException
e = SomeException -> String
forall a. Show a => a -> String
show SomeException
e
prettyError :: IOError -> String
prettyError :: IOError -> String
prettyError IOError
e | IOError -> Bool
isUserError IOError
e = IOError -> String
ioeGetErrorString IOError
e
| Bool
otherwise = IOError -> String
forall a. Show a => a -> String
show IOError
e
die :: String -> IO a
die :: forall a. String -> IO a
die String
msg = Handle -> String -> IO ()
hPutStrLn Handle
stderr String
msg IO () -> IO a -> IO a
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> IO a
forall a. IO a
exitFailure
handleOnlyIOError :: IO a -> IO a -> IO a
handleOnlyIOError :: forall a. IO a -> IO a -> IO a
handleOnlyIOError = (IOError -> Bool) -> IO a -> IO a -> IO a
forall e a. Exception e => (e -> Bool) -> IO a -> IO a -> IO a
handleOnly (Bool -> Bool
not (Bool -> Bool) -> (IOError -> Bool) -> IOError -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IOError -> Bool
isUserError)
handleDoesNotExistError :: IO a -> IO a -> IO a
handleDoesNotExistError :: forall a. IO a -> IO a -> IO a
handleDoesNotExistError = (IOError -> Bool) -> IO a -> IO a -> IO a
forall e a. Exception e => (e -> Bool) -> IO a -> IO a -> IO a
handleOnly IOError -> Bool
isDoesNotExistError
catchDoesNotExistError :: IO a -> IO a -> IO a
catchDoesNotExistError :: forall a. IO a -> IO a -> IO a
catchDoesNotExistError = (IO a -> IO a -> IO a) -> IO a -> IO a -> IO a
forall a b c. (a -> b -> c) -> b -> a -> c
flip IO a -> IO a -> IO a
forall a. IO a -> IO a -> IO a
handleDoesNotExistError
ifIOError :: a -> IO a -> IO a
ifIOError :: forall a. a -> IO a -> IO a
ifIOError a
use_instead = IO a -> IO a -> IO a
forall a. IO a -> IO a -> IO a
handleOnlyIOError (a -> IO a
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return a
use_instead)
ifDoesNotExistError :: a -> IO a -> IO a
ifDoesNotExistError :: forall a. a -> IO a -> IO a
ifDoesNotExistError a
use_instead = (IOError -> Bool) -> IO a -> IO a -> IO a
forall e a. Exception e => (e -> Bool) -> IO a -> IO a -> IO a
handleOnly IOError -> Bool
isDoesNotExistError (a -> IO a
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return a
use_instead)
handleOnly :: Exception e => (e -> Bool) -> IO a -> IO a -> IO a
handleOnly :: forall e a. Exception e => (e -> Bool) -> IO a -> IO a -> IO a
handleOnly e -> Bool
pred IO a
handler = (e -> IO a) -> IO a -> IO a
forall e a. Exception e => (e -> IO a) -> IO a -> IO a
handle (\e
e -> if e -> Bool
pred e
e then IO a
handler else e -> IO a
forall e a. Exception e => e -> IO a
throwIO e
e)