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

fix: Use StdError constructor functions #82

Merged
merged 1 commit into from
Jul 19, 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
30 changes: 8 additions & 22 deletions packages/osmosis-std-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub fn derive_cosmwasm_ext(input: TokenStream) -> TokenStream {
type_url: Self::TYPE_URL.to_string(),
value: self.to_proto_bytes(),
}
}
}
}

#query_request_conversion
Expand All @@ -102,20 +102,16 @@ pub fn derive_cosmwasm_ext(input: TokenStream) -> TokenStream {

fn try_from(binary: cosmwasm_std::Binary) -> Result<Self, Self::Error> {
use ::prost::Message;
#[cfg(feature = "backtraces")]
use std::backtrace::Backtrace;
Self::decode(&binary[..]).map_err(|e| {
cosmwasm_std::StdError::ParseErr {
target_type: stringify!(#ident).to_string(),
msg: format!(
cosmwasm_std::StdError::parse_err(
stringify!(#ident),
format!(
"Unable to decode binary: \n - base64: {}\n - bytes array: {:?}\n\n{:?}",
binary,
binary.to_vec(),
e
),
#[cfg(feature = "backtraces")]
backtrace: Backtrace::capture(),
}
)
)
})
}
}
Expand All @@ -124,21 +120,11 @@ pub fn derive_cosmwasm_ext(input: TokenStream) -> TokenStream {
type Error = cosmwasm_std::StdError;

fn try_from(result: cosmwasm_std::SubMsgResult) -> Result<Self, Self::Error> {
#[cfg(feature = "backtraces")]
use std::backtrace::Backtrace;
result
.into_result()
.map_err(|e| cosmwasm_std::StdError::GenericErr {
msg: e,
#[cfg(feature = "backtraces")]
backtrace: Backtrace::capture(),
})?
.map_err(|e| cosmwasm_std::StdError::generic_err(e))?
.data
.ok_or_else(|| cosmwasm_std::StdError::NotFound {
kind: "cosmwasm_std::SubMsgResult::<T>".to_string(),
#[cfg(feature = "backtraces")]
backtrace: Backtrace::capture(),
})?
.ok_or_else(|| cosmwasm_std::StdError::not_found("cosmwasm_std::SubMsgResult::<T>"))?
.try_into()
}
}
Expand Down
24 changes: 10 additions & 14 deletions packages/osmosis-std/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,12 @@ fn query_pool(
) -> StdResult<osmosis_std::types::osmosis::gamm::v1beta1::Pool> {
let res = GammQuerier::new(&deps.querier).pool(pool_id)?;
res.pool
.ok_or_else(|| StdError::NotFound {
kind: "pool".to_string(),
})?
.ok_or_else(|| StdError::not_found("pool"))?
.try_into() // convert `Any` to `osmosis_std::types::osmosis::gamm::v1beta1::Pool`
.map_err(|e: DecodeError| StdError::ParseErr {
target_type: "osmosis_std::types::osmosis::gamm::v1beta1::Pool".to_string(),
msg: e.to_string(),
})
.map_err(|e: DecodeError| StdError::parse_err(
"osmosis_std::types::osmosis::gamm::v1beta1::Pool",
e
))
}
```

Expand All @@ -119,10 +117,10 @@ impl TryFrom<osmosis_std::shim::Any> for Pool {
return Ok(Pool::StableSwap(pool));
}

Err(StdError::ParseErr {
target_type: "Pool".to_string(),
msg: "Unmatched pool: must be either `Balancer` or `StableSwap`.".to_string(),
})
Err(StdError::parse_err(
"Pool",
"Unmatched pool: must be either `Balancer` or `StableSwap`."
))
}
}

Expand All @@ -132,9 +130,7 @@ fn query_pool(
) -> StdResult<Pool> {
let res = GammQuerier::new(&deps.querier).pool(pool_id)?;
res.pool
.ok_or_else(|| StdError::NotFound {
kind: "pool".to_string(),
})?
.ok_or_else(|| StdError::not_found("pool"))?
.try_into() // convert `Any` to `Pool`
}
```
Expand Down