Skip to main content

ff_encode/preview/
error.rs

1//! Error type for preview-image generation (sprite sheet / GIF preview).
2
3use ff_format::{ErrorSeverity, MediaError};
4use std::path::PathBuf;
5use thiserror::Error;
6
7/// Errors that can occur while generating a sprite sheet or GIF preview.
8#[derive(Error, Debug)]
9pub enum PreviewImageError {
10    /// The output file could not be created.
11    #[error("Cannot create output file: {path}")]
12    CannotCreateFile {
13        /// File path that failed.
14        path: PathBuf,
15    },
16
17    /// The requested codec is not available in this `FFmpeg` build.
18    #[error("Unsupported codec: {codec}")]
19    UnsupportedCodec {
20        /// Codec name.
21        codec: String,
22    },
23
24    /// A preview-generation operation failed for a structural reason (e.g. zero
25    /// rows/cols, or an internal filter-graph/encode step failed).
26    #[error("preview operation failed: {reason}")]
27    OperationFailed {
28        /// Human-readable description of the failure.
29        reason: String,
30    },
31
32    /// An underlying `FFmpeg` function returned an error code.
33    #[error("ffmpeg error: {message} (code={code})")]
34    Ffmpeg {
35        /// Raw `FFmpeg` error code (negative integer). `0` when no numeric code is available.
36        code: i32,
37        /// Human-readable error message from `av_strerror` or an internal description.
38        message: String,
39    },
40
41    /// An I/O error occurred.
42    #[error("IO error: {0}")]
43    Io(#[from] std::io::Error),
44}
45
46impl PreviewImageError {
47    /// Create an error from a raw `FFmpeg` error code, resolving the message via
48    /// `av_strerror`.
49    pub(crate) fn from_ffmpeg_error(errnum: i32) -> Self {
50        PreviewImageError::Ffmpeg {
51            code: errnum,
52            message: ff_sys::av_error_string(errnum),
53        }
54    }
55}
56
57impl MediaError for PreviewImageError {
58    fn severity(&self) -> ErrorSeverity {
59        match self {
60            Self::Ffmpeg { .. } => ErrorSeverity::Other,
61            Self::CannotCreateFile { .. }
62            | Self::UnsupportedCodec { .. }
63            | Self::OperationFailed { .. }
64            | Self::Io(_) => ErrorSeverity::Fatal,
65        }
66    }
67}