error_forge/
async_error_impl.rs

1#[cfg(feature = "async")]
2use async_trait::async_trait;
3use std::error::Error as StdError;
4
5use crate::async_error::AsyncForgeError;
6use crate::error::AppError;
7
8/// Provides async implementations for the AppError type.
9#[cfg(feature = "async")]
10#[async_trait]
11impl AsyncForgeError for AppError {
12    fn kind(&self) -> &'static str {
13        <Self as crate::error::ForgeError>::kind(self)
14    }
15    
16    fn caption(&self) -> &'static str {
17        <Self as crate::error::ForgeError>::caption(self)
18    }
19    
20    fn is_retryable(&self) -> bool {
21        <Self as crate::error::ForgeError>::is_retryable(self)
22    }
23    
24    fn is_fatal(&self) -> bool {
25        <Self as crate::error::ForgeError>::is_fatal(self)
26    }
27    
28    fn status_code(&self) -> u16 {
29        <Self as crate::error::ForgeError>::status_code(self)
30    }
31    
32    fn exit_code(&self) -> i32 {
33        <Self as crate::error::ForgeError>::exit_code(self)
34    }
35    
36    fn user_message(&self) -> String {
37        <Self as crate::error::ForgeError>::user_message(self)
38    }
39    
40    fn dev_message(&self) -> String {
41        <Self as crate::error::ForgeError>::dev_message(self)
42    }
43    
44    async fn async_handle(&self) -> Result<(), Box<dyn StdError + Send + Sync>> {
45        // Default async handling for AppError
46        match self {
47            AppError::Network { .. } => {
48                // In a real implementation, this might attempt reconnection or other async recovery
49                Ok(())
50            },
51            _ => {
52                // For other error types, default to regular error handling
53                Ok(())
54            }
55        }
56    }
57}
58
59#[cfg(feature = "async")]
60impl AppError {
61    /// Create a new error from an async operation result
62    pub async fn from_async_result<T, E>(result: Result<T, E>) -> Result<T, Self> 
63    where
64        E: StdError + Send + Sync + 'static
65    {
66        match result {
67            Ok(value) => Ok(value),
68            Err(e) => Err(Self::other(e.to_string())
69                .with_fatal(false)
70                .with_retryable(true))
71        }
72    }
73    
74    /// Handle this error asynchronously using the AsyncForgeError trait
75    pub async fn handle_async(&self) -> Result<(), Box<dyn StdError + Send + Sync>> {
76        <Self as AsyncForgeError>::async_handle(self).await
77    }
78    
79    /// Wrap this error with async context
80    pub fn with_async_context<C: std::fmt::Display + std::fmt::Debug + Send + Sync + 'static>(
81        self,
82        context_generator: impl FnOnce() -> C + Send + 'static
83    ) -> crate::context::ContextError<Self, C> {
84        crate::context::ContextError::new(self, context_generator())
85    }
86}