{-# LANGUAGE LambdaCase #-}

module Cardano.CLI.Types.Errors.GovernanceQueryError where

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

data GovernanceQueryError
  = GovernanceQueryWriteFileError !(FileError ())
  | GovernanceQueryAcqireFailureError !AcquiringFailure
  | GovernanceQueryUnsupportedNtcVersion !UnsupportedNtcVersionError
  | GovernanceQueryEraMismatch !EraMismatch
  | GovernanceQueryDRepKeyError !(FileError InputDecodeError)
  deriving Int -> GovernanceQueryError -> ShowS
[GovernanceQueryError] -> ShowS
GovernanceQueryError -> String
(Int -> GovernanceQueryError -> ShowS)
-> (GovernanceQueryError -> String)
-> ([GovernanceQueryError] -> ShowS)
-> Show GovernanceQueryError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> GovernanceQueryError -> ShowS
showsPrec :: Int -> GovernanceQueryError -> ShowS
$cshow :: GovernanceQueryError -> String
show :: GovernanceQueryError -> String
$cshowList :: [GovernanceQueryError] -> ShowS
showList :: [GovernanceQueryError] -> ShowS
Show

instance Error GovernanceQueryError where
  prettyError :: forall ann. GovernanceQueryError -> Doc ann
prettyError = \case
    GovernanceQueryWriteFileError FileError ()
err ->
      FileError () -> Doc ann
forall e ann. Error e => e -> Doc ann
forall ann. FileError () -> Doc ann
prettyError FileError ()
err
    GovernanceQueryAcqireFailureError AcquiringFailure
err ->
      AcquiringFailure -> Doc ann
forall a ann. Show a => a -> Doc ann
pshow AcquiringFailure
err
    GovernanceQueryUnsupportedNtcVersion (UnsupportedNtcVersionError MinNodeToClientVersion
minNtcVersion MinNodeToClientVersion
ntcVersion) ->
      [Doc ann] -> Doc ann
forall ann. [Doc ann] -> Doc ann
vsep
        [ Doc ann
"Unsupported feature for the node-to-client protocol version."
        , Doc ann
"This query requires at least "
            Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> MinNodeToClientVersion -> Doc ann
forall a ann. Show a => a -> Doc ann
pshow MinNodeToClientVersion
minNtcVersion
            Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Doc ann
" but the node negotiated "
            Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> MinNodeToClientVersion -> Doc ann
forall a ann. Show a => a -> Doc ann
pshow MinNodeToClientVersion
ntcVersion
            Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Doc ann
"."
        , Doc ann
"Later node versions support later protocol versions (but development protocol versions are not enabled in the node by default)."
        ]
    GovernanceQueryEraMismatch EraMismatch
err ->
      Doc ann
"A query from a certain era was applied to a ledger from a different era: " Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> EraMismatch -> Doc ann
forall a ann. Show a => a -> Doc ann
pshow EraMismatch
err
    GovernanceQueryDRepKeyError FileError InputDecodeError
err ->
      Doc ann
"Error reading delegation representative key: " Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> FileError InputDecodeError -> Doc ann
forall e ann. Error e => e -> Doc ann
forall ann. FileError InputDecodeError -> Doc ann
prettyError FileError InputDecodeError
err