celers-core 0.2.0

Core traits and types for CeleRS distributed task queue
Documentation
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
//! Exception Handling for Task Execution
//!
//! This module provides comprehensive exception handling capabilities for task execution,
//! including exception classification, custom handlers, traceback preservation, and
//! cross-language exception serialization.
//!
//! # Example
//!
//! ```rust
//! use celers_core::exception::{
//!     TaskException, ExceptionCategory, ExceptionPolicy, ExceptionAction,
//! };
//!
//! // Create an exception with traceback
//! let exception = TaskException::new("ValueError", "Invalid input: negative number")
//!     .with_traceback(vec![
//!         ("process_data".to_string(), "tasks.py".to_string(), 42),
//!         ("validate_input".to_string(), "validators.py".to_string(), 15),
//!     ])
//!     .with_category(ExceptionCategory::Retryable);
//!
//! // Create a policy for handling exceptions
//! let policy = ExceptionPolicy::new()
//!     .ignore_on(&["NotFoundError"])
//!     .retry_on(&["TimeoutError", "ConnectionError"])
//!     .fail_on(&["ValidationError"]);
//!
//! // Determine action based on exception
//! let action = policy.get_action(&exception);
//! ```

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt;

/// Category of an exception that determines retry behavior
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum ExceptionCategory {
    /// Exception is retryable (transient errors like network issues)
    Retryable,
    /// Exception is fatal and should not be retried
    Fatal,
    /// Exception result should be ignored (task considered successful)
    Ignorable,
    /// Exception requires manual intervention
    RequiresIntervention,
    /// Unknown category - use default policy
    #[default]
    Unknown,
}

impl fmt::Display for ExceptionCategory {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Retryable => write!(f, "RETRYABLE"),
            Self::Fatal => write!(f, "FATAL"),
            Self::Ignorable => write!(f, "IGNORABLE"),
            Self::RequiresIntervention => write!(f, "REQUIRES_INTERVENTION"),
            Self::Unknown => write!(f, "UNKNOWN"),
        }
    }
}

/// Action to take when an exception occurs
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub enum ExceptionAction {
    /// Retry the task
    Retry,
    /// Fail the task and move to DLQ
    Fail,
    /// Ignore the exception and mark task as successful
    Ignore,
    /// Reject the task (don't retry, don't move to DLQ)
    Reject,
    /// Defer to default retry policy
    #[default]
    Default,
}

impl fmt::Display for ExceptionAction {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Retry => write!(f, "RETRY"),
            Self::Fail => write!(f, "FAIL"),
            Self::Ignore => write!(f, "IGNORE"),
            Self::Reject => write!(f, "REJECT"),
            Self::Default => write!(f, "DEFAULT"),
        }
    }
}

/// A single frame in an exception traceback
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct TracebackFrame {
    /// Function or method name
    pub function: String,
    /// File path or module name
    pub file: String,
    /// Line number
    pub line: u32,
    /// Optional column number
    pub column: Option<u32>,
    /// Optional local variables (for debugging)
    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
    pub locals: HashMap<String, String>,
}

impl TracebackFrame {
    /// Create a new traceback frame
    pub fn new(function: impl Into<String>, file: impl Into<String>, line: u32) -> Self {
        Self {
            function: function.into(),
            file: file.into(),
            line,
            column: None,
            locals: HashMap::new(),
        }
    }

    /// Add column information
    #[must_use]
    pub fn with_column(mut self, column: u32) -> Self {
        self.column = Some(column);
        self
    }

    /// Add a local variable
    #[must_use]
    pub fn with_local(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
        self.locals.insert(name.into(), value.into());
        self
    }
}

impl fmt::Display for TracebackFrame {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "  File \"{}\", line {}", self.file, self.line)?;
        if let Some(col) = self.column {
            write!(f, ", column {col}")?;
        }
        write!(f, ", in {}", self.function)?;
        Ok(())
    }
}

