#[ allow( clippy::missing_inline_in_public_items ) ]
mod private
{
use super::super::core::orphan::*;
use serde::{ Serialize, Deserialize };
use std::time::Duration;
#[ cfg( feature = "error-handling" ) ]
use chrono;
include!( "enhanced_types.rs" );
impl ErrorContext
{
#[ must_use ]
pub fn new( operation : String, request_id : String, context_data : std::collections::HashMap< String, String > ) -> Self
{
Self
{
operation,
request_id,
context_data,
timestamp : chrono::Utc::now(),
}
}
#[ must_use ]
pub fn request_id( &self ) -> &str
{
&self.request_id
}
#[ must_use ]
pub fn context( &self ) -> &str
{
&self.operation
}
}
impl EnhancedAnthropicError
{
#[ must_use ]
pub fn new( error_type : ErrorType, message : String, context : Option< ErrorContext > ) -> Self
{
let ( class, severity, is_transient ) = match error_type
{
ErrorType::Authentication => ( ErrorClass::Authentication, ErrorSeverity::High, false ),
ErrorType::InvalidRequest => ( ErrorClass::InvalidRequest, ErrorSeverity::Medium, false ),
ErrorType::ServerError => ( ErrorClass::ServerError, ErrorSeverity::High, true ),
ErrorType::RateLimit => ( ErrorClass::RateLimit, ErrorSeverity::Medium, true ),
ErrorType::Network => ( ErrorClass::Network, ErrorSeverity::High, true ),
ErrorType::Timeout => ( ErrorClass::Timeout, ErrorSeverity::Medium, true ),
ErrorType::Parsing => ( ErrorClass::Parsing, ErrorSeverity::Medium, false ),
ErrorType::Internal => ( ErrorClass::Internal, ErrorSeverity::High, false ),
};
Self
{
error_type,
message,
context,
class,
severity,
is_transient,
stack_trace : Vec::new(),
correlation_id : None,
request_id : None,
}
}
#[ must_use ]
pub fn error_class( &self ) -> ErrorClass
{
self.class.clone()
}
#[ must_use ]
pub fn severity( &self ) -> ErrorSeverity
{
self.severity
}
#[ must_use ]
pub fn is_transient( &self ) -> bool
{
self.is_transient
}
#[ must_use ]
pub fn requires_credential_refresh( &self ) -> bool
{
matches!( self.error_type, ErrorType::Authentication )
}
#[ must_use ]
pub fn error_type( &self ) -> ErrorType
{
self.error_type.clone()
}
#[ must_use ]
pub fn has_remediation_steps( &self ) -> bool
{
true
}
#[ must_use ]
pub fn is_credential_related( &self ) -> bool
{
matches!( self.error_type, ErrorType::Authentication )
}
#[ must_use ]
pub fn has_backoff_strategy( &self ) -> bool
{
matches!( self.error_type, ErrorType::RateLimit )
}
#[ must_use ]
pub fn supports_retry( &self ) -> bool
{
self.is_transient
}
#[ must_use ]
pub fn has_context( &self ) -> bool
{
self.context.is_some()
}
#[ must_use ]
pub fn context( &self ) -> Option< &ErrorContext >
{
self.context.as_ref()
}
#[ must_use ]
pub fn has_stack_trace( &self ) -> bool
{
!self.stack_trace.is_empty()
}
#[ must_use ]
pub fn stack_trace( &self ) -> &Vec< String >
{
&self.stack_trace
}
#[ must_use ]
pub fn request_id( &self ) -> &Option< String >
{
&self.request_id
}
#[ must_use ]
pub fn correlation_id( &self ) -> &Option< String >
{
&self.correlation_id
}
#[ must_use ]
pub fn message( &self ) -> &str
{
&self.message
}
#[ must_use ]
pub fn with_stack_trace( mut self, stack_trace : Vec< String > ) -> Self
{
self.stack_trace = stack_trace;
self
}
#[ must_use ]
pub fn with_request_id( mut self, request_id : Option< String > ) -> Self
{
self.request_id = request_id;
self
}
#[ must_use ]
pub fn with_correlation_id( mut self, correlation_id : Option< String > ) -> Self
{
self.correlation_id = correlation_id;
self
}
}
impl TimeoutError
{
#[ must_use ]
pub fn new( timeout_type : TimeoutType, duration : Duration, message : String ) -> Self
{
Self
{
timeout_type,
duration,
message,
}
}
}
impl NetworkError
{
#[ must_use ]
pub fn new( error_type : NetworkErrorType, message : String, details : Option< String > ) -> Self
{
Self
{
error_type,
message,
details,
}
}
}
impl CustomError
{
#[ must_use ]
pub fn new( name : String, message : String, severity : ErrorSeverity ) -> Self
{
Self
{
name,
message,
severity,
}
}
}
impl ErrorChain
{
#[ must_use ]
pub fn new( primary : CustomError ) -> Self
{
Self
{
primary,
causes : Vec::new(),
context : String::new(),
}
}
#[ must_use ]
pub fn caused_by( mut self, error : AnthropicError ) -> Self
{
self.causes.push( error );
self
}
#[ must_use ]
pub fn with_context( mut self, context : &str ) -> Self
{
self.context = context.to_string();
self
}
pub fn build( self ) -> AnthropicResult< ChainedError >
{
let chain_length = u32::try_from( self.causes.len() + 1 )
.map_err( | _ | AnthropicError::InvalidArgument( "Chain length exceeds u32 maximum".to_string() ) )?;
let root_cause = if let Some( last_cause ) = self.causes.last()
{
last_cause.to_string()
}
else
{
self.primary.message.clone()
};
let immediate_cause = self.primary.message;
let context = self.context;
Ok( ChainedError
{
chain_length,
root_cause,
immediate_cause,
context,
})
}
}
impl ChainedError
{
#[ must_use ]
pub fn chain_length( &self ) -> u32
{
self.chain_length
}
#[ must_use ]
pub fn root_cause( &self ) -> &str
{
&self.root_cause
}
#[ must_use ]
pub fn immediate_cause( &self ) -> &str
{
&self.immediate_cause
}
#[ must_use ]
pub fn has_context( &self ) -> bool
{
!self.context.is_empty()
}
#[ must_use ]
pub fn context( &self ) -> &str
{
&self.context
}
#[ must_use ]
pub fn chain_iterator( &self ) -> std::vec::IntoIter< String >
{
let chain = vec![ self.immediate_cause.clone(), self.root_cause.clone() ];
chain.into_iter()
}
}
impl RequestContext
{
#[ must_use ]
pub fn new( correlation_id : String ) -> Self
{
Self
{
correlation_id,
request_sequence : 1,
}
}
}
include!( "enhanced_impls.rs" );
}
crate::mod_interface!
{
#[ cfg( feature = "error-handling" ) ]
exposed use
{
EnhancedAnthropicError,
ErrorContext,
TimeoutError,
NetworkError,
ErrorParser,
ErrorMapper,
ErrorClassifier,
NetworkErrorClassifier,
NetworkErrorClassification,
ErrorRecovery,
BackoffCalculator,
BackoffStrategyDetails,
CredentialHintGenerator,
RequestContext,
ErrorSerializer,
ErrorLogger,
ErrorMetrics,
CorrelationTracker,
ErrorLocalizer,
RecoveryStrategy,
ActionableError,
BatchError,
TimeoutClassification,
CredentialHints,
LogEntry,
CorrelationSummary,
LocalizedError,
CustomError,
ErrorChain,
ChainedError,
};
}