I want to deliberately introduce errors (imitating errors at await boundaries) into my Motoko code to test how it will work with unreliable actors.
In C/C++ I would use conditional compilation with #ifdef. But how to do it in Motoko? BTW, my project is a library that I test using a testing canister whose code is distributed with the library.
Maybe, my particular task has a simpler solution? Can I deliberately introduce errors at await boundaries to happen often?
Motoko supports debug blocks, which are only included when --debug is passed as moc option and excluded when --release is passed.
Dfx appears to compile with --debug by default (not sure why tbh), but you can override this by using an “args” : “–release” or “args” : “–debug” field in the dfx.json (to explicitly disable debug blocks).
The debug expression debug <block-or-exp> has type () provided the expression <block-or-exp> has type ().
When the program is compiled or interpreted with (default) flag --debug, evaluating the expression debug <exp> proceeds by evaluating <block-or-exp>, returning the result of <block-or-exp>.
When the program is compiled or interpreted with flag --release, evaluating the expression debug <exp> immediately returns the unit value (). The code for <block-or-exp> is never executed, nor is its code included in the compiled binary.
(PS. I think it’s a mistake that dfx specifies --debug by default).
I would write a test in Motoko that can be run with mops (i.e. in the moc interpreter). In the test file I would mock the actor that you are simulating to be unreliable and make it return the errors that you want. But you can only simulate errors of type canister_reject because you cannot synthesize the others. But there’s a chance that’s good enough.