Skip to content

Commit

Permalink
fix(nervous-system-agent): propagate candid encode errors (#3448)
Browse files Browse the repository at this point in the history
Previously we were panicking, even though there was already an error
case we could be using to propagate these errors.

Pointed out by @aterga
  • Loading branch information
anchpop authored Jan 14, 2025
1 parent fe42b70 commit 02cba76
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion rs/nervous_system/agent/src/agent_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl CallCanisters for Agent {
request: R,
) -> Result<R::Response, Self::Error> {
let canister_id = canister_id.into();
let request_bytes = request.payload();
let request_bytes = request.payload().map_err(AgentCallError::CandidEncode)?;
let response = if request.update() {
let request = self
.update(&canister_id, request.method())
Expand Down
6 changes: 3 additions & 3 deletions rs/nervous_system/agent/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ mod sealed {
pub trait Request: Send {
fn method(&self) -> &'static str;
fn update(&self) -> bool;
fn payload(&self) -> Vec<u8>;
fn payload(&self) -> Result<Vec<u8>, candid::Error>;
type Response: CandidType + DeserializeOwned;
}

Expand All @@ -33,8 +33,8 @@ impl<R: ic_nervous_system_clients::Request> Request for R {
fn update(&self) -> bool {
Self::UPDATE
}
fn payload(&self) -> Vec<u8> {
candid::encode_one(self).unwrap()
fn payload(&self) -> Result<Vec<u8>, candid::Error> {
candid::encode_one(self)
}

type Response = <Self as ic_nervous_system_clients::Request>::Response;
Expand Down
4 changes: 2 additions & 2 deletions rs/nervous_system/agent/src/null_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ impl<T: CandidType + DeserializeOwned + Send> Request for NullRequest<T> {
fn update(&self) -> bool {
self.update
}
fn payload(&self) -> Vec<u8> {
Encode!().unwrap()
fn payload(&self) -> Result<Vec<u8>, candid::Error> {
Encode!()
}

type Response = T;
Expand Down
2 changes: 1 addition & 1 deletion rs/nervous_system/agent/src/pocketic_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl CallCanisters for PocketIc {
request: R,
) -> Result<R::Response, Self::Error> {
let canister_id = canister_id.into();
let request_bytes = request.payload();
let request_bytes = request.payload().map_err(PocketIcCallError::CandidEncode)?;
let response = if request.update() {
self.update_call(
canister_id,
Expand Down

0 comments on commit 02cba76

Please sign in to comment.