Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

forge script --json swallows errors #5867

Closed
2 tasks done
amusingaxl opened this issue Sep 20, 2023 · 2 comments
Closed
2 tasks done

forge script --json swallows errors #5867

amusingaxl opened this issue Sep 20, 2023 · 2 comments
Labels
T-bug Type: bug

Comments

@amusingaxl
Copy link

amusingaxl commented Sep 20, 2023

Component

Forge

Have you ensured that all of these are up to date?

  • Foundry
  • Foundryup

What version of Foundry are you on?

forge 0.2.0 (ec3f9bd 2023-09-19T13:44:30.009787069Z)

What command(s) is the bug in?

forge script

Operating System

Linux

Describe the bug

Errors thrown in scripts when parsing JSON config are swallowed:

import {console2} from "forge-std/console2.sol";
import {Script} from "forge-std/Script.sol";
import {stdJson} from "forge-std/StdJson.sol";

contract MyScript is Script {
    function run() {
        string memory config = "{\"nst\":\"0x7Fc85446b851eBD2ED047978E8A46fEb9b87e67f\",\"vestTot\":2000000000000000000000000,\"vestBgn\":1688158800,\"vestTau\":31536000,\"vestEta\":0}";

        ConfigReader reader = new ConfigReader(config);

        console2.log("config:", config);
        try reader.readAddress(".ngt") returns (address) {

        } catch (bytes memory e) {
            console2.logBytes(e); // Displays the error
        }
        console2.log("ngt:", config.readAddress(".ngt")); // Error is swallowed
}

ConfigReader above is just a wrapper on top of stdJson so it can work with try...catch blocks:

contract ConfigReader {
    string internal config;

    constructor(string memory _config) {
        config = _config;
    }

    function readAddress(string memory key) external returns (address) {
        return stdJson.readAddress(config, key);
    }

    function readUint(string memory key) external returns (uint256) {
        return stdJson.readUint(config, key);
    }
}

If I run the script above:

forge script --json MyScript -vvvvv

I get:

{
  "logs": [
    "config: {\"nst\":\"0x7Fc85446b851eBD2ED047978E8A46fEb9b87e67f\",\"vestTot\":2000000000000000000000000,\"vestBgn\":1688158800,\"vestTau\":31536000,\"vestEta\":0}",
    "0x0bc445030000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002d4e6f206d61746368696e672076616c7565206f7220617272617920666f756e6420666f72206b6579202e6e677400000000000000000000000000000000000000"
  ],
  "gas_used": 885313,
  "returns": {}
}

Only the error caught is shown.

When running the same script without --json, I get a proper error message:

Error: 
No matching value or array found for key .ngt

If there is logging whatsoever (i.e.: simply try to read the missing JSON key), I simply get a pretty much empty response:

{
  "logs": [],
  "gas_used": 874524,
  "returns": {}
}

which is not very useful.

I understand that --json is supposed to output a valid JSON, but I can see 2 possible ways of doing this:

  1. Encode the error into a JSON string and add it to the payload.
  2. Use stderr to display the error in a non-JSON format.
@amusingaxl amusingaxl added the T-bug Type: bug label Sep 20, 2023
@zerosnacks
Copy link
Member

A possible fix was added in: #7071

@amusingaxl would be great if you could check if this fixes you issue

@zerosnacks
Copy link
Member

Marking as resolved by #7071

Feel free to re-open if you are still facing issues around this @amusingaxl

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-bug Type: bug
Projects
Status: Completed
Development

No branches or pull requests

2 participants