/// A structured exception from task execution
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TaskException {
    /// Exception type name (e.g., "`ValueError`", "`TimeoutError`")
    pub exc_type: String,
    /// Exception message
    pub exc_message: String,
    /// Full traceback as structured frames
    #[serde(default)]
    pub traceback: Vec<TracebackFrame>,
    /// Raw traceback string (for Python compatibility)
    #[serde(default)]
    pub traceback_str: Option<String>,
    /// Exception category
    #[serde(default)]
    pub category: ExceptionCategory,
    /// Cause exception (for chained exceptions)
    #[serde(default)]
    pub cause: Option<Box<TaskException>>,
    /// Context exception (for exception context)
    #[serde(default)]
    pub context: Option<Box<TaskException>>,
    /// Additional metadata
    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
    pub metadata: HashMap<String, serde_json::Value>,
    /// Timestamp when exception occurred (Unix timestamp)
    #[serde(default)]
    pub timestamp: Option<f64>,
    /// Worker hostname where exception occurred
    #[serde(default)]
    pub hostname: Option<String>,
    /// Task ID that raised the exception
    #[serde(default)]
    pub task_id: Option<String>,
}

impl TaskException {
    /// Create a new task exception
    pub fn new(exc_type: impl Into<String>, exc_message: impl Into<String>) -> Self {
        Self {
            exc_type: exc_type.into(),
            exc_message: exc_message.into(),
            traceback: Vec::new(),
            traceback_str: None,
            category: ExceptionCategory::Unknown,
            cause: None,
            context: None,
            metadata: HashMap::new(),
            timestamp: None,
            hostname: None,
            task_id: None,
        }
    }

    /// Create from a Rust error
    pub fn from_error<E: std::error::Error>(error: &E) -> Self {
        let exc_type = std::any::type_name::<E>()
            .rsplit("::")
            .next()
            .unwrap_or("Error")
            .to_string();

        let mut exception = Self::new(exc_type, error.to_string());

        // Capture error chain as cause
        if let Some(source) = error.source() {
            exception.cause = Some(Box::new(Self::from_error_dyn(source)));
        }

        exception
    }

    /// Create from a dynamic error reference
    fn from_error_dyn(error: &dyn std::error::Error) -> Self {
        let exc_type = "Error".to_string();
        let mut exception = Self::new(exc_type, error.to_string());

        if let Some(source) = error.source() {
            exception.cause = Some(Box::new(Self::from_error_dyn(source)));
        }

        exception
    }

    /// Add structured traceback frames
    #[must_use]
    pub fn with_traceback(mut self, frames: Vec<(String, String, u32)>) -> Self {
        self.traceback = frames
            .into_iter()
            .map(|(func, file, line)| TracebackFrame::new(func, file, line))
            .collect();
        self
    }

    /// Add traceback frames
    #[must_use]
    pub fn with_traceback_frames(mut self, frames: Vec<TracebackFrame>) -> Self {
        self.traceback = frames;
        self
    }

    /// Add raw traceback string
    #[must_use]
    pub fn with_traceback_str(mut self, traceback: impl Into<String>) -> Self {
        self.traceback_str = Some(traceback.into());
        self
    }

    /// Set exception category
    #[must_use]
    pub fn with_category(mut self, category: ExceptionCategory) -> Self {
        self.category = category;
        self
    }

    /// Set cause exception
    #[must_use]
    pub fn with_cause(mut self, cause: TaskException) -> Self {
        self.cause = Some(Box::new(cause));
        self
    }

    /// Set context exception
    #[must_use]
    pub fn with_context(mut self, context: TaskException) -> Self {
        self.context = Some(Box::new(context));
        self
    }

    /// Add metadata
    #[must_use]
    pub fn with_metadata(mut self, key: impl Into<String>, value: serde_json::Value) -> Self {
        self.metadata.insert(key.into(), value);
        self
    }

    /// Set timestamp
    #[must_use]
    pub fn with_timestamp(mut self, timestamp: f64) -> Self {
        self.timestamp = Some(timestamp);
        self
    }

