Skip to content

A peer-to-peer stack for code collaboration

deamon: Extract errors

Thomas Scholtes1d4e0f46638b
diff --git a/src/Radicle/Daemon/Error.hs b/src/Radicle/Daemon/Error.hs
new file mode 100644
index 0000000..d7c56ec
--- /dev/null
+++ b/src/Radicle/Daemon/Error.hs
@@ -0,0 +1,54 @@
+module Radicle.Daemon.Error
+ ( MachineError(..)
+ , Error(..)
+ , MonadError(..)
+ , displayError
+ ) where
+
+import Protolude
+
+import Radicle (LangError, Value, renderCompactPretty)
+import Radicle.Daemon.Ipfs
+import Radicle.Ipfs
+
+-- | An error relating to a specific machine managed by the daemon.
+data MachineError
+ = InvalidInput (LangError Value)
+ | IpfsError IpfsException
+ | AckTimeout
+ | DaemonError Text
+ | MachineNotCached
+ deriving (Show)
+
+instance Exception MachineError
+
+-- | An error that the daemon can encounter.
+data Error
+ = MachineError MachineId MachineError
+ | CouldNotCreateMachine IpfsException
+ | IpfsDaemonNotReachable
+ deriving (Show)
+
+instance Exception Error
+
+displayError :: Error -> (Text, [(Text,Text)])
+displayError = \case
+ CouldNotCreateMachine (ipfsErr -> (msg, infos)) -> ("Could not create IPFS machine", ("message", msg) : infos)
+ IpfsDaemonNotReachable -> ("Could not connect to Radicle IPFS daemon", [])
+ MachineError id e -> let mid = ("machine-id", getMachineId id) in
+ case e of
+ InvalidInput err -> ("Invalid radicle input", [mid, ("radicle-error", renderCompactPretty err)])
+ IpfsError (ipfsErr -> (msg, infos)) -> (msg, mid : infos)
+ AckTimeout -> ("The machine owner appears to be offline", [mid])
+ MachineNotCached -> ("Machine was not found in cache", [mid])
+ DaemonError err -> ("Internal error", [mid, ("error", err)])
+ where
+ ipfsErr = \case
+ IpfsException msg -> ("There was an error using the IPFS daemon", [("ipfs-error", msg)])
+ IpfsExceptionErrResp msg -> ("The IPFS daemon returned an error", [("ipfs-error", msg)])
+ IpfsExceptionErrRespNoMsg -> ("There was an unknown error using IPFS", [])
+ IpfsExceptionTimeout apiPath -> ("Timeout communicating with IPFS daemon", [("api-path", apiPath)])
+ IpfsExceptionInvalidResponse url parseError -> ("Cannot parse IPFS daemon response", [("url", url), ("parse-error", parseError)])
+ IpfsExceptionNoDaemon -> ("Cannot connect to the IPFS daemon", [])
+ IpfsExceptionIpldParse addr parseError ->
+ ("Failed to parse IPLD document ", [("addr", addressToText addr), ("parse-error", parseError)])
diff --git a/src/Radicle/Daemon/Monad.hs b/src/Radicle/Daemon/Monad.hs
index 0b9dc12..ced7d07 100644
--- a/src/Radicle/Daemon/Monad.hs
+++ b/src/Radicle/Daemon/Monad.hs
@@ -21,11 +21,9 @@ import Control.Exception.Safe
import Control.Monad.Except
import Control.Monad.IO.Unlift
-import Radicle (LangError, Value, renderCompactPretty)
import Radicle.Daemon.Common
-import Radicle.Daemon.Ipfs
+import Radicle.Daemon.Error
import Radicle.Daemon.Logging
-import Radicle.Ipfs
newtype Daemon a = Daemon (ExceptT Error (ReaderT Env IO) a)
@@ -60,47 +58,3 @@ data Env = Env
, machines :: CachedMachines
, logLevel :: LogLevel
}
-
--- * Errors
-
--- | An error relating to a specific machine managed by the daemon.
-data MachineError
- = InvalidInput (LangError Value)
- | IpfsError IpfsException
- | AckTimeout
- | DaemonError Text
- | MachineNotCached
- deriving (Show)
-
-instance Exception MachineError
-
--- | An error that the daemon can encounter.
-data Error
- = MachineError MachineId MachineError
- | CouldNotCreateMachine IpfsException
- | IpfsDaemonNotReachable
- deriving (Show)
-
-instance Exception Error
-
-displayError :: Error -> (Text, [(Text,Text)])
-displayError = \case
- CouldNotCreateMachine (ipfsErr -> (msg, infos)) -> ("Could not create IPFS machine", ("message", msg) : infos)
- IpfsDaemonNotReachable -> ("Could not connect to Radicle IPFS daemon", [])
- MachineError id e -> let mid = ("machine-id", getMachineId id) in
- case e of
- InvalidInput err -> ("Invalid radicle input", [mid, ("radicle-error", renderCompactPretty err)])
- IpfsError (ipfsErr -> (msg, infos)) -> (msg, mid : infos)
- AckTimeout -> ("The machine owner appears to be offline", [mid])
- MachineNotCached -> ("Machine was not found in cache", [mid])
- DaemonError err -> ("Internal error", [mid, ("error", err)])
- where
- ipfsErr = \case
- IpfsException msg -> ("There was an error using the IPFS daemon", [("ipfs-error", msg)])
- IpfsExceptionErrResp msg -> ("The IPFS daemon returned an error", [("ipfs-error", msg)])
- IpfsExceptionErrRespNoMsg -> ("There was an unknown error using IPFS", [])
- IpfsExceptionTimeout apiPath -> ("Timeout communicating with IPFS daemon", [("api-path", apiPath)])
- IpfsExceptionInvalidResponse url parseError -> ("Cannot parse IPFS daemon response", [("url", url), ("parse-error", parseError)])
- IpfsExceptionNoDaemon -> ("Cannot connect to the IPFS daemon", [])
- IpfsExceptionIpldParse addr parseError ->
- ("Failed to parse IPLD document ", [("addr", addressToText addr), ("parse-error", parseError)])