Enumerations, or enums, are a type that can be one of several variations. In our contract, we can use an enum to create custom errors to handle errors in a function.
enum InvalidError {
IncorrectAssetId: ContractId,
NotEnoughTokens: u64,
OnlyOwner: Identity,
}
In our contract, we can expect there to be some different situations where we want to throw an error and prevent the transaction from executing:
We can define the return types for each error. For the IncorrectAssetId
error we can return the asset id sent, which is a ContractId
type. For the NotEnoughTokens
variation, we can return the number of coins by defining the return type as a u64
. For the OnlyOwner
Error, we can use the Identity of the message sender.
Was this page helpful?