My test case still shows "Unhandled exception" even if I try-catch-ed it?
App Development4 posts383 views6 likesLast activity Mar 2023
CH
charlie.ngOP
Mar 2023Hi there,
Is this a bug when I try to write the test case - testRollback? Because the compiler would still complain that it’s a “Unhandled exception”. And furthermore when I run
daml test --show-coverage
, it’ll show the test case failed. Any idea from anyone? Thanks in advance.
data RollbackErrorCode =
VALIDATION_FAILED
deriving (Eq, Ord, Show)
exception RollbackException
with
errorCode : RollbackErrorCode
errorMsg : Text
where
message "RollbackException msg: errorCode - " <> show errorCode <> " , errorMsg - " <> show errorMsg
template TempRollback
with
a: Text
owner: Party
where
signatory owner
choice Test : ()
controller owner
do
throw $ RollbackException VALIDATION_FAILED "abc"
pure ()
testRollback = do
alice <- allocateParty "Alice"
cid <- submit alice do
createCmd TempRollback with
a = "aapple"
owner = alice
try do
submit alice do
exerciseCmd cid Test
catch
RollbackException _ _ ->
pure ()
pure ()
ST
stefanobaghino-da
Mar 2023You might want to have a look here.
tl;dr try/catch is not currently supported at the top level in Daml Script and Daml Triggers, if you want to catch an error you have to do so as part of a transaction.
GY
gyorgybalazsi
Mar 2023Extending the answer given by Stefano, this is an example how you can wrap the try/catch in an adhoc contract: Testing the exact error message returned from failed transactions
CH
charlie.ng
Mar 2023Thanks both!! submitMustFail works in my case.