I have a getter function in my smart contract that returns one of the string values in statically defined string array. Upon entry to the function, it does a bounds check on the incoming index using require()
. If the bounds check fails, what will happen in my Node.JS app that used Web3.js to make the call? Will a Javascript error be thrown? Will I get NULL back?
If anyone has a good article or web resource that shows the proper way to handle errors from a Web3.JS call that resulted in an assert or require operation being triggered on the smart contract side, please share.
Here is my smart contract code:
pragma solidity ^0.4.23;
import "github.com/OpenZeppelin/zeppelin-solidity/contracts/ownership/Ownable.sol";
contract VideoDummyData is Ownable{
// Constructor, MUST be public.
constructor() public {
// Unused.
}
// Array of sample video IDs.
string[3] m_aryVideoIds =
["ZUSPD9zOyJs", "4nqJiBRNQuw", "PLcxE4UkJt0"];
// Return one of the sample video IDs.
function getVideoIdAt (uint ndx) public view returns(string)
{
// Bounds check on the desired index.
require(ndx < m_aryVideoIds.length);
return m_aryVideoIds[ndx];
}
}
call
to aview
function so there is no transaction to be mined.