Check exact contract revert error

Hi this is my code about how to check the exact contract revert error.
I have a contract owner-only method. NotOwner error if called by non-owner

async fn only_owner_can_transfer_ownership() {
    let (token_instance, wallets) = setup_token("My Token", "MTK", 18).await;

    let wallet1_token_instance =
        get_token_instance(token_instance.get_contract_id(), &wallets.wallet1);

    let res = transfer_ownership(
        &wallet1_token_instance,
        Identity::Address(Address::from(wallets.wallet1.address())),
    )
    .await;
    assert!(res.is_err());

    let err = res.unwrap_err();
    if let errors::Error::RevertTransactionError(err_str, _receipt_vec) = &err {
        assert_eq!(err_str, &Error::NotOwner().to_string());
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:?}", self)
        // or, alternatively:
        // fmt::Debug::fmt(self, f)
    }
}

i am new to rust. is it the correct way to compare revert errors?

2 Likes