Suppose I have require(..., "error") but instead, I want the error message to be "error var" where var is a variable of type string, or uint.

What should I write?

2 Answers 2


You can use custom errors:

// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.4;

error Unauthorized();

contract VendingMachine {
    address payable owner = payable(msg.sender);

    function withdraw() public {
        if (msg.sender != owner)
            revert Unauthorized();

    // ...

In fact, I highly recommend you switch to custom errors. They are a godsend for Ethereum development. Compared to revert reason strings, custom errors are easier to work with, more gas efficient, and more elegant.


you can make your custom errors quite descriptive with vars


// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.10;

error InvalidNumber(uint32 required, uint32 passed);
contract CustomErrors{

    uint32 private constant magicNumber = 42;
    function checkMagicNumber(uint32 _number) public pure {
        if (_number != magicNumber)
            revert InvalidNumber({
                required: magicNumber,
                passed: _number

        // ...
    // ...

result calling function passing 21

[vm]from: 0x5B3...eddC4to: CustomErrors.(constructor)value: 0 weidata: 0x608...a0033logs: 0hash: 0xf34...6204d
transact to CustomErrors.checkMagicNumber pending ... 
transact to CustomErrors.checkMagicNumber errored: VM error: revert.

    The transaction has been reverted to the initial state.
Error provided by the contract:
 "required": {
  "value": "42"
 "passed": {
  "value": "21"
Debug the transaction to get more information.

Your Answer

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

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