A peer-to-peer stack for code collaboration
Remove 'MonadError' instance from Daemon
Thomas Scholtes50f26df82114
diff --git a/exe/Daemon.hs b/exe/Daemon.hsindex 89f38da..cd05bd6 100644--- a/exe/Daemon.hs+++ b/exe/Daemon.hs@@ -93,7 +93,7 @@ waitForIpfsDaemon = dogo 5wherego :: Int -> Daemon ()- go 0 = throwError IpfsDaemonNotReachable+ go 0 = throw IpfsDaemonNotReachablego n = doisOnline <- checkIpfsIsOnlineif isOnline@@ -188,7 +188,7 @@ init follows = traverse_ initMachine (Map.toList follows)-- /writer/.newMachine :: Daemon Api.NewResponsenewMachine = do- id <- ipfs createMachine CouldNotCreateMachine+ id <- wrapException CouldNotCreateMachine (liftIO createMachine)sub <- initDaemonSubscription idlogInfo "Created new IPFS machine" [("machine-id", getMachineId id)]m <- emptyMachine id Writer sub@@ -208,7 +208,7 @@ query id (Api.QueryRequest v) = dom <- ensureMachineLoaded idbumpPolling idcase fst <$> runIdentity $ runLang (machineState m) $ eval v of- Left err -> throwError $ MachineError id (InvalidInput err)+ Left err -> throw $ MachineError id (InvalidInput err)Right rv -> dologInfo "Handled query" [("machine-id", getMachineId id)]pure (Api.QueryResponse rv)@@ -255,7 +255,7 @@ send id (Api.SendRequest expressions) = do]bumpPolling idpure Api.SendResponse{..}- Nothing -> throwError $ MachineError id AckTimeout+ Nothing -> throw $ MachineError id AckTimeoutprValues = T.intercalate "," . (renderCompactPretty <$>)@@ -325,7 +325,7 @@ addInputsaddInputs inputs index m = dolet (result, newState) = runIdentity $ runLang (machineState m) $ traverse eval inputscase result of- Left err -> throwError $ MachineError (machineId m) (InvalidInput err)+ Left err -> throw $ MachineError (machineId m) (InvalidInput err)Right outputs -> dot <- liftIO $ Time.getSystemTimelet 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 -> dolet (msg, infos) = displayError errlogError' "Could not initiate reader-mode machine on startup" $ ("init-error", msg) : infosinsertUninitializedReader iddiff --git a/src/Radicle/Daemon/MachineStore.hs b/src/Radicle/Daemon/MachineStore.hsindex 39b6327..4d69ce0 100644--- a/src/Radicle/Daemon/MachineStore.hs+++ b/src/Radicle/Daemon/MachineStore.hs@@ -21,6 +21,7 @@ module Radicle.Daemon.MachineStoreimport Protolude+import Control.Exception.Safeimport Control.Monad.IO.Unliftimport qualified Data.Aeson as Aimport 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 whereaskMachines :: m CachedMachines@@ -88,12 +89,12 @@ modifyMachine id f = domachines <- askMachinesres <- CMap.modifyExistingValue id (getMachines machines) modCachedcase res of- Nothing -> throwError (MachineError id MachineNotCached)+ Nothing -> throw (MachineError id MachineNotCached)Just a -> pure awheremodCached :: CachedMachine -> m (CachedMachine, a)modCached = \case- UninitialisedReader _ -> throwError $ MachineError id MachineNotCached+ UninitialisedReader _ -> throw $ MachineError id MachineNotCachedCached m -> do(m', a) <- f mpure (Cached m', a)diff --git a/src/Radicle/Daemon/Monad.hs b/src/Radicle/Daemon/Monad.hsindex 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.Exceptimport Control.Monad.IO.Unliftimport Radicle.Daemon.Error@@ -26,24 +25,14 @@ import Radicle.Daemon.Loggingimport 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 whereaskLogLevel = asks logLevel