Skip to content

Commit

Permalink
Merge pull request #5627 from IntersectMBO/jordan/bubble-up-exceptions
Browse files Browse the repository at this point in the history
Bubble up exceptions in runNode
  • Loading branch information
carbolymer committed Jan 9, 2024
2 parents 1697b3a + d01fbb1 commit b480540
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
4 changes: 4 additions & 0 deletions cardano-node/src/Cardano/Node/Protocol.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ module Cardano.Node.Protocol
, ProtocolInstantiationError(..)
) where

import Control.Exception
import Control.Monad.Trans.Except (ExceptT)
import Control.Monad.Trans.Except.Extra (firstExceptT)

import Cardano.Api
import Cardano.Api.Pretty

import Cardano.Node.Types

Expand Down Expand Up @@ -53,6 +55,8 @@ data ProtocolInstantiationError =
| CardanoProtocolInstantiationError CardanoProtocolInstantiationError
deriving Show

instance Exception ProtocolInstantiationError where
displayException = docToString . prettyError

instance Error ProtocolInstantiationError where
prettyError (ByronProtocolInstantiationError err) = prettyError err
Expand Down
11 changes: 5 additions & 6 deletions cardano-node/src/Cardano/Node/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import Data.IP (toSockAddr)
import Control.Concurrent (killThread, mkWeakThreadId, myThreadId)
import Control.Concurrent.Class.MonadSTM.Strict
import Control.Exception (try)
import qualified Control.Exception as Exception
import Control.Monad (forM_, unless, void, when)
import Control.Monad.Class.MonadThrow (MonadThrow (..))
import Control.Monad.IO.Class (MonadIO (..))
Expand All @@ -56,7 +57,6 @@ import Network.HostName (getHostName)
import Network.Socket (Socket)
import System.Directory (canonicalizePath, createDirectoryIfMissing, makeAbsolute)
import System.Environment (lookupEnv)
import System.Exit (exitFailure)
#ifdef UNIX
import GHC.Weak (deRefWeak)
import System.Posix.Files
Expand Down Expand Up @@ -148,8 +148,7 @@ runNode cmdPc = do
case shelleyVRFFile $ ncProtocolFiles nc of
Just vrfFp -> do vrf <- runExceptT $ checkVRFFilePermissions (File vrfFp)
case vrf of
Left err ->
Text.putStrLn (renderVRFPrivateKeyFilePermissionError err) >> exitFailure
Left err -> Exception.throwIO err
Right () ->
pure ()
Nothing -> pure ()
Expand All @@ -162,7 +161,7 @@ runNode cmdPc = do

p :: SomeConsensusProtocol <-
case eitherSomeProtocol of
Left err -> putStrLn (docToString (Api.prettyError err)) >> exitFailure
Left err -> Exception.throwIO err
Right p -> pure p

let networkMagic :: Api.NetworkMagic =
Expand Down Expand Up @@ -244,7 +243,7 @@ handleNodeWithTracers cmdPc nc0 p networkMagic blockType runP = do
p

loggingLayer <- case eLoggingLayer of
Left err -> Text.putStrLn (Text.pack $ show err) >> exitFailure
Left err -> Exception.throwIO err
Right res -> return res
!trace <- setupTrace loggingLayer
let tracer = contramap pack $ toLogObject trace
Expand Down Expand Up @@ -367,7 +366,7 @@ handleSimpleNode blockType runP p2pMode tracers nc onKernel = do
Left err -> do
traceWith (startupTracer tracers)
$ StartupSocketConfigError err
throwIO err
Exception.throwIO err

dbPath <- canonDbPath nc

Expand Down
16 changes: 16 additions & 0 deletions cardano-node/src/Cardano/Node/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ module Cardano.Node.Types
, renderVRFPrivateKeyFilePermissionError
) where

import Control.Exception
import Data.Aeson
import Data.ByteString (ByteString)
import Data.Monoid (Last)
Expand All @@ -42,6 +43,7 @@ import Data.Word (Word16, Word8)
import Control.Monad (MonadPlus (..))

import Cardano.Api
import Cardano.Api.Pretty
import Cardano.Crypto (RequiresNetworkMagic (..))
import qualified Cardano.Crypto.Hash as Crypto
import Cardano.Node.Configuration.Socket (SocketConfig (..))
Expand All @@ -57,6 +59,14 @@ data ConfigError =
| ConfigErrorNoEKG
deriving Show

instance Exception ConfigError where
displayException = docToString . prettyError

instance Error ConfigError where
prettyError ConfigErrorNoEKG = "ConfigErrorNoEKG"
prettyError (ConfigErrorFileNotFound fp) =
mconcat ["ConfigErrorFileNotFound: ", pretty fp]

-- | Filepath of the configuration yaml file. This file determines
-- all the configuration settings required for the cardano node
-- (logging, tracing, protocol, slot length etc)
Expand Down Expand Up @@ -352,6 +362,12 @@ data VRFPrivateKeyFilePermissionError
| GenericPermissionsExist FilePath
deriving Show

instance Exception VRFPrivateKeyFilePermissionError where
displayException = Text.unpack . renderVRFPrivateKeyFilePermissionError

instance Error VRFPrivateKeyFilePermissionError where
prettyError = pretty . renderVRFPrivateKeyFilePermissionError

renderVRFPrivateKeyFilePermissionError :: VRFPrivateKeyFilePermissionError -> Text
renderVRFPrivateKeyFilePermissionError err =
case err of
Expand Down

0 comments on commit b480540

Please sign in to comment.