Raise & return error with message if no key found in mapping

by Alejandro Veintimilla   Last Updated July 31, 2020 22:28 PM - source

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;
    }


Related Questions





assign decimal to a variable in solidity

Updated March 04, 2018 02:28 AM