module Matterhorn.IOUtil
  ( convertIOException
  )
where

import Prelude ()
import Matterhorn.Prelude

import Control.Exception
import Control.Monad.Trans.Except
import System.IO.Error ( ioeGetErrorString )


convertIOException :: IO a -> ExceptT String IO a
convertIOException :: forall a. IO a -> ExceptT String IO a
convertIOException IO a
act = do
    Either String a
result <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ (forall a b. b -> Either a b
Right forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO a
act) forall e a. Exception e => IO a -> (e -> IO a) -> IO a
`catch`
                       (\(IOError
e::IOError) -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ IOError -> String
ioeGetErrorString IOError
e)
    case Either String a
result of
        Left String
e -> forall (m :: * -> *) e a. Monad m => e -> ExceptT e m a
throwE String
e
        Right a
v -> forall (m :: * -> *) a. Monad m => a -> m a
return a
v