Enumerations, commonly referred to as enums, are a type that can represent one of several possible variants. Within our contract, we can employ enums to craft custom error messages, facilitating more precise error handling within functions.
Copy the custom error block into your main.sw
file:
enum InvalidError {
IncorrectAssetId: AssetId,
NotEnoughTokens: u64,
OnlyOwner: Identity,
}
Within our contract, we can anticipate various scenarios where we'd want to throw an error and halt the transaction:
For each situation, we can define specific return types for the errors:
IncorrectAssetId
error, we can return the submitted asset id, which is of type AssetId
. NotEnoughTokens
error, we can define the return type as u64
to indicate the number of coins involved. OnlyOwner
error, we can utilize the Identity
of the message sender as the return value.