Skip to main content

ff_filter/
error.rs

1//! Error types for filter graph operations.
2
3use thiserror::Error;
4
5/// Errors that can occur during filter graph construction and processing.
6#[derive(Debug, Error)]
7pub enum FilterError {
8    /// Failed to build the filter graph (invalid filter chain or `FFmpeg` error
9    /// during graph creation).
10    #[error("failed to build filter graph")]
11    BuildFailed,
12
13    /// A frame processing operation (push or pull) failed.
14    #[error("failed to process frame")]
15    ProcessFailed,
16
17    /// An invalid configuration was detected during graph construction.
18    #[error("invalid filter configuration: {reason}")]
19    InvalidConfig {
20        /// Human-readable reason for the failure.
21        reason: String,
22    },
23
24    /// A frame was pushed to an invalid input slot.
25    #[error("invalid input: slot={slot} reason={reason}")]
26    InvalidInput {
27        /// The slot index that was out of range or otherwise invalid.
28        slot: usize,
29        /// Human-readable reason for the failure.
30        reason: String,
31    },
32
33    /// An underlying `FFmpeg` function returned an error code.
34    #[error("ffmpeg error: {message} (code={code})")]
35    Ffmpeg {
36        /// The raw `FFmpeg` error code.
37        code: i32,
38        /// Human-readable description of the error.
39        message: String,
40    },
41}
42
43#[cfg(test)]
44mod tests {
45    use super::FilterError;
46    use std::error::Error;
47
48    #[test]
49    fn build_failed_should_display_correct_message() {
50        let err = FilterError::BuildFailed;
51        assert_eq!(err.to_string(), "failed to build filter graph");
52    }
53
54    #[test]
55    fn process_failed_should_display_correct_message() {
56        let err = FilterError::ProcessFailed;
57        assert_eq!(err.to_string(), "failed to process frame");
58    }
59
60    #[test]
61    fn invalid_input_should_display_slot_and_reason() {
62        let err = FilterError::InvalidInput {
63            slot: 2,
64            reason: "slot out of range".to_string(),
65        };
66        assert_eq!(
67            err.to_string(),
68            "invalid input: slot=2 reason=slot out of range"
69        );
70    }
71
72    #[test]
73    fn ffmpeg_should_display_code_and_message() {
74        let err = FilterError::Ffmpeg {
75            code: -22,
76            message: "Invalid argument".to_string(),
77        };
78        assert_eq!(err.to_string(), "ffmpeg error: Invalid argument (code=-22)");
79    }
80
81    #[test]
82    fn filter_error_should_implement_std_error() {
83        fn assert_error<E: Error>(_: &E) {}
84        assert_error(&FilterError::BuildFailed);
85        assert_error(&FilterError::ProcessFailed);
86        assert_error(&FilterError::InvalidInput {
87            slot: 0,
88            reason: String::new(),
89        });
90        assert_error(&FilterError::Ffmpeg {
91            code: 0,
92            message: String::new(),
93        });
94    }
95}