1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/// Error type for activity operations that supports retry semantics.
///
/// This enum distinguishes between errors that should trigger a retry
/// and those that indicate permanent failure.
///
/// # Examples
///
/// ```rust
/// use runner_q::ActivityError;
/// use runner_q::ActivityHandlerResult;
///
/// // Retryable error - temporary network issue
/// let retry_error = ActivityError::Retry("Network timeout".to_string());
///
/// // Non-retryable error - invalid input data
/// let permanent_error = ActivityError::NonRetry("Invalid user ID format".to_string());
///
/// // Using in activity handlers
/// fn process_user_data(payload: serde_json::Value) -> ActivityHandlerResult {
/// let user_id = payload["user_id"].as_str()
/// .ok_or_else(|| ActivityError::NonRetry("Missing user_id".to_string()))?;
///
/// if user_id.is_empty() {
/// return Err(ActivityError::NonRetry("Empty user_id".to_string()));
/// }
///
/// // Simulate processing that might fail temporarily
/// if payload["retry_processing"].as_bool().unwrap_or(false) {
/// Err(ActivityError::Retry("Processing failed, will retry".to_string()))
/// } else {
/// Ok(Some(serde_json::json!({"processed": true})))
/// }
/// }
/// ```
/// Trait to determine if an error should be retried.
///
/// This trait allows custom error types to specify their retry behavior,
/// enabling automatic conversion to `ActivityError` with appropriate retry semantics.
///
/// # Examples
///
/// ```rust
/// use runner_q::RetryableError;
/// use runner_q::ActivityError;
///
/// // Custom error type
/// #[derive(Debug)]
/// pub struct DatabaseError {
/// message: String,
/// is_connection_error: bool,
/// }
///
/// impl RetryableError for DatabaseError {
/// fn is_retryable(&self) -> bool {
/// self.is_connection_error
/// }
/// }
///
/// impl From<DatabaseError> for ActivityError {
/// fn from(err: DatabaseError) -> Self {
/// if err.is_retryable() {
/// ActivityError::Retry(err.message)
/// } else {
/// ActivityError::NonRetry(err.message)
/// }
/// }
/// }
///
/// // Usage in activity handler
/// fn handle_database_operation() -> Result<(), DatabaseError> {
/// // ... database operation that might fail
/// Err(DatabaseError {
/// message: "Connection timeout".to_string(),
/// is_connection_error: true,
/// })
/// }
/// ```
// Implement RetryableError for common error types
/// Implementation for std::io::Error
/// Implementation for serde_json::Error