Skip to content

Commit aa3620a

Browse files
committed
remove panic from read node configs
1 parent 6e13e85 commit aa3620a

File tree

3 files changed

+56
-42
lines changed

3 files changed

+56
-42
lines changed

cardano-db-sync/src/Cardano/DbSync/Config.hs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@ import qualified Cardano.BM.Setup as Logging
2727
import Cardano.BM.Trace (Trace)
2828
import qualified Cardano.BM.Trace as Logging
2929
import Cardano.DbSync.Config.Cardano
30-
import Cardano.DbSync.Config.Node
30+
import Cardano.DbSync.Config.Node (NodeConfig(..), parseSyncPreConfig, readByteStringFromFile, parseNodeConfig)
3131
import Cardano.DbSync.Config.Shelley
3232
import Cardano.DbSync.Config.Types
33-
import Cardano.DbSync.Error (runOrThrowIO)
3433
import Cardano.Prelude
3534
import System.FilePath (takeDirectory, (</>))
3635

@@ -45,14 +44,14 @@ configureLogging params loggingName = do
4544

4645
readSyncNodeConfig :: ConfigFile -> IO SyncNodeConfig
4746
readSyncNodeConfig (ConfigFile fp) = do
48-
pcfg <- runOrThrowIO . parseSyncPreConfig =<< runOrThrowIO (readByteStringFromFile fp "DbSync")
49-
let afp = adjustNodeFilePath pcfg
50-
ncfg <- runOrThrowIO . parseNodeConfig =<< runOrThrowIO (readByteStringFromFile (pcNodeConfigFilePath afp) "node")
51-
coalesceConfig afp ncfg (mkAdjustPath pcfg)
47+
pcfg <- (adjustNodeFilePath . parseSyncPreConfig) =<< readByteStringFromFile fp "DbSync"
48+
ncfg <- parseNodeConfig =<< readByteStringFromFile (pcNodeConfigFilePath pcfg) "node"
49+
coalesceConfig pcfg ncfg (mkAdjustPath pcfg)
5250
where
53-
adjustNodeFilePath :: SyncPreConfig -> SyncPreConfig
54-
adjustNodeFilePath cfg =
55-
cfg {pcNodeConfigFile = adjustNodeConfigFilePath (takeDirectory fp </>) (pcNodeConfigFile cfg)}
51+
adjustNodeFilePath :: IO SyncPreConfig -> IO SyncPreConfig
52+
adjustNodeFilePath spc = do
53+
cfg <- spc
54+
pure $ cfg {pcNodeConfigFile = adjustNodeConfigFilePath (takeDirectory fp </>) (pcNodeConfigFile cfg)}
5655

5756
coalesceConfig ::
5857
SyncPreConfig ->

cardano-db-sync/src/Cardano/DbSync/Config/Node.hs

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ module Cardano.DbSync.Config.Node (
1818
import qualified Cardano.Chain.Update as Byron
1919
import Cardano.Crypto (RequiresNetworkMagic (..))
2020
import Cardano.DbSync.Config.Types
21+
import Cardano.DbSync.Error (NodeConfigError (..), SyncNodeError (..))
2122
import Cardano.Prelude
2223
import Data.Aeson (FromJSON (..), Object, (.:), (.:?))
2324
import qualified Data.Aeson as Aeson
2425
import Data.Aeson.Types (Parser)
2526
import qualified Data.ByteString.Char8 as BS
26-
import Data.String (String)
2727
import qualified Data.Yaml as Yaml
2828
import qualified Ouroboros.Consensus.Cardano.CanHardFork as Shelley
2929

@@ -52,32 +52,22 @@ data NodeConfig = NodeConfig
5252
ncConwayHardFork :: !Shelley.TriggerHardFork
5353
}
5454

55-
data NodeConfigParseError
56-
= NodeConfigParseError String
57-
| ParseSyncPreConfigError String
58-
| ReadByteStringFromFileError String
59-
deriving (Show)
60-
61-
instance Exception NodeConfigParseError
62-
63-
parseNodeConfig :: ByteString -> IO (Either NodeConfigParseError NodeConfig)
55+
parseNodeConfig :: ByteString -> IO NodeConfig
6456
parseNodeConfig bs =
6557
case Yaml.decodeEither' bs of
66-
Left err -> pure $ Left $ NodeConfigParseError ("Error parsing node config: " <> show err)
67-
Right nc -> pure $ Right nc
58+
Left err -> throwIO $ SNErrNodeConfig $ NodeConfigParseError (show err)
59+
Right nc -> pure nc
6860

