Test error message

My contract has a few require statements

I want to test the error message is right:

let fixture = setup().await;
    let _instance = fixture.vault_instance;

    _instance.methods().initialize().call().await.unwrap();
    let response = _instance.methods().initialize().call().await;
    assert!(response.is_err());
    let err = response.unwrap_err();
    println!("ERROR: {}", err.to_string());

returns

ERROR: Revert transaction error: AlreadyInitalized,
 receipts: [Call { id: 0000000000000000000000000000000000000000000000000000000000000000, to: 39e910f6527aa9374ae4be63b4b1886cefa2023aac6346ab26fcc4c1746b04a3, amount: 0, asset_id: 0000000000000000000000000000000000000000000000000000000000000000, gas: 1000000, param1: 3480246403, param2: 1, pc: 11624, is: 11624 }, LogData { id: 39e910f6527aa9374ae4be63b4b1886cefa2023aac6346ab26fcc4c1746b04a3, ra: 0, rb: 17, ptr: 27088, len: 8, digest: af5570f5a1810b7af78caf4bc70a660f0df51e42baf91d4de5b2328de0e83dfc, data: [0, 0, 0, 0, 0, 0, 0, 0], pc: 20792, is: 11624 }, Revert { id: 39e910f6527aa9374ae4be63b4b1886cefa2023aac6346ab26fcc4c1746b04a3, ra: 18446744073709486080, pc: 20800, is: 11624 }, ScriptResult { result: Revert, gas_used: 1494 }]

How do I just get err to be AlreadyInitalized (which is the name of the error my contract throws

1 Like

You can use a match expression here to match the type of error and get access to the inner string.

let err = response.unwrap_err();
let err_msg = match err {
    Error::RevertTransactionError(string, _receipts) => string,
    _ => String::from("other")
};
1 Like

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.