    /// Set timestamp to now
    #[must_use]
    pub fn with_timestamp_now(mut self) -> Self {
        use std::time::{SystemTime, UNIX_EPOCH};
        self.timestamp = Some(
            SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap_or_default()
                .as_secs_f64(),
        );
        self
    }

    /// Set hostname
    #[must_use]
    pub fn with_hostname(mut self, hostname: impl Into<String>) -> Self {
        self.hostname = Some(hostname.into());
        self
    }

    /// Set task ID
    #[must_use]
    pub fn with_task_id(mut self, task_id: impl Into<String>) -> Self {
        self.task_id = Some(task_id.into());
        self
    }

    /// Check if this exception is retryable
    #[inline]
    #[must_use]
    pub const fn is_retryable(&self) -> bool {
        self.category as u8 == ExceptionCategory::Retryable as u8
    }

    /// Check if this exception is fatal
    #[inline]
    #[must_use]
    pub const fn is_fatal(&self) -> bool {
        self.category as u8 == ExceptionCategory::Fatal as u8
    }

    /// Check if this exception should be ignored
    #[inline]
    #[must_use]
    pub const fn is_ignorable(&self) -> bool {
        self.category as u8 == ExceptionCategory::Ignorable as u8
    }

    /// Get the full exception chain as a vector
    #[must_use]
    pub fn exception_chain(&self) -> Vec<&TaskException> {
        let mut chain = vec![self];
        let mut current = self;

        while let Some(cause) = &current.cause {
            chain.push(cause);
            current = cause;
        }

        chain
    }

    /// Format the traceback as a string
    #[must_use]
    pub fn format_traceback(&self) -> String {
        use std::fmt::Write;

        if let Some(ref tb_str) = self.traceback_str {
            return tb_str.clone();
        }

        if self.traceback.is_empty() {
            return String::new();
        }

        let mut result = String::from("Traceback (most recent call last):\n");
        for frame in &self.traceback {
            let _ = writeln!(result, "{frame}");
        }
        let _ = write!(result, "{}: {}", self.exc_type, self.exc_message);
        result
    }

    /// Serialize to JSON for cross-language compatibility
    ///
    /// # Errors
    ///
    /// Returns an error if serialization fails.
    pub fn to_json(&self) -> Result<String, serde_json::Error> {
        serde_json::to_string(self)
    }

    /// Deserialize from JSON
    ///
    /// # Errors
    ///
    /// Returns an error if deserialization fails.
    pub fn from_json(json: &str) -> Result<Self, serde_json::Error> {
        serde_json::from_str(json)
    }

    /// Convert to Celery-compatible format
    #[must_use]
    pub fn to_celery_format(&self) -> serde_json::Value {
        serde_json::json!({
            "exc_type": self.exc_type,
            "exc_message": self.exc_message,
            "exc_module": self.metadata.get("module").cloned().unwrap_or(serde_json::Value::Null),
            "traceback": self.traceback_str.clone().unwrap_or_else(|| self.format_traceback()),
        })
    }
}

impl fmt::Display for TaskException {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}: {}", self.exc_type, self.exc_message)
    }
}

impl std::error::Error for TaskException {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        self.cause
            .as_ref()
            .map(|e| e.as_ref() as &dyn std::error::Error)
    }
}

/// Policy for handling exceptions
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExceptionPolicy {
    /// Exception types to retry on (by type name pattern)
    #[serde(default)]
    pub retry_on: Vec<String>,
    /// Exception types to ignore (task succeeds despite exception)
    #[serde(default)]
    pub ignore_on: Vec<String>,
    /// Exception types to fail on (no retry, go to DLQ)
    #[serde(default)]
    pub fail_on: Vec<String>,
    /// Exception types to reject (no retry, no DLQ)
    #[serde(default)]
    pub reject_on: Vec<String>,
    /// Default action for unmatched exceptions
    #[serde(default)]
    pub default_action: ExceptionAction,
    /// Whether to preserve full traceback
    #[serde(default = "default_true")]
    pub preserve_traceback: bool,
    /// Maximum traceback depth to preserve
    #[serde(default)]
    pub max_traceback_depth: Option<usize>,
    /// Custom metadata to include in exceptions
    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
    pub include_metadata: HashMap<String, bool>,
}

