{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}

module Cardano.CLI.Types.Errors.ItnKeyConversionError
  ( ItnKeyConversionError (..)
  , renderConversionError
  )
where

import           Cardano.Api

import           Control.Exception (Exception (..), IOException)
import           Data.ByteString (ByteString)
import qualified Data.ByteString.Char8 as BSC

-- | An error that can occur while converting an Incentivized Testnet (ITN)
-- key.
data ItnKeyConversionError
  = ItnKeyBech32DecodeError !Bech32DecodeError
  | ItnReadBech32FileError !FilePath !IOException
  | ItnSigningKeyDeserialisationError !ByteString
  | ItnVerificationKeyDeserialisationError !ByteString
  deriving Int -> ItnKeyConversionError -> ShowS
[ItnKeyConversionError] -> ShowS
ItnKeyConversionError -> String
(Int -> ItnKeyConversionError -> ShowS)
-> (ItnKeyConversionError -> String)
-> ([ItnKeyConversionError] -> ShowS)
-> Show ItnKeyConversionError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ItnKeyConversionError -> ShowS
showsPrec :: Int -> ItnKeyConversionError -> ShowS
$cshow :: ItnKeyConversionError -> String
show :: ItnKeyConversionError -> String
$cshowList :: [ItnKeyConversionError] -> ShowS
showList :: [ItnKeyConversionError] -> ShowS
Show

-- | Render an error message for an 'ItnKeyConversionError'.
renderConversionError :: ItnKeyConversionError -> Doc ann
renderConversionError :: forall ann. ItnKeyConversionError -> Doc ann
renderConversionError = \case
  ItnKeyBech32DecodeError Bech32DecodeError
decErr ->
    Doc ann
"Error decoding Bech32 key: " Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Bech32DecodeError -> Doc ann
forall e ann. Error e => e -> Doc ann
forall ann. Bech32DecodeError -> Doc ann
prettyError Bech32DecodeError
decErr
  ItnReadBech32FileError String
fp IOException
readErr ->
    Doc ann
"Error reading Bech32 key at: "
      Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> String -> Doc ann
forall a ann. Show a => a -> Doc ann
pshow String
fp
      Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Doc ann
" Error: "
      Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> String -> Doc ann
forall a ann. Show a => a -> Doc ann
pshow (IOException -> String
forall e. Exception e => e -> String
displayException IOException
readErr)
  ItnSigningKeyDeserialisationError ByteString
_sKey ->
    -- Sensitive data, such as the signing key, is purposely not included in
    -- the error message.
    Doc ann
"Error deserialising signing key."
  ItnVerificationKeyDeserialisationError ByteString
vKey ->
    Doc ann
"Error deserialising verification key: " Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> String -> Doc ann
forall a ann. Show a => a -> Doc ann
pshow (ByteString -> String
BSC.unpack ByteString
vKey)