{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE RankNTypes #-}

module Cardano.CLI.Type.Error.StakePoolCmdError
  ( StakePoolCmdError (..)
  )
where

import Cardano.Api
import Cardano.Api.Shelley (Hash (StakePoolMetadataHash))

import Cardano.CLI.Type.Error.HashCmdError (FetchURLError)

data StakePoolCmdError
  = StakePoolCmdReadFileError !(FileError TextEnvelopeError)
  | StakePoolCmdReadKeyFileError !(FileError InputDecodeError)
  | StakePoolCmdWriteFileError !(FileError ())
  | StakePoolCmdMetadataValidationError !StakePoolMetadataValidationError
  | StakePoolCmdHashMismatchError
      !(Hash StakePoolMetadata)
      -- ^ Expected hash
      !(Hash StakePoolMetadata)
      -- ^ Actual hash
  | StakePoolCmdFetchURLError !FetchURLError
  deriving Int -> StakePoolCmdError -> ShowS
[StakePoolCmdError] -> ShowS
StakePoolCmdError -> String
(Int -> StakePoolCmdError -> ShowS)
-> (StakePoolCmdError -> String)
-> ([StakePoolCmdError] -> ShowS)
-> Show StakePoolCmdError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> StakePoolCmdError -> ShowS
showsPrec :: Int -> StakePoolCmdError -> ShowS
$cshow :: StakePoolCmdError -> String
show :: StakePoolCmdError -> String
$cshowList :: [StakePoolCmdError] -> ShowS
showList :: [StakePoolCmdError] -> ShowS
Show

instance Error StakePoolCmdError where
  prettyError :: forall ann. StakePoolCmdError -> Doc ann
prettyError = \case
    StakePoolCmdMetadataValidationError StakePoolMetadataValidationError
validationErr ->
      Doc ann
"Error validating stake pool metadata: " Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> StakePoolMetadataValidationError -> Doc ann
forall e ann. Error e => e -> Doc ann
forall ann. StakePoolMetadataValidationError -> Doc ann
prettyError StakePoolMetadataValidationError
validationErr
    StakePoolCmdReadFileError FileError TextEnvelopeError
fileErr ->
      FileError TextEnvelopeError -> Doc ann
forall e ann. Error e => e -> Doc ann
forall ann. FileError TextEnvelopeError -> Doc ann
prettyError FileError TextEnvelopeError
fileErr
    StakePoolCmdReadKeyFileError FileError InputDecodeError
fileErr ->
      FileError InputDecodeError -> Doc ann
forall e ann. Error e => e -> Doc ann
forall ann. FileError InputDecodeError -> Doc ann
prettyError FileError InputDecodeError
fileErr
    StakePoolCmdWriteFileError FileError ()
fileErr ->
      FileError () -> Doc ann
forall e ann. Error e => e -> Doc ann
forall ann. FileError () -> Doc ann
prettyError FileError ()
fileErr
    StakePoolCmdHashMismatchError
      (StakePoolMetadataHash Hash HASH ByteString
expectedHash)
      (StakePoolMetadataHash Hash HASH ByteString
actualHash) ->
        Doc ann
"Hashes do not match!"
          Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Doc ann
"\nExpected:"
            Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> String -> Doc ann
forall ann. String -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty (Hash HASH ByteString -> String
forall a. Show a => a -> String
show Hash HASH ByteString
expectedHash)
          Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Doc ann
"\n  Actual:"
            Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> String -> Doc ann
forall ann. String -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty (Hash HASH ByteString -> String
forall a. Show a => a -> String
show Hash HASH ByteString
actualHash)
    StakePoolCmdFetchURLError FetchURLError
fetchErr ->
      Doc ann
"Error fetching stake pool metadata: " Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> FetchURLError -> Doc ann
forall a ann. Exception a => a -> Doc ann
prettyException FetchURLError
fetchErr