Skip to main content

mangofetch_core/core/
errors.rs

1use thiserror::Error;
2
3#[derive(Error, Debug)]
4pub enum MangoError {
5    #[error("IO error: {0}")]
6    Io(#[from] std::io::Error),
7
8    #[error("Network error: {0}")]
9    Network(#[from] reqwest::Error),
10
11    #[error("Serialization error: {0}")]
12    Serialization(#[from] serde_json::Error),
13
14    #[error("Dependency missing: {0}")]
15    DependencyMissing(String),
16
17    #[error("Process error: {0}")]
18    Process(String),
19
20    #[error("Download error: {0}")]
21    Download(String),
22
23    #[error("Authentication required: {0}")]
24    AuthRequired(String),
25
26    #[error("Rate limited: {0}")]
27    RateLimited(String),
28
29    #[error("Content restricted: {0}")]
30    Restricted(String),
31
32    #[error("Content not found: {0}")]
33    NotFound(String),
34
35    #[error("FFmpeg error: {0}")]
36    FFmpeg(String),
37
38    #[error("Internal error: {0}")]
39    Internal(String),
40
41    #[error("{0}")]
42    Custom(String),
43}
44
45pub type MangoResult<T> = Result<T, MangoError>;
46
47pub fn classify_download_error(error: &str) -> (&str, &str) {
48    let lower = error.to_lowercase();
49
50    if lower.contains("cookie")
51        || lower.contains("login")
52        || lower.contains("sign in")
53        || lower.contains("authentication")
54        || lower.contains("403")
55    {
56        return ("auth_required", "This content requires login. Install the browser extension and visit the site while logged in.");
57    }
58
59    if lower.contains("captcha")
60        || lower.contains("blocking")
61        || lower.contains("rate limit")
62        || lower.contains("429")
63        || lower.contains("too many")
64    {
65        return (
66            "rate_limited",
67            "Too many requests. Try again in a few minutes.",
68        );
69    }
70
71    if lower.contains("private") || lower.contains("restricted") || lower.contains("age") {
72        return ("restricted", "This content is private or age-restricted.");
73    }
74
75    if lower.contains("downloaded file") && lower.contains("not found") {
76        return (
77            "file_missing",
78            "Downloaded file could not be located in the output folder.",
79        );
80    }
81
82    if lower.contains("not found")
83        || lower.contains("404")
84        || lower.contains("unavailable")
85        || lower.contains("deleted")
86    {
87        return ("not_found", "Content not found or has been deleted.");
88    }
89
90    if lower.contains("ffmpeg") || lower.contains("mux") || lower.contains("merge") {
91        return (
92            "ffmpeg_needed",
93            "FFmpeg is required for this download. Install it from Settings.",
94        );
95    }
96
97    if lower.contains("yt-dlp") || lower.contains("ytdlp") || lower.contains("no downloader") {
98        return (
99            "ytdlp_needed",
100            "yt-dlp is required. Install it from Settings.",
101        );
102    }
103
104    if lower.contains("nsig") || lower.contains("signature") || lower.contains("cipher") {
105        return (
106            "ytdlp_outdated",
107            "yt-dlp needs updating. Restart the app to auto-update.",
108        );
109    }
110
111    ("unknown", error)
112}