From 5238d772e6a5b2edb7f6a816d753a168b7056d24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Hurlin?= Date: Tue, 14 Nov 2023 12:01:21 +0100 Subject: [PATCH] conway governance hash: Correct/introduce error handling --- .../src/Cardano/CLI/EraBased/Run/Governance/Hash.hs | 8 +++++--- .../Cardano/CLI/Types/Errors/GovernanceHashError.hs | 10 ++++++---- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/cardano-cli/src/Cardano/CLI/EraBased/Run/Governance/Hash.hs b/cardano-cli/src/Cardano/CLI/EraBased/Run/Governance/Hash.hs index 29274e9954..61298f390d 100644 --- a/cardano-cli/src/Cardano/CLI/EraBased/Run/Governance/Hash.hs +++ b/cardano-cli/src/Cardano/CLI/EraBased/Run/Governance/Hash.hs @@ -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 diff --git a/cardano-cli/src/Cardano/CLI/Types/Errors/GovernanceHashError.hs b/cardano-cli/src/Cardano/CLI/Types/Errors/GovernanceHashError.hs index c6070d5dc0..914ad52dd9 100644 --- a/cardano-cli/src/Cardano/CLI/Types/Errors/GovernanceHashError.hs +++ b/cardano-cli/src/Cardano/CLI/Types/Errors/GovernanceHashError.hs @@ -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