fn default_true() -> bool {
    true
}

impl Default for ExceptionPolicy {
    fn default() -> Self {
        Self {
            retry_on: Vec::new(),
            ignore_on: Vec::new(),
            fail_on: Vec::new(),
            reject_on: Vec::new(),
            default_action: ExceptionAction::default(),
            preserve_traceback: true, // Default to preserving traceback
            max_traceback_depth: None,
            include_metadata: HashMap::new(),
        }
    }
}

impl ExceptionPolicy {
    /// Create a new exception policy with defaults
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Set exception types to retry on
    #[must_use]
    pub fn retry_on(mut self, types: &[&str]) -> Self {
        self.retry_on = types.iter().map(std::string::ToString::to_string).collect();
        self
    }

    /// Add exception type to retry on
    #[must_use]
    pub fn add_retry_on(mut self, exc_type: impl Into<String>) -> Self {
        self.retry_on.push(exc_type.into());
        self
    }

    /// Set exception types to ignore
    #[must_use]
    pub fn ignore_on(mut self, types: &[&str]) -> Self {
        self.ignore_on = types.iter().map(std::string::ToString::to_string).collect();
        self
    }

    /// Add exception type to ignore
    #[must_use]
    pub fn add_ignore_on(mut self, exc_type: impl Into<String>) -> Self {
        self.ignore_on.push(exc_type.into());
        self
    }

    /// Set exception types to fail on
    #[must_use]
    pub fn fail_on(mut self, types: &[&str]) -> Self {
        self.fail_on = types.iter().map(std::string::ToString::to_string).collect();
        self
    }

    /// Add exception type to fail on
    #[must_use]
    pub fn add_fail_on(mut self, exc_type: impl Into<String>) -> Self {
        self.fail_on.push(exc_type.into());
        self
    }

    /// Set exception types to reject
    #[must_use]
    pub fn reject_on(mut self, types: &[&str]) -> Self {
        self.reject_on = types.iter().map(std::string::ToString::to_string).collect();
        self
    }

    /// Add exception type to reject
    #[must_use]
    pub fn add_reject_on(mut self, exc_type: impl Into<String>) -> Self {
        self.reject_on.push(exc_type.into());
        self
    }

    /// Set default action
    #[must_use]
    pub fn with_default_action(mut self, action: ExceptionAction) -> Self {
        self.default_action = action;
        self
    }

    /// Set traceback preservation
    #[must_use]
    pub fn with_traceback(mut self, preserve: bool) -> Self {
        self.preserve_traceback = preserve;
        self
    }

    /// Set maximum traceback depth
    #[must_use]
    pub fn with_max_traceback_depth(mut self, depth: usize) -> Self {
        self.max_traceback_depth = Some(depth);
        self
    }

    /// Get the action to take for an exception
    #[must_use]
    pub fn get_action(&self, exception: &TaskException) -> ExceptionAction {
        let exc_type = &exception.exc_type;

        // Check ignore first (takes precedence)
        if self.matches_pattern(exc_type, &self.ignore_on) {
            return ExceptionAction::Ignore;
        }

        // Check reject
        if self.matches_pattern(exc_type, &self.reject_on) {
            return ExceptionAction::Reject;
        }

        // Check fail
        if self.matches_pattern(exc_type, &self.fail_on) {
            return ExceptionAction::Fail;
        }

        // Check retry
        if self.matches_pattern(exc_type, &self.retry_on) {
            return ExceptionAction::Retry;
        }
        // Use category-based decision if available
        match exception.category {
            ExceptionCategory::Retryable => ExceptionAction::Retry,
            ExceptionCategory::Fatal | ExceptionCategory::RequiresIntervention => {
                ExceptionAction::Fail
            }
            ExceptionCategory::Ignorable => ExceptionAction::Ignore,
            ExceptionCategory::Unknown => self.default_action,
        }
    }

