{-# LANGUAGE LambdaCase #-}

module Cardano.CLI.Types.Errors.DebugCmdError
  ( DebugCmdError (..)
  )
where

import           Cardano.Api
import qualified Cardano.Api.Byron as Byron

import           Cardano.CLI.Types.Errors.TxCmdError

import           Data.Text (Text)
import qualified Data.Text.Lazy.Builder as Text
import           Formatting.Buildable (build)

data DebugCmdError
  = DebugCmdFailed
  | -- | @DebugNodeConfigGenesisDataCmdError filepath error@ represents an error when
    -- reading the node configuration at @filepath@
    DebugNodeConfigGenesisDataCmdError !FilePath !Byron.GenesisDataError
  | -- | @DebugNodeConfigWrongGenesisHashCmdError filepath eraGenesisPath actualHash expectedHash@ represents a user error
    -- that the hash of @eraGenesisPath@ in @filepath@ is @actualHash@, whereas it should be @expectedHash@
    DebugNodeConfigWrongGenesisHashCmdError
      !FilePath
      -- ^ The file path of the node configuration file
      !FilePath
      -- ^ The file path of the era configuration file whose hash is wrong
      !Text
      -- ^ The actual hash (the hash found by hashing the genesis file)
      !Text
      -- ^ The expected hash (the hash mentioned in the configuration file)
  | -- | @DebugNodeConfigNoConwayFileCmdError filepath@ represents a user error
    -- that the genesis file for Conway in @filepath@ is not specified
    DebugNodeConfigNoConwayFileCmdError
      !FilePath
  | -- | @DebugNodeConfigNoConwayHashCmdError filepath@ represents a user error
    -- that the hash for the Conway genesis file in @filepath@ is not specified
    DebugNodeConfigNoConwayHashCmdError
      !FilePath
      -- ^ The file path of the node configuration file
  | DebugTxCmdError !TxCmdError

instance Error DebugCmdError where
  prettyError :: forall ann. DebugCmdError -> Doc ann
prettyError = \case
    DebugCmdError
DebugCmdFailed -> Doc ann
"Debug command failed"
    DebugNodeConfigGenesisDataCmdError FilePath
fp GenesisDataError
err ->
      Doc ann
"Error reading node configuration at: "
        Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> FilePath -> Doc ann
forall ann. FilePath -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty FilePath
fp
        Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Doc ann
": "
        Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Text -> Doc ann
forall a ann. Pretty a => a -> Doc ann
forall ann. Text -> Doc ann
pretty (Builder -> Text
Text.toLazyText (Builder -> Text) -> Builder -> Text
forall a b. (a -> b) -> a -> b
$ GenesisDataError -> Builder
forall p. Buildable p => p -> Builder
build GenesisDataError
err)
    DebugNodeConfigNoConwayFileCmdError FilePath
fp ->
      Doc ann
"Conway genesis file not specified in "
        Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> FilePath -> Doc ann
forall ann. FilePath -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty FilePath
fp
        Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Doc ann
". Please add a \"ConwayGenesisFile\" key to the file at "
        Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> FilePath -> Doc ann
forall ann. FilePath -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty FilePath
fp
    DebugNodeConfigNoConwayHashCmdError FilePath
fp ->
      Doc ann
"Conway genesis hash not specified in "
        Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> FilePath -> Doc ann
forall ann. FilePath -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty FilePath
fp
        Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Doc ann
". Please add a \"ConwayGenesisHash\" key to the file at "
        Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> FilePath -> Doc ann
forall ann. FilePath -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty FilePath
fp
    DebugNodeConfigWrongGenesisHashCmdError FilePath
nodeFp FilePath
genesisFp Text
actualHash Text
expectedHash ->
      Doc ann
"Wrong genesis hash for "
        Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> FilePath -> Doc ann
forall ann. FilePath -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty FilePath
genesisFp
        Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Doc ann
" in "
        Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> FilePath -> Doc ann
forall ann. FilePath -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty FilePath
nodeFp
        Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Doc ann
": when computing the hash, got: "
        Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Text -> Doc ann
forall ann. Text -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty Text
actualHash
        Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Doc ann
", but the node configuration files states that this hash is expected: "
        Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Text -> Doc ann
forall ann. Text -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty Text
expectedHash
    DebugTxCmdError TxCmdError
err -> TxCmdError -> Doc ann
forall ann. TxCmdError -> Doc ann
renderTxCmdError TxCmdError
err