69-
parseSyncPreConfig :: ByteString -> IO (Either NodeConfigParseError SyncPreConfig)
61+
parseSyncPreConfig :: ByteString -> IO SyncPreConfig
7062
parseSyncPreConfig bs =
71-
case Yaml.decodeEither' bs of
72-
Left err -> pure $ Left $ ParseSyncPreConfigError ("Error parseSyncPreConfig: Error parsing config: " <> show err)
73-
Right res -> pure $ Right res
74-
75-
readByteStringFromFile :: FilePath -> Text -> IO (Either NodeConfigParseError ByteString)
76-
readByteStringFromFile fp cfgType = do
77-
result <- try (BS.readFile fp) :: IO (Either SomeException BS.ByteString)
78-
case result of
79-
Left _ -> pure $ Left $ ReadByteStringFromFileError ("Error Cannot find the " <> show cfgType <> " configuration file at : " <> show fp)
80-
Right res -> pure $ Right res
63+
case Yaml.decodeEither' bs of
64+
Left err -> throwIO $ SNErrNodeConfig $ ParseSyncPreConfigError ("Error parsing config: " <> show err)
65+
Right res -> pure res
66+
67+
readByteStringFromFile :: FilePath -> Text -> IO ByteString
68+
readByteStringFromFile fp cfgType =
69+
catch (BS.readFile fp) $ \(_ :: IOException) ->
70+
throwIO $ SNErrNodeConfig $ NodeConfigParseError ("Cannot find the " <> show cfgType <> " configuration file at : " <> show fp)
8171

8272
-- -------------------------------------------------------------------------------------------------
8373

cardano-db-sync/src/Cardano/DbSync/Error.hs

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
module Cardano.DbSync.Error (
77
SyncInvariant (..),
88
SyncNodeError (..),
9+
NodeConfigError(..),
910
annotateInvariantTx,
1011
bsBase16Encode,
1112
dbSyncNodeError,
@@ -50,17 +51,19 @@ data SyncNodeError
5051
| SNErrCardanoConfig !Text
5152
| SNErrInsertGenesis !String
5253
| SNErrLedgerState !String
54+
| SNErrNodeConfig NodeConfigError
5355

5456
instance Exception SyncNodeError
5557

5658
instance Show SyncNodeError where
5759
show =
5860
\case
59-
SNErrDefault t -> "Error: " <> Text.show t
60-
SNErrInvariant loc i -> Text.show loc <> ": " <> Text.show (renderSyncInvariant i)
61+
SNErrDefault t -> "Error SNErrDefault: " <> Text.show t
62+
SNErrInvariant loc i -> "Error SNErrInvariant: " <> Text.show loc <> ": " <> Text.show (renderSyncInvariant i)
6163
SNEErrBlockMismatch blkNo hashDb hashBlk ->
6264
mconcat
63-
[ "Block mismatch for block number "
65+
[ "Error SNEErrBlockMismatch: "
66+
, "Block mismatch for block number "
6467
, show blkNo
6568
, ", db has "
6669
, Text.show $ bsBase16Encode hashDb
@@ -69,38 +72,60 @@ instance Show SyncNodeError where
6972
]
7073
SNErrIgnoreShelleyInitiation ->
7174
mconcat
72-
[ "Node configs that don't fork to Shelley directly and initiate"
75+
[ "Error SNErrIgnoreShelleyInitiation: "
76+
, "Node configs that don't fork to Shelley directly and initiate"
7377
, " funds or stakes in Shelley Genesis are not supported."
7478
]
7579
SNErrByronConfig fp ce ->
7680
mconcat
77-
[ "Failed reading Byron genesis file "
81+
[ "Error SNErrByronConfig: "
82+
, "Failed reading Byron genesis file "
7883
, Text.show $ textShow fp
7984
, ": "
8085
, Text.show $ textShow ce
8186
]
8287
SNErrShelleyConfig fp txt ->
8388
mconcat
84-
[ "Failed reading Shelley genesis file "
89+
[ "Error SNErrShelleyConfig: "
90+
, "Failed reading Shelley genesis file "
8591
, Text.show $ textShow fp
8692
, ": "
8793
, show txt
8894
]
8995
SNErrAlonzoConfig fp txt ->
9096
mconcat
91-
[ "Failed reading Alonzo genesis file "
97+
["Error SNErrAlonzoConfig: "
98+
, "Failed reading Alonzo genesis file "
9299
, Text.show $ textShow fp
93100
, ": "
94101
, show txt
95102
]
96103
SNErrCardanoConfig err ->
97104
mconcat
98-
[ "With Cardano protocol, Byron/Shelley config mismatch:\n"
105+
[ "Error SNErrCardanoConfig: "
106+
, "With Cardano protocol, Byron/Shelley config mismatch:\n"
99107
, " "
100108
, show err
101109
]
102-
SNErrInsertGenesis err -> "Error InsertGenesis: " <> err
103-
SNErrLedgerState err -> "Error Ledger State: " <> err
110+
SNErrInsertGenesis err -> "Error SNErrInsertGenesis: " <> err
111+
SNErrLedgerState err -> "Error SNErrLedgerState: " <> err
112+
SNErrNodeConfig err -> "Error SNErrNodeConfig: " <> show err
113+
114+
data NodeConfigError
115+
= NodeConfigParseError String
116+
| ParseSyncPreConfigError String
117+
| ReadByteStringFromFileError String
118+
119+
instance Exception NodeConfigError
120+
121+
instance Show NodeConfigError where
122+
show =
123+
\case
124+
NodeConfigParseError err -> "NodeConfigParseError - " <> err
125+
ParseSyncPreConfigError err -> "ParseSyncPreConfigError - " <> err
126+
ReadByteStringFromFileError err -> "ReadByteStringFromFileError - " <> err
127+
128+
104129

105130
annotateInvariantTx :: Byron.Tx -> SyncInvariant -> SyncInvariant
106131
annotateInvariantTx tx ei =

0 commit comments

Comments
 (0)