Skip to content

Commit

Permalink
feat: make error clonable
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanosdev committed May 31, 2024
1 parent 608a3f4 commit 7b7d36a
Show file tree
Hide file tree
Showing 10 changed files with 196 additions and 79 deletions.
163 changes: 139 additions & 24 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ ic-utils = { path = "ic-utils", version = "0.35.0" }
ic-transport-types = { path = "ic-transport-types", version = "0.35.0" }

ic-certification = "2.2"
candid = "0.10.1"
candid_parser = "0.1.1"
candid = { git = "https://github.com/nathanosdev/candid" }
candid_parser = { git = "https://github.com/nathanosdev/candid" }
clap = "4.4.3"
futures-util = "0.3.21"
hex = "0.4.3"
Expand Down
38 changes: 29 additions & 9 deletions ic-agent/src/agent/agent_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::{
use thiserror::Error;

/// An error that occurred when using the agent.
#[derive(Error, Debug)]
#[derive(Error, Debug, Clone)]
pub enum AgentError {
/// The replica URL was invalid.
#[error(r#"Invalid Replica URL: "{0}""#)]
Expand All @@ -28,23 +28,23 @@ pub enum AgentError {

/// The data fetched was invalid CBOR.
#[error("Invalid CBOR data, could not deserialize: {0}")]
InvalidCborData(#[from] serde_cbor::Error),
InvalidCborData(String),

/// There was an error calculating a request ID.
#[error("Cannot calculate a RequestID: {0}")]
CannotCalculateRequestId(#[from] RequestIdError),

/// There was an error when de/serializing with Candid.
#[error("Candid returned an error: {0}")]
CandidError(Box<dyn Send + Sync + std::error::Error>),
CandidError(#[from] candid::Error),

/// There was an error parsing a URL.
#[error(r#"Cannot parse url: "{0}""#)]
UrlParseError(#[from] url::ParseError),

/// The HTTP method was invalid.
#[error(r#"Invalid method: "{0}""#)]
InvalidMethodError(#[from] http::method::InvalidMethod),
InvalidMethodError(String),

/// The principal string was not a valid principal.
#[error("Cannot parse Principal: {0}")]
Expand Down Expand Up @@ -76,7 +76,7 @@ pub enum AgentError {

/// There was an error reading a LEB128 value.
#[error("Error reading LEB128 value: {0}")]
Leb128ReadError(#[from] read::Error),
Leb128ReadError(String),

/// A string was invalid UTF-8.
#[error("Error in UTF-8 string: {0}")]
Expand Down Expand Up @@ -185,7 +185,7 @@ pub enum AgentError {

/// An unknown error occurred during communication with the replica.
#[error("An error happened during communication with the replica: {0}")]
TransportError(Box<dyn std::error::Error + Send + Sync>),
TransportError(String),

/// There was a mismatch between the expected and actual CBOR data during inspection.
#[error("There is a mismatch between the CBOR encoded call and the arguments: field {field}, value in argument is {value_arg}, value in CBOR is {value_cbor}")]
Expand Down Expand Up @@ -215,12 +215,32 @@ impl PartialEq for AgentError {
}
}

impl From<candid::Error> for AgentError {
fn from(e: candid::Error) -> AgentError {
AgentError::CandidError(e.into())
impl From<serde_cbor::Error> for AgentError {
fn from(e: serde_cbor::Error) -> AgentError {
AgentError::InvalidCborData(e.to_string())
}
}

impl From<http::method::InvalidMethod> for AgentError {
fn from(e: http::method::InvalidMethod) -> AgentError {
AgentError::InvalidMethodError(e.to_string())
}
}

impl From<read::Error> for AgentError {
fn from(e: read::Error) -> AgentError {
AgentError::Leb128ReadError(e.to_string())
}
}

#[cfg(feature = "reqwest")]
impl From<reqwest::Error> for AgentError {
fn from(e: reqwest::Error) -> AgentError {
AgentError::TransportError(e.to_string())
}
}

#[derive(Clone)]
/// A HTTP error from the replica.
pub struct HttpErrorPayload {
/// The HTTP status code.
Expand Down
Loading

0 comments on commit 7b7d36a

Please sign in to comment.