    /// Check if exception type matches any pattern
    #[allow(clippy::unused_self)]
    fn matches_pattern(&self, exc_type: &str, patterns: &[String]) -> bool {
        for pattern in patterns {
            if pattern == exc_type {
                return true;
            }
            // Support contains pattern (*middle*)
            if let Some(rest) = pattern.strip_prefix('*') {
                if let Some(middle) = rest.strip_suffix('*') {
                    if !middle.is_empty() && exc_type.contains(middle) {
                        return true;
                    }
                } else {
                    // Suffix pattern (*suffix)
                    if exc_type.ends_with(rest) {
                        return true;
                    }
                }
            }
            // Prefix pattern (prefix*)
            if let Some(prefix) = pattern.strip_suffix('*') {
                if exc_type.starts_with(prefix) {
                    return true;
                }
            }
        }
        false
    }

    /// Process an exception according to policy
    #[must_use]
    pub fn process_exception(&self, mut exception: TaskException) -> TaskException {
        // Truncate traceback if needed
        if let Some(max_depth) = self.max_traceback_depth {
            if exception.traceback.len() > max_depth {
                exception.traceback.truncate(max_depth);
            }
        }

        // Clear traceback if not preserving
        if !self.preserve_traceback {
            exception.traceback.clear();
            exception.traceback_str = None;
        }

        exception
    }
}

/// Trait for custom exception handlers
pub trait ExceptionHandler: Send + Sync {
    /// Handle an exception and return the action to take
    fn handle(&self, exception: &TaskException) -> ExceptionAction;

    /// Transform an exception (e.g., add metadata, modify traceback)
    fn transform(&self, exception: TaskException) -> TaskException {
        exception
    }

    /// Called before the exception is processed
    fn on_exception(&self, _exception: &TaskException) {}

    /// Get handler name for logging
    fn name(&self) -> &'static str {
        "ExceptionHandler"
    }
}

/// A chain of exception handlers
#[derive(Default)]
pub struct ExceptionHandlerChain {
    handlers: Vec<Box<dyn ExceptionHandler>>,
}

impl ExceptionHandlerChain {
    /// Create a new handler chain
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Add a handler to the chain
    #[must_use]
    pub fn add_handler<H: ExceptionHandler + 'static>(mut self, handler: H) -> Self {
        self.handlers.push(Box::new(handler));
        self
    }

    /// Handle an exception through the chain
    ///
    /// Returns the first non-Default action, or Default if all handlers return Default
    #[must_use]
    pub fn handle(&self, exception: &TaskException) -> ExceptionAction {
        for handler in &self.handlers {
            handler.on_exception(exception);
            let action = handler.handle(exception);
            if action != ExceptionAction::Default {
                return action;
            }
        }
        ExceptionAction::Default
    }

    /// Transform an exception through all handlers
    #[must_use]
    pub fn transform(&self, mut exception: TaskException) -> TaskException {
        for handler in &self.handlers {
            exception = handler.transform(exception);
        }
        exception
    }
}

/// Built-in handler that logs exceptions
pub struct LoggingExceptionHandler {
    /// Log level for exceptions
    pub log_level: tracing::Level,
}

impl Default for LoggingExceptionHandler {
    fn default() -> Self {
        Self {
            log_level: tracing::Level::ERROR,
        }
    }
}

impl LoggingExceptionHandler {
    /// Create a new logging handler
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Set log level
    #[must_use]
    pub fn with_level(mut self, level: tracing::Level) -> Self {
        self.log_level = level;
        self
    }
}

impl ExceptionHandler for LoggingExceptionHandler {
    fn handle(&self, _exception: &TaskException) -> ExceptionAction {
        ExceptionAction::Default
    }

