1use std::path::PathBuf;
4
5use ff_format::{ErrorSeverity, MediaError};
6use thiserror::Error;
7
8#[derive(Debug, Error)]
10pub enum PreviewError {
11 #[error("file not found: path={path}")]
13 FileNotFound {
14 path: PathBuf,
16 },
17
18 #[error("no video stream found: path={path}")]
20 NoVideoStream {
21 path: PathBuf,
23 },
24
25 #[error("seek failed: target={target:?} reason={reason}")]
27 SeekFailed {
28 target: std::time::Duration,
30 reason: String,
32 },
33
34 #[error("decode failed: {0}")]
36 Decode(#[from] ff_decode::DecodeError),
37
38 #[error("ffmpeg error: {message} (code={code})")]
44 Ffmpeg {
45 code: i32,
47 message: String,
49 },
50
51 #[error("probe failed: {0}")]
53 Probe(#[from] ff_probe::ProbeError),
54
55 #[cfg(feature = "proxy")]
57 #[error("pipeline failed: {0}")]
58 Pipeline(#[from] ff_pipeline::PipelineError),
59
60 #[error("io error: {0}")]
62 Io(#[from] std::io::Error),
63
64 #[error("seek out of range: pts={pts:?}")]
66 SeekOutOfRange {
67 pts: std::time::Duration,
69 },
70
71 #[error("decode thread poisoned: the background decoder panicked and cannot be recovered")]
77 DecodeThreadPoisoned,
78}
79
80impl MediaError for PreviewError {
81 fn severity(&self) -> ErrorSeverity {
82 match self {
83 Self::Decode(e) => e.severity(),
84 Self::Probe(e) => e.severity(),
85 #[cfg(feature = "proxy")]
86 Self::Pipeline(e) => e.severity(),
87 Self::SeekFailed { .. } | Self::DecodeThreadPoisoned => ErrorSeverity::Recoverable,
88 Self::Ffmpeg { .. } | Self::SeekOutOfRange { .. } => ErrorSeverity::Other,
89 Self::FileNotFound { .. } | Self::NoVideoStream { .. } | Self::Io(_) => {
90 ErrorSeverity::Fatal
91 }
92 }
93 }
94}
95
96#[cfg(test)]
97mod tests {
98 use super::*;
99
100 #[test]
101 fn preview_io_should_be_fatal() {
102 let e: PreviewError = std::io::Error::other("x").into();
103 assert!(e.is_fatal() && !e.is_recoverable());
104 }
105
106 #[test]
107 fn preview_seek_failed_should_be_recoverable() {
108 let e = PreviewError::SeekFailed {
109 target: std::time::Duration::from_secs(1),
110 reason: "x".into(),
111 };
112 assert!(e.is_recoverable() && !e.is_fatal());
113 }
114
115 #[test]
116 fn preview_decode_thread_poisoned_should_be_recoverable() {
117 let e = PreviewError::DecodeThreadPoisoned;
118 assert!(e.is_recoverable() && !e.is_fatal());
119 assert!(
120 e.to_string().contains("poisoned"),
121 "message must name the condition: {e}"
122 );
123 }
124
125 #[test]
126 fn preview_decode_should_delegate_recoverable() {
127 let e = PreviewError::Decode(ff_decode::DecodeError::decoding_failed("x"));
129 assert!(e.is_recoverable() && !e.is_fatal());
130 }
131}