about summary refs log tree commit diff stats
path: root/crates/yt_dlp/src/error.rs
blob: 4327f0d2710ffe452c09afecb125d7afea1a844d (plain) (blame)
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
use std::{fmt::Display, io};

#[derive(Debug)]
#[allow(clippy::module_name_repetitions)]
pub enum YtDlpError {
    ResponseParseError { error: serde_json::error::Error },
    PythonError { error: Box<pyo3::PyErr> },
    IoError { error: io::Error },
}

impl std::error::Error for YtDlpError {}

impl Display for YtDlpError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            YtDlpError::ResponseParseError { error } => write!(
                f,
                include_str!("./python_json_decode_failed.error_msg"),
                error
            ),
            YtDlpError::PythonError { error } => write!(f, "Python error: {error}"),
            YtDlpError::IoError { error } => write!(f, "Io error: {error}"),
        }
    }
}

impl From<serde_json::error::Error> for YtDlpError {
    fn from(value: serde_json::error::Error) -> Self {
        Self::ResponseParseError { error: value }
    }
}

impl From<pyo3::PyErr> for YtDlpError {
    fn from(value: pyo3::PyErr) -> Self {
        Self::PythonError {
            error: Box::new(value),
        }
    }
}

impl From<io::Error> for YtDlpError {
    fn from(value: io::Error) -> Self {
        Self::IoError { error: value }
    }
}