    fn on_exception(&self, exception: &TaskException) {
        match self.log_level {
            tracing::Level::ERROR => {
                tracing::error!(
                    exc_type = %exception.exc_type,
                    exc_message = %exception.exc_message,
                    task_id = ?exception.task_id,
                    "Task exception occurred"
                );
            }
            tracing::Level::WARN => {
                tracing::warn!(
                    exc_type = %exception.exc_type,
                    exc_message = %exception.exc_message,
                    task_id = ?exception.task_id,
                    "Task exception occurred"
                );
            }
            _ => {
                tracing::info!(
                    exc_type = %exception.exc_type,
                    exc_message = %exception.exc_message,
                    task_id = ?exception.task_id,
                    "Task exception occurred"
                );
            }
        }
    }

    fn name(&self) -> &'static str {
        "LoggingExceptionHandler"
    }
}

/// Built-in handler based on exception policy
pub struct PolicyExceptionHandler {
    policy: ExceptionPolicy,
}

impl PolicyExceptionHandler {
    /// Create a handler from a policy
    #[must_use]
    pub fn new(policy: ExceptionPolicy) -> Self {
        Self { policy }
    }
}

impl ExceptionHandler for PolicyExceptionHandler {
    fn handle(&self, exception: &TaskException) -> ExceptionAction {
        self.policy.get_action(exception)
    }

    fn transform(&self, exception: TaskException) -> TaskException {
        self.policy.process_exception(exception)
    }

    fn name(&self) -> &'static str {
        "PolicyExceptionHandler"
    }
}

/// Common exception types for categorization
pub mod exception_types {
    /// Network-related exceptions (typically retryable)
    pub const NETWORK_EXCEPTIONS: &[&str] = &[
        "ConnectionError",
        "TimeoutError",
        "ConnectionRefused",
        "ConnectionReset",
        "BrokenPipe",
        "NetworkError",
        "SocketError",
        "DNSError",
    ];

    /// Database-related exceptions (often retryable)
    pub const DATABASE_EXCEPTIONS: &[&str] = &[
        "DatabaseError",
        "OperationalError",
        "InterfaceError",
        "ConnectionPoolError",
        "DeadlockError",
        "LockTimeout",
    ];

    /// Validation exceptions (typically not retryable)
    pub const VALIDATION_EXCEPTIONS: &[&str] = &[
        "ValidationError",
        "ValueError",
        "TypeError",
        "InvalidArgument",
        "SchemaError",
    ];

    /// Resource exceptions (may be retryable)
    pub const RESOURCE_EXCEPTIONS: &[&str] = &[
        "ResourceExhausted",
        "QuotaExceeded",
        "RateLimitExceeded",
        "OutOfMemory",
        "DiskFull",
    ];

    /// Authentication exceptions (typically not retryable)
    pub const AUTH_EXCEPTIONS: &[&str] = &[
        "AuthenticationError",
        "AuthorizationError",
        "PermissionDenied",
        "TokenExpired",
        "InvalidCredentials",
    ];
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_task_exception_creation() {
        let exc = TaskException::new("ValueError", "Invalid input");
        assert_eq!(exc.exc_type, "ValueError");
        assert_eq!(exc.exc_message, "Invalid input");
        assert_eq!(exc.category, ExceptionCategory::Unknown);
    }

    #[test]
    fn test_task_exception_with_traceback() {
        let exc = TaskException::new("RuntimeError", "Something went wrong").with_traceback(vec![
            ("main".to_string(), "app.rs".to_string(), 10),
            ("process".to_string(), "tasks.rs".to_string(), 25),
        ]);

        assert_eq!(exc.traceback.len(), 2);
        assert_eq!(exc.traceback[0].function, "main");
        assert_eq!(exc.traceback[0].file, "app.rs");
        assert_eq!(exc.traceback[0].line, 10);
    }

    #[test]
    fn test_task_exception_with_cause() {
        let cause = TaskException::new("IOError", "File not found");
        let exc = TaskException::new("ProcessingError", "Failed to process file").with_cause(cause);

        assert!(exc.cause.is_some());
        assert_eq!(exc.cause.as_ref().unwrap().exc_type, "IOError");
    }

