Skip to content

Commit

Permalink
Suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
Jimbo4350 committed Mar 17, 2021
1 parent fc0afd7 commit 862f692
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
6 changes: 2 additions & 4 deletions cardano-submit-api/src/Cardano/TxSubmit/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import Cardano.Binary (DecoderError)
import Cardano.TxSubmit.Util (textShow)
import Data.Aeson (ToJSON (..), Value (..))
import Data.ByteString.Char8 (ByteString)
import Data.List.NonEmpty (NonEmpty)
import Data.Text (Text)
import Formatting (build, sformat)
import GHC.Generics (Generic)
Expand All @@ -32,17 +31,16 @@ import Servant.API.Generic (ToServantApi, (:-))

import qualified Data.ByteString.Lazy.Char8 as LBS
import qualified Data.List as L
import qualified Data.List.NonEmpty as NEL

newtype TxSubmitPort = TxSubmitPort Int

-- | The errors that the raw CBOR transaction parsing\/decoding functions can return.
--
newtype RawCborDecodeError = RawCborDecodeError (NonEmpty DecoderError)
newtype RawCborDecodeError = RawCborDecodeError [DecoderError]
deriving (Eq, Show)

instance Error RawCborDecodeError where
displayError (RawCborDecodeError decodeErrors) = "RawCborDecodeError decode error: \n" <> L.intercalate " \n" (fmap show (NEL.toList decodeErrors))
displayError (RawCborDecodeError decodeErrors) = "RawCborDecodeError decode error: \n" <> L.intercalate " \n" (fmap show decodeErrors)

-- | An error that can occur in the transaction submission web API.
data TxSubmitWebApiError
Expand Down
14 changes: 9 additions & 5 deletions cardano-submit-api/src/Cardano/TxSubmit/Web.hs
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,18 @@ deserialiseAnyOf :: forall b. ()
=> [FromSomeType SerialiseAsCBOR b]
-> ByteString
-> Either RawCborDecodeError b
deserialiseAnyOf ts te = let (es, as) = partitionEithers results in maybe (errors es) Right (listToMaybe as)
deserialiseAnyOf ts te = getResult $ partitionEithers results

where
getResult :: ([DecoderError], [b]) -> Either RawCborDecodeError b
getResult (dErrors, []) = Left $ RawCborDecodeError dErrors
getResult (_, [result]) = Right result
getResult (_dErrors, _results) =
Left $ error "May want to make RawCborDecodeError a sum type to account for this case"

results = fmap (`deserialiseOne` te) ts

errors :: [DecoderError] -> Either RawCborDecodeError b
errors es = case NEL.nonEmpty es of
Just fs -> Left (RawCborDecodeError fs)
Nothing -> Left (RawCborDecodeError (pure DecoderErrorVoid)) -- Should never hapen


readByteStringTx :: ByteString -> ExceptT TxCmdError IO (InAnyCardanoEra Tx)
readByteStringTx = firstExceptT TxCmdTxReadError . hoistEither . deserialiseAnyOf
Expand Down

0 comments on commit 862f692

Please sign in to comment.