Skip to content

Commit

Permalink
Merge pull request #186 from input-output-hk/newhoggy/move-some-error…
Browse files Browse the repository at this point in the history
…-types

Move some error types
  • Loading branch information
newhoggy authored Aug 18, 2023
2 parents 511a74b + 01ecbd3 commit 4a19503
Show file tree
Hide file tree
Showing 17 changed files with 201 additions and 187 deletions.
7 changes: 5 additions & 2 deletions cardano-cli/cardano-cli.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ library
Cardano.CLI.EraBased.Commands.Governance
Cardano.CLI.EraBased.Commands.Governance.Actions
Cardano.CLI.EraBased.Commands.Governance.Committee
Cardano.CLI.EraBased.Errors.Common
Cardano.CLI.EraBased.Errors.StakeAddress
Cardano.CLI.EraBased.Options.Common
Cardano.CLI.EraBased.Options.Governance
Cardano.CLI.EraBased.Options.Governance.Actions
Expand Down Expand Up @@ -121,6 +119,11 @@ library
Cardano.CLI.Run.Ping
Cardano.CLI.TopHandler
Cardano.CLI.Types.Common
Cardano.CLI.Types.Errors.GovernanceCmdError
Cardano.CLI.Types.Errors.ScriptDecodeError
Cardano.CLI.Types.Errors.ShelleyStakeAddressCmdError
Cardano.CLI.Types.Errors.StakeAddressDelegationError
Cardano.CLI.Types.Errors.StakeAddressRegistrationError
Cardano.CLI.Types.Governance
Cardano.CLI.Types.Key
Cardano.CLI.Types.Output
Expand Down
103 changes: 1 addition & 102 deletions cardano-cli/src/Cardano/CLI/Commands/Governance.hs
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}

module Cardano.CLI.Commands.Governance where

import Cardano.Api
import Cardano.Api.Shelley

import Cardano.Binary (DecoderError)
import Cardano.CLI.Legacy.Run.StakeAddress
import Cardano.CLI.Read (CddlError)
import Cardano.CLI.Types.Common
import Cardano.CLI.Types.Errors.GovernanceCmdError
import Cardano.CLI.Types.Governance
import Cardano.CLI.Types.Key

Expand All @@ -19,105 +16,7 @@ import Control.Monad.Trans.Except (ExceptT)
import Control.Monad.Trans.Except.Extra (firstExceptT, hoistEither, newExceptT)
import Data.Bifunctor
import qualified Data.ByteString as BS
import qualified Data.List as List
import Data.Text (Text)
import qualified Data.Text as Text
import qualified Data.Text.Encoding as Text
import Data.Text.Encoding.Error
import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.Builder as TL
import qualified Formatting.Buildable as B

data GovernanceCmdError
= -- Voting related
StakeCredGovCmdError ShelleyStakeAddressCmdError
| VotingCredentialDecodeGovCmdEror DecoderError
| WriteFileError (FileError ())
| ReadFileError (FileError InputDecodeError)
-- Governance action related
| NonUtf8EncodedConstitution UnicodeException

| GovernanceCmdTextEnvReadError !(FileError TextEnvelopeError)
| GovernanceCmdCddlError !CddlError
| GovernanceCmdKeyReadError !(FileError InputDecodeError)
| GovernanceCmdCostModelReadError !(FileError ())
| GovernanceCmdTextEnvWriteError !(FileError ())
| GovernanceCmdEmptyUpdateProposalError
| GovernanceCmdMIRCertificateKeyRewardMistmach
!FilePath
!Int
-- ^ Number of stake verification keys
!Int
-- ^ Number of reward amounts
| GovernanceCmdCostModelsJsonDecodeErr !FilePath !Text
| GovernanceCmdEmptyCostModel !FilePath
| GovernanceCmdUnexpectedKeyType
![TextEnvelopeType]
-- ^ Expected key types
| GovernanceCmdPollOutOfBoundAnswer
!Int
-- ^ Maximum answer index
| GovernanceCmdPollInvalidChoice
| GovernanceCmdDecoderError !DecoderError
| GovernanceCmdVerifyPollError !GovernancePollError
| GovernanceCmdWriteFileError !(FileError ())
-- Legacy - remove me after cardano-cli transitions to new era based structure
| GovernanceCmdMIRCertNotSupportedInConway
| GovernanceCmdGenesisDelegationNotSupportedInConway
deriving Show

