Skip to content

Commit

Permalink
UP-18: daml-script upgrade error piping (#20255)
Browse files Browse the repository at this point in the history
* UP-18: daml-script upgrade error piping
  • Loading branch information
carlpulley-da authored Nov 12, 2024
1 parent eaa3633 commit 92d0d43
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import DA.Exception
data DevErrorType
= ChoiceGuardFailed
| WronglyTypedContractSoft
| Upgrade
| UnknownNewFeature -- ^ This should never happen - Update Scripts when you see this!
deriving Show

Expand Down Expand Up @@ -127,6 +126,9 @@ data SubmitError
LocalVerdictLockedKeys with
localVerdictLockedKeys : [AnyContractKey]
-- ^ Locked contract keys
| -- | Upgrade exception
UpgradeError with -- We just give the error's prettied message. Scripts doesn't need anything more right now.
errorMessage : Text
| -- | Development feature exceptions
DevError with -- We just give the error's simple class name and the prettied message it generates. Scripts doesn't need anything more right now.
devErrorType : DevErrorType
Expand Down Expand Up @@ -177,6 +179,7 @@ instance Show SubmitError where
ValueNesting limit -> "Exceeded maximum nesting depth for values - limit: " <> show limit
LocalVerdictLockedContracts cids -> "Attempted to use locked contracts: " <> show cids
LocalVerdictLockedKeys _ -> "Attempted to use locked contract keys"
UpgradeError msg -> "UpgradeError: \"" <> msg <> "\""
DevError ty msg -> "DevError of type " <> show ty <> " and message \"" <> msg <> "\""
UnknownError msg -> "Unknown error: " <> msg
TruncatedError ty msg -> "TruncatedError of type " <> ty <> " and message \"" <> msg <> "\""
Original file line number Diff line number Diff line change
Expand Up @@ -404,20 +404,11 @@ class SubmitErrors(majorLanguageVersion: LanguageMajorVersion) {
}

sealed case class UpgradeError(message: String) extends SubmitError {
// TODO https://github.com/digital-asset/daml/issues/18616: use a non-Dev based error code
override def toDamlSubmitError(env: Env): SValue = {
val upgradeErrorTypeIdentifier =
env.scriptIds.damlScriptModule(
"Daml.Script.Internal.Questions.Submit.Error",
"DevErrorType",
)
val upgradeErrorType = SEnum(upgradeErrorTypeIdentifier, Name.assertFromString("Upgrade"), 2)

SubmitErrorConverters(env).damlScriptError(
"DevError",
"UpgradeError",
20,
("devErrorType", upgradeErrorType),
("devErrorMessage", SText(message)),
("errorMessage", SText(message)),
)
}
}
Expand All @@ -439,7 +430,7 @@ class SubmitErrors(majorLanguageVersion: LanguageMajorVersion) {
}
SubmitErrorConverters(env).damlScriptError(
"DevError",
20,
21,
("devErrorType", devErrorType),
("devErrorMessage", SText(message)),
)
Expand All @@ -450,7 +441,7 @@ class SubmitErrors(majorLanguageVersion: LanguageMajorVersion) {
override def toDamlSubmitError(env: Env): SValue =
SubmitErrorConverters(env).damlScriptError(
"UnknownError",
21,
22,
("unknownErrorMessage", SText(message)),
)
}
Expand All @@ -459,7 +450,7 @@ class SubmitErrors(majorLanguageVersion: LanguageMajorVersion) {
override def toDamlSubmitError(env: Env): SValue =
SubmitErrorConverters(env).damlScriptError(
"TruncatedError",
22,
23,
("truncatedErrorType", SText(errType)),
("truncatedErrorMessage", SText(message)),
)
Expand Down
2 changes: 1 addition & 1 deletion sdk/daml-script/test/daml/upgrades/ContractKeys.daml
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ exerciseUpdateKeyUpgraded = test $ do
expectMetadataChangedError : Either SubmitError a -> Script ()
expectMetadataChangedError r = case r of
Right _ -> assertFail "Expected failure but got success"
Left (DevError Upgrade msg)
Left (UpgradeError msg)
| "Verify that neither the signatories, nor the observers, nor the contract key, nor the key's maintainers have changed" `isInfixOf` msg
-> pure ()
Left e -> assertFail $ "Expected Upgrade error but got " <> show e
2 changes: 1 addition & 1 deletion sdk/daml-script/test/daml/upgrades/Fetch.daml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ fetchDowngradedSome = test $ do
eV1Name <- a `tryExerciseV1Util` V1.DoFetch with cid = v1Cid

case eV1Name of
Left (DevError Upgrade msg)
Left (UpgradeError msg)
| "An optional contract field with a value of Some may not be dropped during downgrading" `isInfixOf` msg
-> pure ()
res -> assertFail $ "Expected DevError Upgrade, got " <> show res
Expand Down
2 changes: 1 addition & 1 deletion sdk/daml-script/test/daml/upgrades/FromInterface.daml
Original file line number Diff line number Diff line change
Expand Up @@ -147,5 +147,5 @@ fromInterfaceInvalidDowngrade = test $ do
res <- a `trySubmit` createAndExerciseCmd (TestHelper a) InvalidDowngrade
case res of
Right _ -> fail "Expected failure but got success"
Left (DevError Upgrade msg) | "field with a value of Some may not be dropped" `isInfixOf` msg -> pure ()
Left (UpgradeError msg) | "field with a value of Some may not be dropped" `isInfixOf` msg -> pure ()
Left e -> fail $ "Expected Upgrade error but got " <> show e
6 changes: 3 additions & 3 deletions sdk/daml-script/test/daml/upgrades/InterfaceViews.daml
Original file line number Diff line number Diff line change
Expand Up @@ -164,23 +164,23 @@ exerciseCommandShouldFail = test $ do
(alice, icid) <- setupAliceAndInterface
res <- alice `trySubmit` exerciseCmd icid GetVersion
case res of
Left (DevError Upgrade msg) | "View mismatch" `isInfixOf` msg -> pure ()
Left (UpgradeError msg) | "View mismatch" `isInfixOf` msg -> pure ()
_ -> assertFail ("Expected fetchFromInterface to fail, got " <> show res)

exerciseInChoiceBodyShouldFail : Test
exerciseInChoiceBodyShouldFail = test $ do
(alice, icid) <- setupAliceAndInterface
res <- alice `trySubmit` createAndExerciseCmd (Caller with party = alice) (CallInterface with icid = icid)
case res of
Left (DevError Upgrade msg) | "View mismatch" `isInfixOf` msg -> pure ()
Left (UpgradeError msg) | "View mismatch" `isInfixOf` msg -> pure ()
_ -> assertFail ("Expected fetchFromInterface to fail, got " <> show res)

fetchFromInterfaceShouldFail : Test
fetchFromInterfaceShouldFail = test $ do
(alice, icid) <- setupAliceAndInterface
res <- alice `trySubmit` createAndExerciseExactCmd (V2.FITemplate with party = alice) (V2.FetchFromInterface with icid = icid)
case res of
Left (DevError Upgrade msg) | "View mismatch" `isInfixOf` msg -> pure ()
Left (UpgradeError msg) | "View mismatch" `isInfixOf` msg -> pure ()
_ -> assertFail ("Expected fetchFromInterface to fail, got " <> show res)

queryInterfaceContractIdInView : Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ signatoryObserverUpgrade shouldSucceed sigF obsF exerciseChoice = test $ do
res <- trySubmitMulti [alice, bob, charlie] [] $ exerciseChoice cid
case (res, shouldSucceed) of
(Right _, True) -> pure ()
(Left (DevError Upgrade msg), False)
(Left (UpgradeError msg), False)
| "Verify that neither the signatories, nor the observers, nor the contract key, nor the key's maintainers have changed" `isInfixOf` msg
-> pure ()
_ -> assertFail $ "Expected " <> (if shouldSucceed then "success" else "Upgrade error") <> " but got " <> show res
Expand Down
2 changes: 1 addition & 1 deletion sdk/daml-script/test/daml/upgrades/VariantChanges.daml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ templateInvalidChange shouldSucceed makeV1Contract v2Choice = test $ do
case (res, shouldSucceed) of
(Right "V1", True) -> pure ()
(Right "V2", True) -> pure ()
(Left (DevError Upgrade _), False) -> pure ()
(Left (UpgradeError _), False) -> pure ()
(Left (WronglyTypedContract {}), False) -> pure ()
(Left (UnknownError msg), False) | "An error occurred." `isInfixOf` msg -> pure ()
-- IDE Ledger doesn't apply obfuscation, instead the lookup error is wrapped in SCrash
Expand Down

0 comments on commit 92d0d43

Please sign in to comment.