Skip to content

Commit

Permalink
conway governance hash: Correct/introduce error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
smelc committed Nov 14, 2023
1 parent 77b7edb commit 5238d77
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
8 changes: 5 additions & 3 deletions cardano-cli/src/Cardano/CLI/EraBased/Run/Governance/Hash.hs
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,18 @@ runGovernanceHashCmd Cmd.GovernanceHashCmdArgs { toHash } =
-- This can be done in a separate PR
case toHash of
Cmd.GovernanceHashSourceBinaryFile fp -> do
bytes <- liftIO $ BS.readFile $ unFile fp
let path = unFile fp
bytes <- handleIOExceptT (GovernanceHashReadFileError path) $ BS.readFile path
let hash = Ledger.hashAnchorData $ Ledger.AnchorData bytes
printHash hash
Cmd.GovernanceHashSourceTextFile fp -> do
text <- liftIO $ Text.readFile $ unFile fp
let path = unFile fp
text <- handleIOExceptT (GovernanceHashReadFileError path) $ Text.readFile path
let hash = Ledger.hashAnchorData $ Ledger.AnchorData $ Text.encodeUtf8 text
printHash hash
Cmd.GovernanceHashSourceText text -> do
let hash = Ledger.hashAnchorData $ Ledger.AnchorData $ Text.encodeUtf8 text
printHash hash
where
printHash :: Ledger.SafeHash StandardCrypto i -> ExceptT GovernanceHashError IO ()
printHash :: Ledger.SafeHash StandardCrypto i -> ExceptT a IO ()
printHash = liftIO . putStr . Text.unpack . hashToTextAsHex . extractHash
10 changes: 6 additions & 4 deletions cardano-cli/src/Cardano/CLI/Types/Errors/GovernanceHashError.hs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
{-# LANGUAGE LambdaCase #-}
module Cardano.CLI.Types.Errors.GovernanceHashError
( GovernanceHashError(..)
) where

import Cardano.Api

import Cardano.Prelude (UnicodeException)
import Cardano.Prelude (Exception (displayException), IOException)

data GovernanceHashError
= GovernanceHashReadFileError (FileError InputDecodeError)
| GovernanceHashUnicodeError UnicodeException
= GovernanceHashReadFileError FilePath IOException
deriving Show

instance Error GovernanceHashError where
displayError = undefined
displayError = \case
GovernanceHashReadFileError filepath exc ->
"Cannot read " <> filepath <> ": " <> displayException exc

0 comments on commit 5238d77

Please sign in to comment.