Skip to content

Commit

Permalink
using throwError
Browse files Browse the repository at this point in the history
  • Loading branch information
silky committed Jun 8, 2021
1 parent c649140 commit 5c47477
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
27 changes: 11 additions & 16 deletions plutus-pab/src/Plutus/PAB/Db/Beam/ContractStore.hs
Expand Up @@ -72,24 +72,11 @@ mkContracts xs =
uuidStr :: ContractInstanceId -> Text
uuidStr = toText . unContractInstanceId

extractState
:: Maybe ContractInstance
-> State ContractExe
-- TODO: use 'throwError' instead
extractState Nothing = error "Couldn't find contract instance"
extractState (Just c) =
fromMaybe (error "No state found for this contract instance")
(join (decodeText <$> (_contractInstanceState c)))
where
decodeText = decode . toLazyByteString . encodeUtf8Builder

handleContractStore ::
forall effs.
( Member DbStoreEffect effs
, Member (Error PABError) effs
)
-- TODO: State t ~ ContractResponse Value Value Value ContractPABRequest
-- instead of 'ContractExe'
=> ContractStore ContractExe
~> Eff effs
handleContractStore = \case
Expand All @@ -105,13 +92,21 @@ handleContractStore = \case
(\ci -> ci ^. contractInstanceState <-. val_ (encode' state))
(\ci -> ci ^. contractInstanceId ==. val_ (uuidStr instanceId))

GetState instanceId ->
fmap extractState
GetState instanceId -> do
let decodeText = decode . toLazyByteString . encodeUtf8Builder
extractState = \case
Nothing -> throwError $ ContractInstanceNotFound instanceId
Just c ->
fromMaybe (throwError $ ContractStateNotFound instanceId)
(pure <$> (_contractInstanceState c >>= decodeText))

join
$ fmap extractState
$ selectOne
$ select
$ do
inst <- all_ (_contractInstances db)
guard_ ( inst ^. contractInstanceId ==. val_ (uuidStr instanceId) )
guard_ (inst ^. contractInstanceId ==. val_ (uuidStr instanceId))
pure inst

PutStopInstance instanceId ->
Expand Down
2 changes: 2 additions & 0 deletions plutus-pab/src/Plutus/PAB/Types.hs
Expand Up @@ -52,6 +52,7 @@ data PABError
| InstanceAlreadyStopped ContractInstanceId -- ^ Attempt to stop the instance failed because it was not running
| WalletNotFound Wallet
| MissingConfigFileOption
| ContractStateNotFound ContractInstanceId
deriving stock (Show, Eq, Generic)
deriving anyclass (ToJSON, FromJSON)

Expand All @@ -74,6 +75,7 @@ instance Pretty PABError where
InstanceAlreadyStopped i -> "Instance already stopped:" <+> pretty i
WalletNotFound w -> "Wallet not found:" <+> pretty w
MissingConfigFileOption -> "The --config-file option is required"
ContractStateNotFound i -> "State for contract instance not found:" <+> pretty i

data DbConfig =
DbConfig
Expand Down

0 comments on commit 5c47477

Please sign in to comment.