Skip to content

Commit

Permalink
Replace panic with fail in JSON parsers in:
Browse files Browse the repository at this point in the history
* Cardano.Node.Types
* Cardano.Node.Orphans
* Cardano.Tracing.OrphanInstances.Common
  • Loading branch information
intricate authored and newhoggy committed Nov 13, 2020
1 parent fc9c022 commit 6f5822d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 31 deletions.
14 changes: 7 additions & 7 deletions cardano-node/src/Cardano/Node/Orphans.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
module Cardano.Node.Orphans () where

import Cardano.Prelude
import qualified Prelude
import Prelude (fail)

import Data.Aeson (FromJSON (..), Value (..))
import qualified Data.Text as Text
Expand All @@ -19,15 +19,15 @@ instance FromJSON TracingVerbosity where
"MinimalVerbosity" -> pure MinimalVerbosity
"MaximalVerbosity" -> pure MaximalVerbosity
"NormalVerbosity" -> pure NormalVerbosity
err -> panic $ "Parsing of TracingVerbosity failed, "
<> err <> " is not a valid TracingVerbosity"
parseJSON invalid = panic $ "Parsing of TracingVerbosity failed due to type mismatch. "
<> "Encountered: " <> Text.pack (Prelude.show invalid)
err -> fail $ "Parsing of TracingVerbosity failed, "
<> Text.unpack err <> " is not a valid TracingVerbosity"
parseJSON invalid = fail $ "Parsing of TracingVerbosity failed due to type mismatch. "
<> "Encountered: " <> show invalid

deriving instance Show TracingVerbosity

instance FromJSON Update.ApplicationName where
parseJSON (String x) = pure $ Update.ApplicationName x
parseJSON invalid =
panic $ "Parsing of application name failed due to type mismatch. "
<> "Encountered: " <> Text.pack (Prelude.show invalid)
fail $ "Parsing of application name failed due to type mismatch. "
<> "Encountered: " <> show invalid
44 changes: 30 additions & 14 deletions cardano-node/src/Cardano/Node/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralisedNewtypeDeriving #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}

module Cardano.Node.Types
( -- * Configuration
Expand Down Expand Up @@ -48,7 +49,7 @@ module Cardano.Node.Types
) where

import Cardano.Prelude
import Prelude (String)
import Prelude (String, fail)

import Data.Aeson
import Data.IP (IP (..), IPv4, IPv6)
Expand Down Expand Up @@ -91,8 +92,23 @@ newtype GenesisFile = GenesisFile

instance FromJSON GenesisFile where
parseJSON (String genFp) = pure . GenesisFile $ Text.unpack genFp
parseJSON invalid = panic $ "Parsing of GenesisFile failed due to type mismatch. "
<> "Encountered: " <> Text.pack (show invalid)
parseJSON invalid = fail $ "Parsing of GenesisFile failed due to type mismatch. "
<> "Encountered: " <> show invalid

-- Node can be run in two modes.
data ViewMode = LiveView -- Live mode with TUI
| SimpleView -- Simple mode, just output text.
deriving (Eq, Show)

instance FromJSON ViewMode where
parseJSON (String str) = case str of
"LiveView" -> pure LiveView
"SimpleView" -> pure SimpleView
view -> fail $ "Parsing of ViewMode: "
<> Text.unpack view <> " failed. "
<> Text.unpack view <> " is not a valid view mode"
parseJSON invalid = fail $ "Parsing of ViewMode failed due to type mismatch. "
<> "Encountered: " <> show invalid

newtype MaxConcurrencyBulkSync = MaxConcurrencyBulkSync
{ unMaxConcurrencyBulkSync :: Word }
Expand Down Expand Up @@ -163,9 +179,9 @@ instance FromJSON NodeHostIPv4Address where
parseJSON (String ipStr) =
case readMaybe $ Text.unpack ipStr of
Just ip -> pure $ NodeHostIPv4Address ip
Nothing -> panic $ "Parsing of IPv4 failed: " <> ipStr
parseJSON invalid = panic $ "Parsing of IPv4 failed due to type mismatch. "
<> "Encountered: " <> Text.pack (show invalid) <> "\n"
Nothing -> fail $ "Parsing of IPv4 failed: " <> Text.unpack ipStr
parseJSON invalid = fail $ "Parsing of IPv4 failed due to type mismatch. "
<> "Encountered: " <> show invalid <> "\n"

