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

Feature/Downcast CwOrchError #159

Merged
merged 3 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion contracts/counter/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use cosmwasm_std::StdError;
use thiserror::Error;

#[derive(Error, Debug)]
#[derive(Error, PartialEq, Debug)]
pub enum ContractError {
#[error("{0}")]
Std(#[from] StdError),
Expand Down
15 changes: 13 additions & 2 deletions contracts/counter/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use counter_contract::{
contract::CONTRACT_NAME,
msg::{GetCountResponse, InstantiateMsg, QueryMsg},
CounterContract,
ContractError, CounterContract,
};
// Use prelude to get all the necessary imports
use cw_orch::prelude::*;
Expand Down Expand Up @@ -49,6 +49,8 @@ fn setup<Chain: CwEnv>(chain: Chain) -> CounterContract<Chain> {
fn count() {
// Create a sender
let sender = Addr::unchecked(ADMIN);
// Create a user
let user = Addr::unchecked(USER);
// Create the mock
let mock = Mock::new(&sender);

Expand All @@ -58,7 +60,7 @@ fn count() {
// Increment the count of the contract
contract
// Set the caller to user
.call_as(&Addr::unchecked(USER))
.call_as(&user)
// Call the increment function (auto-generated function provided by CounterExecuteMsgFns)
.increment()
.unwrap();
Expand All @@ -84,6 +86,15 @@ fn count() {
let count = contract.get_count().unwrap();
assert_eq!(count.count, 0);
// ANCHOR_END: reset

// Check negative case
let exec_res = contract.call_as(&user).reset(0);

let expected_err = ContractError::Unauthorized {};
assert_eq!(
exec_res.unwrap_err().downcast::<ContractError>().unwrap(),
expected_err
);
}
// ANCHOR_END: count_test
// ANCHOR_END: integration_test
Expand Down
10 changes: 10 additions & 0 deletions cw-orch/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,14 @@ impl CwOrchError {
_ => panic!("Unexpected error type"),
}
}

pub fn downcast<E>(self) -> anyhow::Result<E>
where
E: std::fmt::Display + std::fmt::Debug + Send + Sync + 'static,
{
match self {
CwOrchError::AnyError(e) => e.downcast(),
_ => panic!("Unexpected error type"),
}
}
}