{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}

module Cardano.CLI.Types.Errors.QueryCmdLocalStateQueryError
  ( QueryCmdLocalStateQueryError (..)
  , mkEraMismatchError
  )
where

import           Cardano.Api
import           Cardano.Api.Consensus (EraMismatch (..))

import           Cardano.CLI.Types.Errors.NodeEraMismatchError

-- | An error that can occur while querying a node's local state.
newtype QueryCmdLocalStateQueryError
  = -- | A query from a certain era was applied to a ledger from a different era.
    EraMismatchError EraMismatch
  deriving (QueryCmdLocalStateQueryError
-> QueryCmdLocalStateQueryError -> Bool
(QueryCmdLocalStateQueryError
 -> QueryCmdLocalStateQueryError -> Bool)
-> (QueryCmdLocalStateQueryError
    -> QueryCmdLocalStateQueryError -> Bool)
-> Eq QueryCmdLocalStateQueryError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: QueryCmdLocalStateQueryError
-> QueryCmdLocalStateQueryError -> Bool
== :: QueryCmdLocalStateQueryError
-> QueryCmdLocalStateQueryError -> Bool
$c/= :: QueryCmdLocalStateQueryError
-> QueryCmdLocalStateQueryError -> Bool
/= :: QueryCmdLocalStateQueryError
-> QueryCmdLocalStateQueryError -> Bool
Eq, Int -> QueryCmdLocalStateQueryError -> ShowS
[QueryCmdLocalStateQueryError] -> ShowS
QueryCmdLocalStateQueryError -> String
(Int -> QueryCmdLocalStateQueryError -> ShowS)
-> (QueryCmdLocalStateQueryError -> String)
-> ([QueryCmdLocalStateQueryError] -> ShowS)
-> Show QueryCmdLocalStateQueryError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> QueryCmdLocalStateQueryError -> ShowS
showsPrec :: Int -> QueryCmdLocalStateQueryError -> ShowS
$cshow :: QueryCmdLocalStateQueryError -> String
show :: QueryCmdLocalStateQueryError -> String
$cshowList :: [QueryCmdLocalStateQueryError] -> ShowS
showList :: [QueryCmdLocalStateQueryError] -> ShowS
Show)

mkEraMismatchError :: NodeEraMismatchError -> QueryCmdLocalStateQueryError
mkEraMismatchError :: NodeEraMismatchError -> QueryCmdLocalStateQueryError
mkEraMismatchError NodeEraMismatchError{CardanoEra nodeEra
nodeEra :: CardanoEra nodeEra
nodeEra :: ()
nodeEra, CardanoEra era
era :: CardanoEra era
era :: ()
era} =
  EraMismatch -> QueryCmdLocalStateQueryError
EraMismatchError
    EraMismatch
      { ledgerEraName :: Text
ledgerEraName = Doc AnsiStyle -> Text
docToText (Doc AnsiStyle -> Text) -> Doc AnsiStyle -> Text
forall a b. (a -> b) -> a -> b
$ CardanoEra nodeEra -> Doc AnsiStyle
forall a ann. Pretty a => a -> Doc ann
forall ann. CardanoEra nodeEra -> Doc ann
pretty CardanoEra nodeEra
nodeEra
      , otherEraName :: Text
otherEraName = Doc AnsiStyle -> Text
docToText (Doc AnsiStyle -> Text) -> Doc AnsiStyle -> Text
forall a b. (a -> b) -> a -> b
$ CardanoEra era -> Doc AnsiStyle
forall a ann. Pretty a => a -> Doc ann
forall ann. CardanoEra era -> Doc ann
pretty CardanoEra era
era
      }

instance Error QueryCmdLocalStateQueryError where
  prettyError :: forall ann. QueryCmdLocalStateQueryError -> Doc ann
prettyError = \case
    EraMismatchError EraMismatch{Text
ledgerEraName :: EraMismatch -> Text
ledgerEraName :: Text
ledgerEraName, Text
otherEraName :: EraMismatch -> Text
otherEraName :: Text
otherEraName} ->
      Doc ann
"A query from"
        Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> Text -> Doc ann
forall ann. Text -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty Text
otherEraName
        Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> Doc ann
"era was applied to a ledger from a different era:"
        Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> Text -> Doc ann
forall ann. Text -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty Text
ledgerEraName