Skip to content

Commit

Permalink
refactor(law-stone): introduce specific errors for reply handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ccamel committed Apr 1, 2024
1 parent 5cfaca8 commit 0dbcd2b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
24 changes: 13 additions & 11 deletions contracts/okp4-law-stone/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{
to_json_binary, Binary, Deps, DepsMut, Env, MessageInfo, Reply, Response, StdError, StdResult,
SubMsg, WasmMsg,
to_json_binary, Binary, Deps, DepsMut, Env, MessageInfo, Reply, Response, StdResult, SubMsg,
WasmMsg,
};
use cw2::set_contract_version;
use okp4_logic_bindings::LogicCustomQuery;
Expand Down Expand Up @@ -133,7 +133,7 @@ pub mod query {
use crate::msg::ProgramResponse;
use crate::state::PROGRAM;
use cosmwasm_std::QueryRequest;
use okp4_logic_bindings::AskResponse;
use okp4_logic_bindings::{Answer, AskResponse};

pub fn program(deps: Deps<'_, LogicCustomQuery>) -> StdResult<ProgramResponse> {
let program = PROGRAM.load(deps.storage)?.into();
Expand Down Expand Up @@ -180,14 +180,15 @@ pub fn reply(
) -> Result<Response, ContractError> {
match msg.id {
STORE_PROGRAM_REPLY_ID => reply::store_program_reply(deps, env, msg),
_ => Err(StdError::generic_err("Not implemented").into()),
_ => Err(ContractError::UnknownReplyID),
}
}

pub mod reply {
use super::*;
use crate::helper::{ask_response_to_objects, get_reply_event_attribute, object_ref_to_uri};
use crate::state::{LawStone, DEPENDENCIES, PROGRAM};
use cw_utils::ParseReplyError;

pub fn store_program_reply(
deps: DepsMut<'_, LogicCustomQuery>,
Expand All @@ -198,14 +199,14 @@ pub mod reply {

msg.result
.into_result()
.map_err(|_| {
ContractError::InvalidReplyMsg(StdError::generic_err("no message in reply"))
})
.map_err(ParseReplyError::SubMsgFailure)
.map_err(Into::into)
.and_then(|e| {
get_reply_event_attribute(e.events, "id".to_string()).ok_or_else(|| {
ContractError::InvalidReplyMsg(StdError::generic_err(
"reply event doesn't contains object id",
))
get_reply_event_attribute(&e.events, "id").ok_or_else(|| {
ParseReplyError::SubMsgFailure(
"reply event doesn't contains object id".to_string(),
)
.into()
})
})
.map(|obj_id| LawStone {
Expand Down Expand Up @@ -275,6 +276,7 @@ mod tests {
};
use okp4_objectarium::msg::PageInfo;
use okp4_wasm::uri::CosmwasmUri;
use schemars::_serde_json::json;
use std::collections::VecDeque;

fn custom_logic_handler_with_dependencies(
Expand Down
6 changes: 3 additions & 3 deletions contracts/okp4-law-stone/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ pub enum ContractError {
Std(#[from] StdError),

#[error("{0}")]
Parse(#[from] ParseReplyError),
ParseReplyError(#[from] ParseReplyError),

#[error("Invalid reply message: {0}")]
InvalidReplyMsg(StdError),
#[error("An unknown reply ID was received.")]
UnknownReplyID,

#[error("Cannot parse cosmwasm uri: {0}")]
ParseCosmwasmUri(CosmwasmUriError),
Expand Down

0 comments on commit 0dbcd2b

Please sign in to comment.