instance Error GovernanceCmdError where
displayError = \case
StakeCredGovCmdError stakeAddressCmdError ->
"Stake credential error: " <> displayError stakeAddressCmdError
VotingCredentialDecodeGovCmdEror decoderError ->
"Could not decode voting credential: " <> renderDecoderError decoderError
WriteFileError fileError ->
displayError fileError
ReadFileError fileError ->
displayError fileError
NonUtf8EncodedConstitution unicodeException ->
"Constitution encoded in a format different than UTF-8: " <> show unicodeException
GovernanceCmdTextEnvReadError fileError ->
"Cannot read text envelope: " <> displayError fileError
GovernanceCmdCddlError cddlError ->
"Reading transaction CDDL file error: " <> displayError cddlError
GovernanceCmdKeyReadError fileError ->
"Cannot read key: " <> displayError fileError
GovernanceCmdCostModelReadError fileError ->
"Cannot read cost model: " <> displayError fileError
GovernanceCmdTextEnvWriteError fileError ->
displayError fileError
GovernanceCmdEmptyUpdateProposalError ->
"Empty update proposals are not allowed."
GovernanceCmdMIRCertificateKeyRewardMistmach fp nStakeVerKeys nRewards ->
"Error creating the MIR certificate at: " <> fp
<> " The number of staking keys: " <> show nStakeVerKeys
<> " and the number of reward amounts: " <> show nRewards
<> " are not equivalent."
GovernanceCmdCostModelsJsonDecodeErr fp msg ->
"Error decoding cost model: " <> Text.unpack msg <> " at: " <> fp
GovernanceCmdEmptyCostModel fp ->
"The decoded cost model was empty at: " <> fp
GovernanceCmdUnexpectedKeyType expectedTypes ->
"Unexpected poll key type; expected one of: "
<> List.intercalate ", " (show <$> expectedTypes)
GovernanceCmdPollOutOfBoundAnswer maxIdx ->
"Poll answer out of bounds. Choices are between 0 and " <> show maxIdx
GovernanceCmdPollInvalidChoice ->
"Invalid choice. Please choose from the available answers."
GovernanceCmdDecoderError decoderError ->
"Unable to decode metadata: " <> renderDecoderError decoderError
GovernanceCmdVerifyPollError pollError ->
Text.unpack (renderGovernancePollError pollError)
GovernanceCmdWriteFileError fileError ->
"Cannot write file: " <> displayError fileError
GovernanceCmdMIRCertNotSupportedInConway ->
"MIR certificates are not supported in Conway era onwards."
GovernanceCmdGenesisDelegationNotSupportedInConway ->
"Genesis delegation is not supported in Conway era onwards."
where
renderDecoderError = TL.unpack . TL.toLazyText . B.build

runGovernanceCreateVoteCmd
:: AnyShelleyBasedEra
Expand Down
49 changes: 0 additions & 49 deletions cardano-cli/src/Cardano/CLI/EraBased/Errors/StakeAddress.hs

This file was deleted.

2 changes: 1 addition & 1 deletion cardano-cli/src/Cardano/CLI/EraBased/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ module Cardano.CLI.EraBased.Run

import Cardano.Api

import Cardano.CLI.Commands.Governance
import Cardano.CLI.EraBased.Commands
import Cardano.CLI.EraBased.Options.Governance
import Cardano.CLI.EraBased.Run.Certificate
import Cardano.CLI.EraBased.Run.Governance
import Cardano.CLI.EraBased.Run.Governance.Actions
import Cardano.CLI.EraBased.Run.Governance.Committee
import Cardano.CLI.EraBased.Vote
import Cardano.CLI.Types.Errors.GovernanceCmdError

import Control.Monad.Trans.Except
import Control.Monad.Trans.Except.Extra (firstExceptT)
Expand Down
3 changes: 2 additions & 1 deletion cardano-cli/src/Cardano/CLI/EraBased/Run/Certificate.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ import Cardano.Api
import qualified Cardano.Api.Ledger as Ledger
import Cardano.Api.Shelley

import Cardano.CLI.EraBased.Errors.StakeAddress
import Cardano.CLI.Read
import Cardano.CLI.Types.Common
import Cardano.CLI.Types.Errors.ShelleyStakeAddressCmdError
import Cardano.CLI.Types.Errors.StakeAddressRegistrationError
import Cardano.CLI.Types.Key

import Control.Monad.Trans.Class
Expand Down
3 changes: 1 addition & 2 deletions cardano-cli/src/Cardano/CLI/EraBased/Run/Governance.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ import Cardano.Api
import qualified Cardano.Api.Ledger as Ledger
import Cardano.Api.Shelley

import Cardano.CLI.Commands.Governance
import Cardano.CLI.Types.Common
import Cardano.CLI.Types.Errors.GovernanceCmdError
import qualified Cardano.Ledger.Shelley.TxBody as Shelley

import Control.Monad

import Control.Monad.IO.Class (liftIO)
import Control.Monad.Trans.Except (ExceptT)
import Control.Monad.Trans.Except.Extra
Expand Down
15 changes: 7 additions & 8 deletions cardano-cli/src/Cardano/CLI/EraBased/Run/Governance/Actions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,23 @@ import qualified Data.ByteString as BS
import qualified Data.Text.Encoding as Text
import Data.Text.Encoding.Error


