Skip to content

A peer-to-peer stack for code collaboration

Remove 'MonadError' instance from Daemon

Thomas Scholtes50f26df82114
diff --git a/exe/Daemon.hs b/exe/Daemon.hs
index 89f38da..cd05bd6 100644
--- a/exe/Daemon.hs
+++ b/exe/Daemon.hs
@@ -93,7 +93,7 @@ waitForIpfsDaemon = do
go 5
where
go :: Int -> Daemon ()
- go 0 = throwError IpfsDaemonNotReachable
+ go 0 = throw IpfsDaemonNotReachable
go n = do
isOnline <- checkIpfsIsOnline
if isOnline
@@ -188,7 +188,7 @@ init follows = traverse_ initMachine (Map.toList follows)
-- /writer/.
newMachine :: Daemon Api.NewResponse
newMachine = do
- id <- ipfs createMachine CouldNotCreateMachine
+ id <- wrapException CouldNotCreateMachine (liftIO createMachine)
sub <- initDaemonSubscription id
logInfo "Created new IPFS machine" [("machine-id", getMachineId id)]
m <- emptyMachine id Writer sub
@@ -208,7 +208,7 @@ query id (Api.QueryRequest v) = do
m <- ensureMachineLoaded id
bumpPolling id
case fst <$> runIdentity $ runLang (machineState m) $ eval v of
- Left err -> throwError $ MachineError id (InvalidInput err)
+ Left err -> throw $ MachineError id (InvalidInput err)
Right rv -> do
logInfo "Handled query" [("machine-id", getMachineId id)]
pure (Api.QueryResponse rv)
@@ -255,7 +255,7 @@ send id (Api.SendRequest expressions) = do
]
bumpPolling id
pure Api.SendResponse{..}
- Nothing -> throwError $ MachineError id AckTimeout
+ Nothing -> throw $ MachineError id AckTimeout
prValues = T.intercalate "," . (renderCompactPretty <$>)
@@ -325,7 +325,7 @@ addInputs
addInputs inputs index m = do
let (result, newState) = runIdentity $ runLang (machineState m) $ traverse eval inputs
case result of
- Left err -> throwError $ MachineError (machineId m) (InvalidInput err)
+ Left err -> throw $ MachineError (machineId m) (InvalidInput err)
Right outputs -> do
t <- liftIO $ Time.getSystemTime
let m' = m { machineState = newState
@@ -334,17 +334,9 @@ addInputs inputs index m = do
}
pure (m', outputs)
--- | Run some IPFS IO.
-ipfs :: IO a -> (Ipfs.IpfsException -> Error) -> Daemon a
-ipfs io err = do
- res <- liftIO $ (Right <$> io) `catch` \(e :: Ipfs.IpfsException) -> pure (Left e)
- case res of
- Left e -> throwError (err e)
- Right x -> pure x
-
-- | Run some IPFS IO related to a specific machine.
machineIpfs :: MachineId -> IO a -> Daemon a
-machineIpfs id io = ipfs io (MachineError id . IpfsError)
+machineIpfs id io = wrapException (MachineError id . IpfsError) (liftIO io)
-- | Do some high-freq polling for a while.
bumpPolling :: MachineId -> Daemon ()
@@ -381,7 +373,7 @@ initAsReader id = do
-- | Try to initiate a reader, but don't fail in case of errors; just log a
-- message.
initReaderNoFail :: MachineId -> Daemon ()
-initReaderNoFail id = catchError (initAsReader id $> ()) $ \err -> do
+initReaderNoFail id = catch (initAsReader id $> ()) $ \err -> do
let (msg, infos) = displayError err
logError' "Could not initiate reader-mode machine on startup" $ ("init-error", msg) : infos
insertUninitializedReader id
diff --git a/src/Radicle/Daemon/MachineStore.hs b/src/Radicle/Daemon/MachineStore.hs
index 39b6327..4d69ce0 100644
--- a/src/Radicle/Daemon/MachineStore.hs
+++ b/src/Radicle/Daemon/MachineStore.hs
@@ -21,6 +21,7 @@ module Radicle.Daemon.MachineStore
import Protolude
+import Control.Exception.Safe
import Control.Monad.IO.Unlift
import qualified Data.Aeson as A
import qualified Data.Map.Strict as Map
@@ -76,7 +77,7 @@ newtype CachedMachines = CachedMachines { getMachines :: CMap.CMap MachineId Cac
-- * Machine access and modification
-class (MonadUnliftIO m, MonadError Error m) => MonadMachineStore m where
+class (MonadUnliftIO m, MonadThrow m) => MonadMachineStore m where
askMachines :: m CachedMachines
@@ -88,12 +89,12 @@ modifyMachine id f = do
machines <- askMachines
res <- CMap.modifyExistingValue id (getMachines machines) modCached
case res of
- Nothing -> throwError (MachineError id MachineNotCached)
+ Nothing -> throw (MachineError id MachineNotCached)
Just a -> pure a
where
modCached :: CachedMachine -> m (CachedMachine, a)
modCached = \case
- UninitialisedReader _ -> throwError $ MachineError id MachineNotCached
+ UninitialisedReader _ -> throw $ MachineError id MachineNotCached
Cached m -> do
(m', a) <- f m
pure (Cached m', a)
diff --git a/src/Radicle/Daemon/Monad.hs b/src/Radicle/Daemon/Monad.hs
index c4e50f6..269898a 100644
--- a/src/Radicle/Daemon/Monad.hs
+++ b/src/Radicle/Daemon/Monad.hs
@@ -4,7 +4,7 @@
module Radicle.Daemon.Monad
( Daemon
, runDaemon
- , liftExceptT
+ , wrapException
, Env(..)
, FileLock
@@ -15,10 +15,9 @@ module Radicle.Daemon.Monad
, displayError
) where
-import Protolude hiding (try)
+import Protolude hiding (catch, try)
import Control.Exception.Safe
-import Control.Monad.Except
import Control.Monad.IO.Unlift
import Radicle.Daemon.Error
@@ -26,24 +25,14 @@ import Radicle.Daemon.Logging
import Radicle.Daemon.MachineStore
-newtype Daemon a = Daemon (ExceptT Error (ReaderT Env IO) a)
- deriving (Functor, Applicative, Monad, MonadError Error, MonadIO, MonadReader Env)
-
-instance MonadUnliftIO Daemon where
- withRunInIO inner = do
- env <- ask
- result <- liftIO $ try $ inner $ \d -> do
- result <- runDaemon env d
- case result of
- Left err -> throw err
- Right a -> pure a
- liftEither result
+newtype Daemon a = Daemon (ReaderT Env IO a)
+ deriving (Functor, Applicative, Monad, MonadThrow, MonadCatch, MonadIO, MonadReader Env, MonadUnliftIO)
runDaemon :: Env -> Daemon a -> IO (Either Error a)
-runDaemon env (Daemon x) = runReaderT (runExceptT x) env
+runDaemon env (Daemon x) = try $ runReaderT x env
-liftExceptT :: (e -> Error) -> ExceptT e IO a -> Daemon a
-liftExceptT makeError action = Daemon $ mapExceptT (lift . fmap (first makeError)) action
+wrapException :: (MonadCatch m, Exception e, Exception e') => (e -> e') -> m a -> m a
+wrapException f action = action `catch` (throw . f)
instance MonadLog Daemon where
askLogLevel = asks logLevel