Skip to content

A peer-to-peer stack for code collaboration

Apps catch daemon errors. (#437)

James Haydoncc3dff5ebaf3
diff --git a/bin/rad-diff b/bin/rad-diff
index 6450ed9..25640b6 100755
--- a/bin/rad-diff
+++ b/bin/rad-diff
@@ -296,18 +296,22 @@
(/cmd-opts "list" opts cmd-options help)))
(def args (get-args!))
-(match args
- (/list-cmd 'options) (list-items (machine-name) options)
- (/cmd-1 "propose" 'commit help) (new-diff! (machine-name) commit)
- (/cmd-1 "checkout" 'diff-no help) (checkout-diff! (machine-name) (read diff-no))
- (/cmd-1 "show" 'diff-no help) (show-diff! (machine-name) (read diff-no))
- (/cmd-1 "retract" 'diff-no help) (edit-diff-state! (machine-name) (read diff-no) :retracted)
- (/cmd-1 "accept" 'diff-no help) (edit-diff-state! (machine-name) (read diff-no) :accepted)
- (/cmd-1 "reject" 'diff-no help) (edit-diff-state! (machine-name) (read diff-no) :rejected)
- ["comment" 'diff-no 'comment] (create-comment! (machine-name) (read diff-no) comment)
- (/cmd-0 "create-machine" help) (new-diff-machine! (machine-name))
- ["help"] (put-str! help)
- ["-h"] (put-str! help)
- ["--help"] (put-str! help)
- (/cons 'cmd _) (cmd-parse-failure (string-append "Unknown command: " cmd))
- [] (put-str! help))
+
+(machine/catch-daemon!
+ (fn []
+ (match args
+ (/list-cmd 'options) (list-items (machine-name) options)
+ (/cmd-1 "propose" 'commit help) (new-diff! (machine-name) commit)
+ (/cmd-1 "checkout" 'diff-no help) (checkout-diff! (machine-name) (read diff-no))
+ (/cmd-1 "show" 'diff-no help) (show-diff! (machine-name) (read diff-no))
+ (/cmd-1 "retract" 'diff-no help) (edit-diff-state! (machine-name) (read diff-no) :retracted)
+ (/cmd-1 "accept" 'diff-no help) (edit-diff-state! (machine-name) (read diff-no) :accepted)
+ (/cmd-1 "reject" 'diff-no help) (edit-diff-state! (machine-name) (read diff-no) :rejected)
+ ["comment" 'diff-no 'comment] (create-comment! (machine-name) (read diff-no) comment)
+ (/cmd-0 "create-machine" help) (new-diff-machine! (machine-name))
+ ["help"] (put-str! help)
+ ["-h"] (put-str! help)
+ ["--help"] (put-str! help)
+ (/cons 'cmd _) (cmd-parse-failure (string-append "Unknown command: " cmd))
+ [] (put-str! help))))
+
diff --git a/bin/rad-issues b/bin/rad-issues
index d78d0a9..25a96dd 100755
--- a/bin/rad-issues
+++ b/bin/rad-issues
@@ -180,14 +180,17 @@
(/cmd-opts "list" opts cmd-options help)))
(def args (get-args!))
-(match args
- (/list-cmd 'options) (list-items (machine-name) options)
- (/cmd-1 "make-key" 'nick help) (make-key nick)
- (/cmd-0 "new" help) (new-issue (machine-name))
- (/cmd-1 "create-machine" 'machine-name help) (create-machine machine-name)
- (/cmd-1 "close" 'issue-number help) (close-issue issue-number (machine-name))
- ["help"] (put-str! help)
- ["-h"] (put-str! help)
- ["--help"] (put-str! help)
- (/cons 'cmd _) (cmd-parse-failure (string-append "Unknown command: " cmd))
- [] (put-str! help))
+
+(machine/catch-daemon!
+ (fn []
+ (match args
+ (/list-cmd 'options) (list-items (machine-name) options)
+ (/cmd-1 "make-key" 'nick help) (make-key nick)
+ (/cmd-0 "new" help) (new-issue (machine-name))
+ (/cmd-1 "create-machine" 'machine-name help) (create-machine machine-name)
+ (/cmd-1 "close" 'issue-number help) (close-issue issue-number (machine-name))
+ ["help"] (put-str! help)
+ ["-h"] (put-str! help)
+ ["--help"] (put-str! help)
+ (/cons 'cmd _) (cmd-parse-failure (string-append "Unknown command: " cmd))
+ [] (put-str! help))))
diff --git a/bin/rad-project b/bin/rad-project
index 62c4527..5c396b6 100755
--- a/bin/rad-project
+++ b/bin/rad-project
@@ -100,12 +100,15 @@
(put-str! "=> Share this project id for people to collaborate with you on your project.")))
(def args (get-args!))
-(match args
- (/cmd-0 "init" help) (new-project!)
- (/cmd-0 "show-id" help) (show-project-id)
- (/cmd-1 "checkout" 'project-id help) (checkout! project-id)
- ["help"] (put-str! help)
- ["-h"] (put-str! help)
- ["--help"] (put-str! help)
- (/cons 'cmd _) (parse-failure (string-append "Unknown command: " cmd) help)
- [] (put-str! help))
+
+(machine/catch-daemon!
+ (fn []
+ (match args
+ (/cmd-0 "init" help) (new-project!)
+ (/cmd-0 "show-id" help) (show-project-id)
+ (/cmd-1 "checkout" 'project-id help) (checkout! project-id)
+ ["help"] (put-str! help)
+ ["-h"] (put-str! help)
+ ["--help"] (put-str! help)
+ (/cons 'cmd _) (parse-failure (string-append "Unknown command: " cmd) help)
+ [] (put-str! help))))
diff --git a/docs/source/reference.rst b/docs/source/reference.rst
index ff1823d..7dee4e8 100644
--- a/docs/source/reference.rst
+++ b/docs/source/reference.rst
@@ -1217,6 +1217,12 @@ to be loaded
Send a command signed by the keys in ``my-keys.rad``.
+``(catch-daemon! f)``
+~~~~~~~~~~~~~~~~~~~~~
+
+Catches all ``radicle-daemon`` related errors and just prints them out
+to the user.
+
``prelude/state-machine``
-------------------------
diff --git a/exe/Daemon.hs b/exe/Daemon.hs
index e5a1c6c..ec48a2d 100644
--- a/exe/Daemon.hs
+++ b/exe/Daemon.hs
@@ -9,7 +9,6 @@ module Daemon (main) where
import Protolude hiding (fromStrict, option, poll)
-import Data.ByteString.Lazy (fromStrict)
import qualified Data.Map.Strict as Map
import qualified Data.Text as T
import qualified Data.Time.Clock.System as Time
@@ -25,7 +24,7 @@ import Radicle.Daemon.Ipfs
import Radicle.Daemon.MachineConfig
import Radicle.Daemon.Monad
-import Radicle hiding (Env)
+import Radicle hiding (DaemonError, Env)
import qualified Radicle.Internal.CLI as Local
import qualified Radicle.Internal.ConcurrentMap as CMap
import qualified Radicle.Internal.UUID as UUID
@@ -120,12 +119,16 @@ server env = hoistServer Api.daemonApi nt daemonServer
throwError (toServantErr err)
Right x -> pure x
- toServantErr err@(MachineError _ e) = case e of
- InvalidInput e' -> err400 { errBody = fromStrict $ encodeUtf8 (renderPrettyDef e') }
- AckTimeout -> err504 { errBody = "The writer for this IPFS machine does not appear to be online." }
- _ -> case displayError err of
- (msg, _) -> err500 { errBody = toS msg }
- toServantErr (CouldNotCreateMachine err) = err500 { errBody = "Could not create IPFS machine: " <> toS err }
+ toServantErr err = errCode { errBody = toS bod }
+ where
+ (msg, infos) = displayError err
+ bod = msg <> ":" <> T.concat ["\n " <> k <> ": " <> v | (k,v) <- infos]
+ errCode = case err of
+ MachineError _ e -> case e of
+ InvalidInput _ -> err400
+ AckTimeout -> err504
+ _ -> err500
+ CouldNotCreateMachine _ -> err500
-- * Init
diff --git a/package.yaml b/package.yaml
index f248967..29e39bf 100644
--- a/package.yaml
+++ b/package.yaml
@@ -140,7 +140,6 @@ executables:
- radicle
- mtl
- containers
- - bytestring
- optparse-applicative
- servant-server
- text
diff --git a/rad/prelude/machine.rad b/rad/prelude/machine.rad
index 3e7f76f..a9b1b6d 100644
--- a/rad/prelude/machine.rad
+++ b/rad/prelude/machine.rad
@@ -1,7 +1,8 @@
{:module 'prelude/machine
:doc "Functions for simulating remote machines."
:exports '[eval updatable-eval eval-fn-app send-prelude! new-machine!
- send-code! send! query! install-remote-machine-fake send-signed-command!]}
+ send-code! send! query! install-remote-machine-fake send-signed-command!
+ catch-daemon!]}
(import prelude/io '[read-file-value! read-file-values!] :as 'io)
(import prelude/ref '[modify-ref] :unqualified)
@@ -195,4 +196,13 @@ Do `rad-create-keys` to create your own key pair and rerun your command.")
(def send-signed-command!
"Send a command signed by the keys in `my-keys.rad`."
(fn [machine machine-id cmd payload]
- (send! machine [(list cmd (sign-entity! payload machine-id))])))
+ (send! machine [(list cmd (sign-entity! payload machine-id))])))
+
+(def catch-daemon!
+ "Catches all `radicle-daemon` related errors and just prints them out to the
+ user."
+ (fn [f]
+ (catch 'daemon-error (f)
+ (fn [e]
+ (put-str! (lookup :info e))
+ (exit! 1)))))
diff --git a/src/Radicle/Daemon/Client.hs b/src/Radicle/Daemon/Client.hs
index 4452453..b394a9f 100644
--- a/src/Radicle/Daemon/Client.hs
+++ b/src/Radicle/Daemon/Client.hs
@@ -111,6 +111,8 @@ wrapServantError :: Monad m => ExceptT Servant.ServantError m a -> Lang m a
wrapServantError action = do
result <- lift $ runExceptT action
case result of
- Left (Servant.FailureResponse response) -> throwErrorHere $ OtherError $ toS $ Servant.responseBody response
- Left e -> throwErrorHere $ OtherError $ toS $ displayException e
+ Left (Servant.FailureResponse response) -> throwErrorHere $ DaemonError $ toS $ Servant.responseBody response
+ Left e -> case e of
+ Servant.ConnectionError _ -> throwErrorHere $ DaemonError "Could not connect to radicle-daemon. Most likely it is not running. Please start it with 'radicle-daemon'."
+ _ -> throwErrorHere $ DaemonError $ toS $ displayException e
Right value -> pure value
diff --git a/src/Radicle/Daemon/Ipfs.hs b/src/Radicle/Daemon/Ipfs.hs
index 367f9eb..fee2688 100644
--- a/src/Radicle/Daemon/Ipfs.hs
+++ b/src/Radicle/Daemon/Ipfs.hs
@@ -61,9 +61,9 @@ instance Aeson.FromJSON MachineId where
parseJSON js = MachineId <$> Aeson.parseJSON js
data IpfsError
- = IpfsDaemonError Ipfs.IpfsException
- | InternalError Text
- | NetworkError Text
+ = IpfsDaemonException Ipfs.IpfsException
+ | IpfsDaemonErrMsg Text
+ | IpfsDaemonNoErrMsg
| Timeout
-- | Messages sent on a machine's IPFS pubsub topic.
@@ -115,7 +115,7 @@ data SubmitInputs = SubmitInputs
safeIpfs :: forall a. IO a -> ExceptT IpfsError IO a
safeIpfs io = ExceptT $ liftIO $
catches (Right <$> io)
- [ Handler $ pure . Left . IpfsDaemonError
+ [ Handler $ pure . Left . IpfsDaemonException
, Handler httpHdlr
]
where
@@ -124,9 +124,9 @@ safeIpfs io = ExceptT $ liftIO $
(Http.StatusCodeException _
(decodeStrict ->
Just (Aeson.Object (HashMap.lookup "Message" ->
- Just (Aeson.String msg))))) -> NetworkError msg
- Http.HttpExceptionRequest _ Http.ResponseTimeout -> InternalError (toS (displayException e))
- _ -> Timeout
+ Just (Aeson.String msg))))) -> IpfsDaemonErrMsg msg
+ Http.HttpExceptionRequest _ Http.ResponseTimeout -> Timeout
+ _ -> IpfsDaemonNoErrMsg
-- | Write and pin some inputs to an IPFS machine.
@@ -160,8 +160,12 @@ machineInputsFrom :: MachineId -> Maybe Ipfs.MachineEntryIndex -> ExceptT IpfsEr
machineInputsFrom (MachineId id) = safeIpfs . Ipfs.receiveIpfs id
-- | Create an IPFS machine and return its ID.
-createMachine :: ExceptT Text IO MachineId
-createMachine = ExceptT $ liftIO $ second MachineId <$> (Ipfs.ipfsMachineCreate =<< UUID.uuid)
+createMachine :: ExceptT IpfsError IO MachineId
+createMachine = do
+ id_ <- safeIpfs $ Ipfs.ipfsMachineCreate =<< UUID.uuid
+ case id_ of
+ Left e -> throwError (IpfsDaemonErrMsg e)
+ Right id -> pure (MachineId id)
-- * Topic subscriptions
diff --git a/src/Radicle/Daemon/Monad.hs b/src/Radicle/Daemon/Monad.hs
index 540cfee..760f902 100644
--- a/src/Radicle/Daemon/Monad.hs
+++ b/src/Radicle/Daemon/Monad.hs
@@ -52,25 +52,25 @@ data MachineError
| IpfsError IpfsError
| AckTimeout
| DaemonError Text
- | MachineAlreadyCached
| MachineNotCached
data Error
= MachineError MachineId MachineError
- | CouldNotCreateMachine Text
+ | CouldNotCreateMachine IpfsError
displayError :: Error -> (Text, [(Text,Text)])
displayError = \case
- CouldNotCreateMachine e -> ("Could not create IPFS machine", [("error", e)])
+ CouldNotCreateMachine (ipfsErr -> (msg, infos)) -> ("Could not create IPFS machine", ("message", msg) : infos)
MachineError id e -> let mid = ("machine-id", getMachineId id) in
case e of
- InvalidInput err -> ("Invalid radicle input", [mid, ("error", renderCompactPretty err)])
- DaemonError err -> ("Internal error", [mid, ("error", err)])
- IpfsError e' -> case e' of
- IpfsDaemonError err -> ("There was an error using IPFS", [mid, ("error", toS (displayException err))])
- InternalError err -> ("Internal error", [mid, ("error", err)])
- NetworkError err -> ("There was an error communicating with the IPFS daemon", [mid, ("error", err)])
- Timeout -> ("Timeout communicating with IPFS daemon", [mid])
- AckTimeout -> ("The writer appears to be offline", [mid])
- MachineAlreadyCached -> ("Tried to add already cached machine", [mid])
+ 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
+ IpfsDaemonErrMsg err -> ("The IPFS daemon returned an error", [("ipfs-error", err)])
+ IpfsDaemonException exc -> ("There was an error using the IPFS daemon", [("ipfs-error", toS (displayException exc))])
+ IpfsDaemonNoErrMsg -> ("There was an unknown error using IPFS", [])
+ Timeout -> ("Timeout communicating with IPFS daemon", [])
diff --git a/src/Radicle/Internal/Core.hs b/src/Radicle/Internal/Core.hs
index 727e6f2..c78e2db 100644
--- a/src/Radicle/Internal/Core.hs
+++ b/src/Radicle/Internal/Core.hs
@@ -81,6 +81,7 @@ data LangErrorData r =
| ThrownError Ident r
| SendError Text
| PatternMatchError PatternMatchError
+ | DaemonError Text
-- | Raised if the effectful @exit!@ primitive is evaluated.
| Exit Int
deriving (Eq, Show, Generic, Functor)
@@ -147,6 +148,7 @@ errorDataToValue e = case e of
( "other-error"
, [("info", String i)]
)
+ DaemonError i -> makeVal ( "daemon-error", [("info", String i)])
ParseError _ -> makeVal ("parse-error", [])
ThrownError label val -> pure (label, val)
PatternMatchError pe -> case pe of
diff --git a/src/Radicle/Internal/Pretty.hs b/src/Radicle/Internal/Pretty.hs
index 6e3cb3b..1f01f6a 100644
--- a/src/Radicle/Internal/Pretty.hs
+++ b/src/Radicle/Internal/Pretty.hs
@@ -117,6 +117,7 @@ instance PrettyV r => PrettyV (LangErrorData r) where
<+> "has exports which are not defined:"
<+> sep (prettyV . asValue . Atom <$> is)
OtherError t -> "Error:" <+> pretty t
+ DaemonError t -> "Daemon error:" <+> pretty t
SpecialForm f t -> "Error using special form" <+> pretty f <> ":" <+> pretty t <> "."
ParseError t -> "Parser error:" <+> pretty (errorBundlePretty t)
Exit code -> "Exit with code " <+> pretty code