Errors Catching

If you want to abort your Smart Contract execution, because of error or some condition, inside module or inside script, you can use two constructs: abort and assert.

In case of error VM will generate error and add error code to the transaction output events.

For example, let's start with assert:

script {
fun error(a: u128, b: u128) {
assert(a > b, 101); // Throw error with code 101 if a is less than b.

And same could be done with abort:

script {
fun error_1(a: u128, b: u128) {
if (a < b) {
abort 101 // Throw error with code 101 if a is less than b.

You can use abort and assert in your modules also.

Let's compile one of examples using dove and execute using CLI (easiest way to see returned events):

polkadot-js-api tx.mvm.execute @<script> <file> --seed <seed> --ws <ws-endpoint>

Replace values with your own, run execute transaction and see output, generated event:

"event": {
"method": "Event",
"section": "mvm",
"index": "0x0800",
"data": [

Where value 0x02016500000000000000 is indeed our error included in BCS, see Events tutorial for details.

Important: The described way is not the best, but only way to get custom errors from Move VM in Move pallet right now, we are working hard to make it better and more useful.