0

I am writing a test that should fail because it looks for a key that doesn't exist on a mapping. How can I raise the error with and error message in solidity? this is the test:

  it('Should NOT update a non-existing user', async() => {
      const magicSmartContract = await MagicSmartContract.deployed();
      try {
         await magicSmartContract.update_card_data('667', '', '', '');
      } catch(e) {
         assert(e.message.includes('This card does not exist'));
         return;  // stop execution
      }
      assert(false); // Should have catched an error
   });

But I am not sure how to catch & raise an error with a message in the smart contract, this is the relevant part:

function update_card_data(string memory _card_id,
                        string memory _new_title,
                        string memory _new_text,
                        string memory _new_img_url) public {
    IdeaCard storage updatedCard = IdCardMap[_card_id];  // Here
    if(bytes(_new_title).length>0){
        updatedCard.title = _new_title;
    }
    if(bytes(_new_text).length>0){
        updatedCard.text = _new_text;
    }
    if(bytes(_new_img_url).length>0){
        updatedCard.img_url = _new_img_url;
    }
1
  • 1
    Define "doesn't exist". All possible keys are mapped (to zero values) by default, so you cannot expect your call to throw an exception, other than when your web3 provider (or node) does not respond. Hence, as I have initially mentioned, define to yourself the criteria for which you consider the key to "does not exist" (for example, all returned values are zero), and then act accordingly. – goodvibration Aug 1 '20 at 10:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.