3

We're having requirement to get failure message from require validation. I've searched a lot for the same and found that Remix is displaying messages, however web3 is not.

In order to get messages, we have created internal function which returns Boolean result and a string message.

function validateDoc(
    bytes32 _a,
    string _b,    
    uint256 _c
    ) internal view 
    returns(bool, string){

    if(condition 1){
        return(false, "failure message1"); 

    }else if(condition 2){
        return(false, "failure message2"); 
    }
    }else if(condition 3){
        return(false, "failure message3"); 
    }        
    }else if(condition 4){
        return(false, "failure message4"); 
    }        
}  

Further, I'm calling it from another function:

function Main(
    bytes32 _a,
    string _b,
    uint256 _c    
) public returns(bool, string) {

    validateDoc(_a, _b, _c); // TODO: return bool result and message.

    // Other code 
    // ...
    return (true, "success message");                
} 

But the issue is, how to return both result of validateDoc in Main function?

3 Answers 3

6
function Main(
    bytes32 _a,
    string _b,
    uint256 _c    
) public returns(bool _success, string _message) {

    (_success, _message) = validateDoc(_a, _b, _c); // TODO: return bool result and message.

    if(_success) {
        // Other code 
        // ...
    }
    return (_success, _message);                
} 

If i understood you correctly it should look like this.

5
  • Thanks! but even if my condition(condition 1) is wrong, it's giving me message "success message"
    – Hina
    Jul 24, 2018 at 9:22
  • Edited return to reflect data received from validateDoc.
    – Maxpeinas
    Jul 24, 2018 at 9:25
  • This seems good for returning message, but //other code getting executed, which should not
    – Hina
    Jul 24, 2018 at 9:29
  • Okay checking condition resolve that too!! if(_success){//other code}
    – Hina
    Jul 24, 2018 at 9:32
  • Yea i changed my post, a bit too late it seems lol
    – Maxpeinas
    Jul 24, 2018 at 9:33
6

Here's an example how to read the multiple returns

pragma solidity ^0.4.24;
contract Testing {


function test() view returns (uint, string)
{
    return (10,"test");



}

   string s;
   uint u;

    function f(uint a) external view{ 
   (u,s)= test();

}
}
0

You can also do this:

(uint foo, address bar) = baz();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.