instance ToJSON NodeHostIPv4Address where
toJSON (NodeHostIPv4Address ip) = String (Text.pack $ show ip)
Expand All @@ -180,9 +196,9 @@ instance FromJSON NodeHostIPv6Address where
parseJSON (String ipStr) =
case readMaybe $ Text.unpack ipStr of
Just ip -> pure $ NodeHostIPv6Address ip
Nothing -> panic $ "Parsing of IPv6 failed: " <> ipStr
parseJSON invalid = panic $ "Parsing of IPv6 failed due to type mismatch. "
<> "Encountered: " <> Text.pack (show invalid) <> "\n"
Nothing -> fail $ "Parsing of IPv6 failed: " <> Text.unpack ipStr
parseJSON invalid = fail $ "Parsing of IPv6 failed due to type mismatch. "
<> "Encountered: " <> show invalid <> "\n"
instance ToJSON NodeHostIPv6Address where
toJSON (NodeHostIPv6Address ip) = String (Text.pack $ show ip)

Expand All @@ -196,9 +212,9 @@ instance FromJSON NodeHostIPAddress where
parseJSON (String ipStr) =
case readMaybe $ Text.unpack ipStr of
Just ip -> pure $ NodeHostIPAddress ip
Nothing -> panic $ "Parsing of IP failed: " <> ipStr
parseJSON invalid = panic $ "Parsing of IP failed due to type mismatch. "
<> "Encountered: " <> Text.pack (show invalid) <> "\n"
Nothing -> fail $ "Parsing of IP failed: " <> Text.unpack ipStr
parseJSON invalid = fail $ "Parsing of IP failed due to type mismatch. "
<> "Encountered: " <> show invalid <> "\n"

instance ToJSON NodeHostIPAddress where
toJSON (NodeHostIPAddress ip) = String (Text.pack $ show ip)
Expand Down Expand Up @@ -235,8 +251,8 @@ instance FromJSON NodeDiffusionMode where
-> pure $ NodeDiffusionMode InitiatorOnlyDiffusionMode
"InitiatorAndResponder"
-> pure $ NodeDiffusionMode InitiatorAndResponderDiffusionMode
_ -> panic "Parsing NodeDiffusionMode failed: can be either 'InitiatorOnly' or 'InitiatorAndResponder'"
parseJSON _ = panic "Parsing NodeDiffusionMode failed"
_ -> fail "Parsing NodeDiffusionMode failed: can be either 'InitiatorOnly' or 'InitiatorAndResponder'"
parseJSON _ = fail "Parsing NodeDiffusionMode failed"

class AdjustFilePaths a where
adjustFilePaths :: (FilePath -> FilePath) -> a -> a
Expand Down
20 changes: 10 additions & 10 deletions cardano-node/src/Cardano/Tracing/OrphanInstances/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ module Cardano.Tracing.OrphanInstances.Common
) where

import Cardano.Prelude
import qualified Prelude
import Prelude (fail)

import Data.Aeson
import qualified Data.ByteString.Base16 as B16
Expand Down Expand Up @@ -77,23 +77,23 @@ instance FromJSON TracingVerbosity where
"MinimalVerbosity" -> pure MinimalVerbosity
"MaximalVerbosity" -> pure MaximalVerbosity
"NormalVerbosity" -> pure NormalVerbosity
err -> panic $ "Parsing of TracingVerbosity failed, "
<> err <> " is not a valid TracingVerbosity"
parseJSON invalid = panic $ "Parsing of TracingVerbosity failed due to type mismatch. "
<> "Encountered: " <> Text.pack (Prelude.show invalid)
invalid -> fail $ "Parsing of TracingVerbosity failed, "
<> Text.unpack invalid <> " is not a valid TracingVerbosity"
parseJSON invalid = fail $ "Parsing of TracingVerbosity failed due to type mismatch. "
<> "Encountered: " <> show invalid

instance FromJSON PortNumber where
parseJSON (Number portNum) = case readMaybe . show $ coefficient portNum of
Just port -> pure port
Nothing -> panic $ show portNum <> " is not a valid port number."
parseJSON invalid = panic $ "Parsing of port number failed due to type mismatch. "
<> "Encountered: " <> Text.pack (Prelude.show invalid)
Nothing -> fail $ show portNum <> " is not a valid port number."
parseJSON invalid = fail $ "Parsing of port number failed due to type mismatch. "
<> "Encountered: " <> show invalid

instance FromJSON Update.ApplicationName where
parseJSON (String x) = pure $ Update.ApplicationName x
parseJSON invalid =
panic $ "Parsing of application name failed due to type mismatch. "
<> "Encountered: " <> Text.pack (Prelude.show invalid)
fail $ "Parsing of application name failed due to type mismatch. "
<> "Encountered: " <> show invalid

instance ToJSON (HeaderHash blk) => ToJSON (Tip blk) where
toJSON TipGenesis = object [ "genesis" .= True ]
Expand Down

0 comments on commit 6f5822d

Please sign in to comment.