data GovernanceActionsError
= GovernanceActionsCmdWriteFileError (FileError ())
= GovernanceActionsCmdNonUtf8EncodedConstitution UnicodeException
| GovernanceActionsCmdReadFileError (FileError InputDecodeError)
| GovernanceActionsCmdReadTextEnvelopeFileError (FileError TextEnvelopeError)
| GovernanceActionsCmdNonUtf8EncodedConstitution UnicodeException
| GovernanceActionsCmdWriteFileError (FileError ())
deriving Show

instance Error GovernanceActionsError where
displayError = \case
GovernanceActionsCmdWriteFileError e ->
"Cannot write file: " <> displayError e
GovernanceActionsCmdNonUtf8EncodedConstitution e ->
"Cannot read constitution: " <> show e
GovernanceActionsCmdReadFileError e ->
"Cannot read file: " <> displayError e
GovernanceActionsCmdReadTextEnvelopeFileError e ->
"Cannot read text envelope from file: " <> displayError e
GovernanceActionsCmdNonUtf8EncodedConstitution e ->
"Cannot read constitution: " <> show e
"Cannot read text envelope file: " <> displayError e
GovernanceActionsCmdWriteFileError e ->
"Cannot write file: " <> displayError e

runGovernanceActionCmds :: ()
=> GovernanceActionCmds era
Expand Down
2 changes: 1 addition & 1 deletion cardano-cli/src/Cardano/CLI/Legacy/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ module Cardano.CLI.Legacy.Run

import Cardano.Api

import Cardano.CLI.Commands.Governance
import Cardano.CLI.Legacy.Options
import Cardano.CLI.Legacy.Run.Address
import Cardano.CLI.Legacy.Run.Genesis
Expand All @@ -20,6 +19,7 @@ import Cardano.CLI.Legacy.Run.Query
import Cardano.CLI.Legacy.Run.StakeAddress
import Cardano.CLI.Legacy.Run.TextView
import Cardano.CLI.Legacy.Run.Transaction
import Cardano.CLI.Types.Errors.GovernanceCmdError

import Control.Monad.Trans.Except (ExceptT)
import Control.Monad.Trans.Except.Extra (firstExceptT)
Expand Down
1 change: 1 addition & 0 deletions cardano-cli/src/Cardano/CLI/Legacy/Run/Governance.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import Cardano.CLI.EraBased.Run.Governance
import Cardano.CLI.Legacy.Commands.Governance
import Cardano.CLI.Read (fileOrPipe, readFileTx)
import Cardano.CLI.Types.Common
import Cardano.CLI.Types.Errors.GovernanceCmdError
import Cardano.CLI.Types.Governance
import qualified Cardano.CLI.Types.Governance as Cli
import Cardano.CLI.Types.Key (VerificationKeyOrHashOrFile,
Expand Down
4 changes: 3 additions & 1 deletion cardano-cli/src/Cardano/CLI/Legacy/Run/StakeAddress.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ import Cardano.Api
import qualified Cardano.Api.Ledger as Ledger
import Cardano.Api.Shelley

import Cardano.CLI.EraBased.Errors.StakeAddress
import Cardano.CLI.EraBased.Run.Certificate
import Cardano.CLI.Legacy.Commands.StakeAddress
import Cardano.CLI.Types.Common
import Cardano.CLI.Types.Errors.ShelleyStakeAddressCmdError
import Cardano.CLI.Types.Errors.StakeAddressDelegationError
import Cardano.CLI.Types.Errors.StakeAddressRegistrationError
import Cardano.CLI.Types.Key (DelegationTarget (..), StakeIdentifier (..),
StakeVerifier (..), VerificationKeyOrFile, readVerificationKeyOrFile,
readVerificationKeyOrHashOrFile)
Expand Down
18 changes: 1 addition & 17 deletions cardano-cli/src/Cardano/CLI/Read.hs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ import Cardano.Api.Shelley

import qualified Cardano.Binary as CBOR
import Cardano.CLI.Types.Common
import Cardano.CLI.Types.Errors.ScriptDecodeError
import Cardano.CLI.Types.Governance

import Prelude
Expand Down Expand Up @@ -379,23 +380,6 @@ readScriptDataOrFile (ScriptDataCborFile fp) = do
$ hoistEither $ validateScriptData $ getScriptData hSd
return hSd


--
-- Handling decoding the variety of script languages and formats
--

data ScriptDecodeError =
ScriptDecodeTextEnvelopeError TextEnvelopeError
| ScriptDecodeSimpleScriptError JsonDecodeError
deriving Show

instance Error ScriptDecodeError where
displayError (ScriptDecodeTextEnvelopeError err) =
"Error decoding script: " ++ displayError err
displayError (ScriptDecodeSimpleScriptError err) =
"Syntax error in script: " ++ displayError err


-- | Read a script file. The file can either be in the text envelope format
-- wrapping the binary representation of any of the supported script languages,
-- or alternatively it can be a JSON format file for one of the simple script
Expand Down
Loading

0 comments on commit 4a19503

Please sign in to comment.