cosmwasm: How to distinguish errors in smart contracts?
As I’ve seen so far, developers define their errors using StdErr:generic_err("this is error message").
In testing, I need to check if a handler throws the expected error. Is pattern matching internal message string the only way?
Update after the discussion, I pulled an approach we agreed upon into two separate issues:
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 26 (26 by maintainers)
With this we could have:
And then do something like:
And to make this nicer when using, eg.
.may_load(..)?which returnsStdError:It would buy us more clarity in the unit tests (even using
handle,inithigh-level interfaces), but would all compress down to strings when running in a vm.This also goes along with respecting that unit tests and integration tests are fundamentally different things. It will make supporting other languages much easier since they only need to implement creating a compatible serialization of
Result<HandleResponse, String>instead ofResult<HandleResponse<U>, StdError>.You can easily test your approach without affecting the framework:
and then test
handle_implin unit tests.@maurolacy this will break wasm builds as the various
cosmwasm-std::entry_pointscode requiresStdErrorreturn. Best to try to update one of the contracts incosmwasm-std, so we can try it and update the othercosmwasm-stddeps in one PR. They are all quite simple contracts.queueis probably the simplest one, andhackatomis where we put most of the generic test casesA best practice test for this looks like
The create assert_matches can potentially add syntactic sugar to it. I never used it but know it is used by the NEAR team. Probably worth trying it internally at some point and sharing experience to evaluate if it is worth an additional dependency.
This is true for Rust in general. However,
StdErrorandStdResultused in the contract-VM interface do no support that.