From 7cf2006b17191370507074dd211d50edf27e6a31 Mon Sep 17 00:00:00 2001 From: Adam Spofford Date: Thu, 20 Mar 2025 10:18:43 -0700 Subject: [PATCH 01/25] wip --- ic-agent/src/agent/agent_error.rs | 155 +++--- ic-agent/src/agent/mod.rs | 471 +++++++++++------- ic-agent/src/agent/response_authentication.rs | 158 ++++-- 3 files changed, 483 insertions(+), 301 deletions(-) diff --git a/ic-agent/src/agent/agent_error.rs b/ic-agent/src/agent/agent_error.rs index 006b0f23..32495d54 100644 --- a/ic-agent/src/agent/agent_error.rs +++ b/ic-agent/src/agent/agent_error.rs @@ -1,56 +1,75 @@ //! Errors that can occur when using the replica agent. -use crate::{agent::status::Status, RequestIdError}; -use candid::Principal; use ic_certification::Label; -use ic_transport_types::{InvalidRejectCodeError, RejectResponse}; -use leb128::read; -use std::time::Duration; +use ic_transport_types::RejectResponse; use std::{ + error::Error, fmt::{Debug, Display, Formatter}, - str::Utf8Error, + time::Duration, }; use thiserror::Error; +use super::{status::Status, Operation, OperationInfo, CURRENT_OPERATION}; + +#[derive(Debug)] +pub struct AgentError { + source: Option>, + kind: ErrorKind, + operation_info: Option, +} + +#[derive(Copy, Clone, Eq, PartialEq, Debug)] +pub enum ErrorKind { + Trust, + Protocol, + Reject, + Transport, + External, + Limit, + Input, + Unknown, +} + +impl AgentError { + pub fn kind(&self) -> ErrorKind { + self.kind + } + pub fn operation_info(&self) -> Option<&OperationInfo> { + self.operation_info.as_ref() + } + pub fn new_tool_error_in_context(message: String) -> Self { + todo!() + } + pub(crate) fn from_boxed_in_context( + inner: Box, + kind: ErrorKind, + ) -> Self { + match inner.downcast::() { + Ok(agent_err) => *agent_err, + Err(source) => AgentError { + kind, + operation_info: CURRENT_OPERATION.try_with(|op| (*op.borrow()).clone()).ok(), + source: Some(source), + }, + } + } +} + /// An error that occurred when using the agent. #[derive(Error, Debug)] -pub enum AgentError { +pub(crate) enum ErrorCode { /// The replica URL was invalid. #[error(r#"Invalid Replica URL: "{0}""#)] InvalidReplicaUrl(String), /// The request timed out. #[error("The request timed out.")] - TimeoutWaitingForResponse(), + TimeoutWaitingForResponse, /// An error occurred when signing with the identity. #[error("Identity had a signing error: {0}")] SigningError(String), - /// The data fetched was invalid CBOR. - #[error("Invalid CBOR data, could not deserialize: {0}")] - InvalidCborData(#[from] serde_cbor::Error), - - /// 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), - - /// 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), - - /// The principal string was not a valid principal. - #[error("Cannot parse Principal: {0}")] - PrincipalError(#[from] crate::export::PrincipalError), - /// The subnet rejected the message. #[error("The replica returned a rejection error: reject code {:?}, reject message {}, error code {:?}", .reject.reject_code, .reject.reject_message, .reject.error_code)] CertifiedReject { @@ -85,14 +104,6 @@ pub enum AgentError { #[error("A tool returned a string message error: {0}")] MessageError(String), - /// There was an error reading a LEB128 value. - #[error("Error reading LEB128 value: {0}")] - Leb128ReadError(#[from] read::Error), - - /// A string was invalid UTF-8. - #[error("Error in UTF-8 string: {0}")] - Utf8ReadError(#[from] Utf8Error), - /// The lookup path was absent in the certificate. #[error("The lookup path ({0:?}) is absent in the certificate.")] LookupPathAbsent(Vec