From 6ebdc978efe1f4c745aae350a56fe457dbcbeb20 Mon Sep 17 00:00:00 2001 From: Marcin Szamotulski Date: Thu, 12 Sep 2024 12:56:48 +0200 Subject: [PATCH] cardano-ping: report user friendly error on misconfiguration --- cardano-cli/src/Cardano/CLI/Commands/Ping.hs | 14 ++++++++++++++ cardano-cli/src/Cardano/CLI/Run/Ping.hs | 9 ++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/cardano-cli/src/Cardano/CLI/Commands/Ping.hs b/cardano-cli/src/Cardano/CLI/Commands/Ping.hs index b45bb87231..d873825ab9 100644 --- a/cardano-cli/src/Cardano/CLI/Commands/Ping.hs +++ b/cardano-cli/src/Cardano/CLI/Commands/Ping.hs @@ -1,6 +1,7 @@ module Cardano.CLI.Commands.Ping ( EndPoint (..) , PingCmd (..) + , getConfigurationError ) where @@ -22,3 +23,16 @@ data PingCmd = PingCmd , pingOptsGetTip :: !Bool } deriving (Eq, Show) + +getConfigurationError :: PingCmd -> Maybe String +getConfigurationError + PingCmd + { pingCmdEndPoint = endPoint + , pingOptsGetTip = getTip + , pingOptsHandshakeQuery = query + } = + case endPoint of + UnixSockEndPoint{} + | query || getTip -> Nothing + | otherwise -> Just "Unix sockets only support queries for available versions or a tip." + HostEndPoint{} -> Nothing diff --git a/cardano-cli/src/Cardano/CLI/Run/Ping.hs b/cardano-cli/src/Cardano/CLI/Run/Ping.hs index 2cbaaa6c39..5ac32a09db 100644 --- a/cardano-cli/src/Cardano/CLI/Run/Ping.hs +++ b/cardano-cli/src/Cardano/CLI/Run/Ping.hs @@ -18,6 +18,7 @@ import qualified Control.Concurrent.Class.MonadSTM.Strict as STM import Control.Exception (SomeException) import Control.Monad (forM, unless) import Control.Monad.Class.MonadAsync (MonadAsync (async, wait, waitCatch)) +import Control.Monad.Except (throwError) import Control.Monad.Trans.Except (ExceptT) import Control.Monad.Trans.Except.Extra (left) import Control.Tracer (Tracer (..)) @@ -28,7 +29,9 @@ import qualified Network.Socket as Socket import qualified System.Exit as IO import qualified System.IO as IO -newtype PingClientCmdError = PingClientCmdError [(AddrInfo, SomeException)] +data PingClientCmdError + = PingClientCmdError [(AddrInfo, SomeException)] + | PingClientMisconfigurationError String maybeHostEndPoint :: EndPoint -> Maybe String maybeHostEndPoint = \case @@ -58,6 +61,9 @@ pingClient stdout stderr cmd = CNP.pingClient stdout stderr opts } runPingCmd :: PingCmd -> ExceptT PingClientCmdError IO () +runPingCmd options + | Just err <- getConfigurationError options = + throwError $ PingClientMisconfigurationError err runPingCmd options = do let hints = Socket.defaultHints{Socket.addrSocketType = Socket.Stream} @@ -120,3 +126,4 @@ runPingCmd options = do renderPingClientCmdError :: PingClientCmdError -> Doc ann renderPingClientCmdError = \case PingClientCmdError es -> mconcat $ List.intersperse "\n" $ pshow <$> es + PingClientMisconfigurationError err -> pretty err