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    /// A frame was pushed to an invalid input slot.
18    #[error("invalid input: slot={slot} reason={reason}")]
19    InvalidInput {
20        /// The slot index that was out of range or otherwise invalid.
21        slot: usize,
22        /// Human-readable reason for the failure.
23        reason: String,
24    },
25
26    /// An underlying `FFmpeg` function returned an error code.
27    #[error("ffmpeg error: {message} (code={code})")]
28    Ffmpeg {
29        /// The raw `FFmpeg` error code.
30        code: i32,
31        /// Human-readable description of the error.
32        message: String,
33    },
34}
35
36#[cfg(test)]
37mod tests {
38    use super::FilterError;
39    use std::error::Error;
40
41    #[test]
42    fn build_failed_should_display_correct_message() {
43        let err = FilterError::BuildFailed;
44        assert_eq!(err.to_string(), "failed to build filter graph");
45    }
46
47    #[test]
48    fn process_failed_should_display_correct_message() {
49        let err = FilterError::ProcessFailed;
50        assert_eq!(err.to_string(), "failed to process frame");
51    }
52
53    #[test]
54    fn invalid_input_should_display_slot_and_reason() {
55        let err = FilterError::InvalidInput {
56            slot: 2,
57            reason: "slot out of range".to_string(),
58        };
59        assert_eq!(
60            err.to_string(),
61            "invalid input: slot=2 reason=slot out of range"
62        );
63    }
64
65    #[test]
66    fn ffmpeg_should_display_code_and_message() {
67        let err = FilterError::Ffmpeg {
68            code: -22,
69            message: "Invalid argument".to_string(),
70        };
71        assert_eq!(err.to_string(), "ffmpeg error: Invalid argument (code=-22)");
72    }
73
74    #[test]
75    fn filter_error_should_implement_std_error() {
76        fn assert_error<E: Error>(_: &E) {}
77        assert_error(&FilterError::BuildFailed);
78        assert_error(&FilterError::ProcessFailed);
79        assert_error(&FilterError::InvalidInput {
80            slot: 0,
81            reason: String::new(),
82        });
83        assert_error(&FilterError::Ffmpeg {
84            code: 0,
85            message: String::new(),
86        });
87    }
88}