    #[test]
    fn test_exception_chain() {
        let root = TaskException::new("RootError", "Root cause");
        let middle = TaskException::new("MiddleError", "Middle error").with_cause(root);
        let top = TaskException::new("TopError", "Top level error").with_cause(middle);

        let chain = top.exception_chain();
        assert_eq!(chain.len(), 3);
        assert_eq!(chain[0].exc_type, "TopError");
        assert_eq!(chain[1].exc_type, "MiddleError");
        assert_eq!(chain[2].exc_type, "RootError");
    }

    #[test]
    fn test_exception_category() {
        let retryable = TaskException::new("TimeoutError", "Request timed out")
            .with_category(ExceptionCategory::Retryable);
        let fatal = TaskException::new("ValidationError", "Invalid data")
            .with_category(ExceptionCategory::Fatal);

        assert!(retryable.is_retryable());
        assert!(!retryable.is_fatal());
        assert!(fatal.is_fatal());
        assert!(!fatal.is_retryable());
    }

    #[test]
    fn test_exception_policy_retry_on() {
        let policy = ExceptionPolicy::new().retry_on(&["TimeoutError", "ConnectionError"]);

        let timeout_exc = TaskException::new("TimeoutError", "Timed out");
        let validation_exc = TaskException::new("ValidationError", "Invalid");

        assert_eq!(policy.get_action(&timeout_exc), ExceptionAction::Retry);
        assert_eq!(policy.get_action(&validation_exc), ExceptionAction::Default);
    }

    #[test]
    fn test_exception_policy_ignore_on() {
        let policy = ExceptionPolicy::new().ignore_on(&["NotFoundError"]);

        let not_found = TaskException::new("NotFoundError", "Resource not found");
        assert_eq!(policy.get_action(&not_found), ExceptionAction::Ignore);
    }

    #[test]
    fn test_exception_policy_fail_on() {
        let policy = ExceptionPolicy::new().fail_on(&["ValidationError"]);

        let validation = TaskException::new("ValidationError", "Invalid input");
        assert_eq!(policy.get_action(&validation), ExceptionAction::Fail);
    }

    #[test]
    fn test_exception_policy_pattern_matching() {
        let policy = ExceptionPolicy::new()
            .retry_on(&["*Error"])
            .fail_on(&["Validation*"]);

        let timeout = TaskException::new("TimeoutError", "Timed out");
        let validation = TaskException::new("ValidationFailed", "Invalid");

        // fail_on takes precedence due to order in get_action
        assert_eq!(policy.get_action(&validation), ExceptionAction::Fail);
        assert_eq!(policy.get_action(&timeout), ExceptionAction::Retry);
    }

    #[test]
    fn test_exception_policy_category_fallback() {
        let policy = ExceptionPolicy::new();

        let retryable =
            TaskException::new("CustomError", "Error").with_category(ExceptionCategory::Retryable);
        let fatal =
            TaskException::new("CustomError", "Error").with_category(ExceptionCategory::Fatal);

        assert_eq!(policy.get_action(&retryable), ExceptionAction::Retry);
        assert_eq!(policy.get_action(&fatal), ExceptionAction::Fail);
    }

    #[test]
    fn test_exception_policy_default_action() {
        let policy = ExceptionPolicy::new().with_default_action(ExceptionAction::Retry);

        let unknown = TaskException::new("UnknownError", "Unknown");
        assert_eq!(policy.get_action(&unknown), ExceptionAction::Retry);
    }

    #[test]
    fn test_exception_policy_traceback_truncation() {
        let policy = ExceptionPolicy::new().with_max_traceback_depth(2);

        let exc = TaskException::new("Error", "Error").with_traceback(vec![
            ("f1".to_string(), "a.rs".to_string(), 1),
            ("f2".to_string(), "b.rs".to_string(), 2),
            ("f3".to_string(), "c.rs".to_string(), 3),
            ("f4".to_string(), "d.rs".to_string(), 4),
        ]);

        let processed = policy.process_exception(exc);
        assert_eq!(processed.traceback.len(), 2);
    }

