Skip to content

Commit

Permalink
fix(agent): fix request failure errors not providing more information
Browse files Browse the repository at this point in the history
  • Loading branch information
Meshiest committed Dec 10, 2024
1 parent cf9869a commit 285b4af
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 25 deletions.
31 changes: 18 additions & 13 deletions crates/agent/src/rpc/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ impl AgentService for AgentRpcServer {
.state
.get_env_info(env_id)
.await
.map_err(|_| AgentError::FailedToMakeRequest)?
.map_err(|e| AgentError::FailedToGetEnvInfo(e.to_string()))?
.network;

let url = format!(
Expand All @@ -186,26 +186,31 @@ impl AgentService for AgentRpcServer {
self.state.cli.ports.rest
);
let response = reqwest::Client::new()
.post(url)
.post(&url)
.header("Content-Type", "application/json")
.body(tx)
.send()
.await
.map_err(|_| AgentError::FailedToMakeRequest)?;
.map_err(|e| AgentError::FailedToMakeRequest(format!("{url}: {e:?}")))?;
let status = response.status();
if status.is_success() {
Ok(())
return Ok(());
// transaction already exists so this is technically a success
} else if status.is_server_error()
&& response
.text()
.await
.ok()
}

let text = response.text().await.ok();

if status.is_server_error()
&& text
.as_ref()
.is_some_and(|text| text.contains("exists in the ledger"))
{
return Ok(());
Ok(())
} else {
Err(AgentError::FailedToMakeRequest)
Err(AgentError::FailedToMakeRequest(format!(
"{url}: {status:?} {}",
text.unwrap_or_else(|| "no error message".to_string())
)))
}
}

Expand Down Expand Up @@ -329,7 +334,7 @@ impl AgentService for AgentRpcServer {
.ok_or(AgentError::NodeClientNotSet)?
.get_block_lite(ctx, block_hash)
.await
.map_err(|_| AgentError::FailedToMakeRequest)?
.map_err(|e| AgentError::FailedToMakeRequest(format!("block info: {e:?}")))?
}

async fn find_transaction(
Expand All @@ -343,7 +348,7 @@ impl AgentService for AgentRpcServer {
.ok_or(AgentError::NodeClientNotSet)?
.find_transaction(context, tx_id)
.await
.map_err(|_| AgentError::FailedToMakeRequest)?
.map_err(|e| AgentError::FailedToMakeRequest(format!("find tx: {e:?}")))?
}

async fn get_status(self, ctx: Context) -> Result<AgentStatus, AgentError> {
Expand Down
16 changes: 9 additions & 7 deletions crates/aot/src/runner/rpc/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ impl<N: Network> NodeService for NodeRpcServer<N> {

block_db
.find_block_hash(&tx_id)
.map_err(|_| AgentError::FailedToMakeRequest)
.map_err(|e| {
AgentError::FailedToMakeRequest(format!("block hash for tx {tx_id}: {e:?}"))
})
.map(|hash| hash.map(|hash| hash.to_string()))
}

Expand All @@ -80,9 +82,9 @@ impl<N: Network> NodeService for NodeRpcServer<N> {
.parse()
.map_err(|_| AgentError::InvalidBlockHash)?;

let Some(height) = block_db
.get_block_height(&hash)
.map_err(|_| AgentError::FailedToMakeRequest)?
let Some(height) = block_db.get_block_height(&hash).map_err(|e| {
AgentError::FailedToMakeRequest(format!("block height for hash {hash}: {e:?}"))
})?
else {
return Ok(None);
};
Expand All @@ -91,9 +93,9 @@ impl<N: Network> NodeService for NodeRpcServer<N> {
return Ok(None);
};

let Some(transactions) = block_db
.get_block_transactions(&hash)
.map_err(|_| AgentError::FailedToMakeRequest)?
let Some(transactions) = block_db.get_block_transactions(&hash).map_err(|e| {
AgentError::FailedToMakeRequest(format!("transactions for height {height}: {e:?}"))
})?
else {
return Ok(None);
};
Expand Down
4 changes: 2 additions & 2 deletions crates/common/src/rpc/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ pub enum AgentError {
InvalidState,
#[error("failed to parse json")]
FailedToParseJson,
#[error("failed to make a request")]
FailedToMakeRequest,
#[error("failed to make a request: {0}")]
FailedToMakeRequest(String),
#[error("failed to get env info: {0}")]
FailedToGetEnvInfo(String),
#[error("failed to spawn a process")]
Expand Down
6 changes: 3 additions & 3 deletions crates/controlplane/src/cannon/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ impl ExecutionContext {
};

if let Err(e) = client.broadcast_tx(tx_str.clone()).await {
warn!("cannon {env_id}.{cannon_id} failed to broadcast transaction to agent {id}: {e}");
warn!("cannon {env_id}.{cannon_id} failed to broadcast transaction to agent {id}: {e:?}");
continue;
}

Expand All @@ -332,7 +332,7 @@ impl ExecutionContext {

match res {
Err(e) => {
warn!("cannon {env_id}.{cannon_id} failed to broadcast transaction to {addr}: {e}");
warn!("cannon {env_id}.{cannon_id} failed to broadcast transaction to {addr}: {e:?}");
continue;
}
Ok(req) => {
Expand All @@ -350,7 +350,7 @@ impl ExecutionContext {
return Ok(tx_id);
}

warn!("cannon {env_id}.{cannon_id} failed to broadcast transaction to {addr}: {}", status);
warn!("cannon {env_id}.{cannon_id} failed to broadcast transaction to {addr}: {status}");
continue;
}
}
Expand Down

0 comments on commit 285b4af

Please sign in to comment.