    #[test]
    fn test_exception_policy_no_traceback() {
        let policy = ExceptionPolicy::new().with_traceback(false);

        let exc = TaskException::new("Error", "Error")
            .with_traceback(vec![("f1".to_string(), "a.rs".to_string(), 1)])
            .with_traceback_str("Traceback...");

        let processed = policy.process_exception(exc);
        assert!(processed.traceback.is_empty());
        assert!(processed.traceback_str.is_none());
    }

    #[test]
    fn test_traceback_frame_display() {
        let frame = TracebackFrame::new("process_data", "tasks.py", 42).with_column(10);

        let display = format!("{frame}");
        assert!(display.contains("tasks.py"));
        assert!(display.contains("42"));
        assert!(display.contains("process_data"));
        assert!(display.contains("column 10"));
    }

    #[test]
    fn test_task_exception_serialization() {
        let exc = TaskException::new("TestError", "Test message")
            .with_category(ExceptionCategory::Retryable)
            .with_traceback(vec![("test".to_string(), "test.rs".to_string(), 1)]);

        let json = exc.to_json().unwrap();
        let parsed = TaskException::from_json(&json).unwrap();

        assert_eq!(parsed.exc_type, "TestError");
        assert_eq!(parsed.exc_message, "Test message");
        assert_eq!(parsed.category, ExceptionCategory::Retryable);
        assert_eq!(parsed.traceback.len(), 1);
    }

    #[test]
    fn test_task_exception_celery_format() {
        let exc = TaskException::new("ValueError", "Invalid value")
            .with_traceback_str("Traceback (most recent call last):\n  File \"test.py\"");

        let celery = exc.to_celery_format();
        assert_eq!(celery["exc_type"], "ValueError");
        assert_eq!(celery["exc_message"], "Invalid value");
    }

    #[test]
    fn test_exception_display() {
        let exc = TaskException::new("ValueError", "Invalid input");
        assert_eq!(format!("{exc}"), "ValueError: Invalid input");
    }

    #[test]
    fn test_exception_handler_chain() {
        struct RetryHandler;
        impl ExceptionHandler for RetryHandler {
            fn handle(&self, exc: &TaskException) -> ExceptionAction {
                if exc.exc_type.contains("Timeout") {
                    ExceptionAction::Retry
                } else {
                    ExceptionAction::Default
                }
            }
            fn name(&self) -> &'static str {
                "RetryHandler"
            }
        }

        struct FailHandler;
        impl ExceptionHandler for FailHandler {
            fn handle(&self, exc: &TaskException) -> ExceptionAction {
                if exc.exc_type.contains("Fatal") {
                    ExceptionAction::Fail
                } else {
                    ExceptionAction::Default
                }
            }
            fn name(&self) -> &'static str {
                "FailHandler"
            }
        }

        let chain = ExceptionHandlerChain::new()
            .add_handler(RetryHandler)
            .add_handler(FailHandler);

        let timeout = TaskException::new("TimeoutError", "Timed out");
        let fatal = TaskException::new("FatalError", "Fatal");
        let unknown = TaskException::new("UnknownError", "Unknown");

        assert_eq!(chain.handle(&timeout), ExceptionAction::Retry);
        assert_eq!(chain.handle(&fatal), ExceptionAction::Fail);
        assert_eq!(chain.handle(&unknown), ExceptionAction::Default);
    }

    #[test]
    fn test_policy_exception_handler() {
        let policy = ExceptionPolicy::new()
            .retry_on(&["TimeoutError"])
            .fail_on(&["ValidationError"]);

        let handler = PolicyExceptionHandler::new(policy);

        let timeout = TaskException::new("TimeoutError", "Timed out");
        let validation = TaskException::new("ValidationError", "Invalid");

        assert_eq!(handler.handle(&timeout), ExceptionAction::Retry);
        assert_eq!(handler.handle(&validation), ExceptionAction::Fail);
    }
}