uranium_rs/
error.rs

1use reqwest::header::InvalidHeaderValue;
2use thiserror::Error;
3use tokio::task::JoinError;
4
5use crate::downloaders::DownloadableObject;
6
7pub type Result<T> = std::result::Result<T, UraniumError>;
8
9#[derive(Debug, Error)]
10pub enum UraniumError {
11    #[error("Wrong file format")]
12    WrongFileFormat,
13    #[error("Wrong modpack format")]
14    WrongModpackFormat,
15    #[error("File `{0}` not found")]
16    FileNotFound(String),
17    #[error("Can't create dir: `{0}`")]
18    CantCreateDir(&'static str),
19    #[error("Error while writing the files: `{0}`")]
20    WriteError(std::io::Error),
21    #[error("IO Error: `{0}`")]
22    IOError(std::io::Error),
23    #[error("Error downloading files")]
24    DownloadError,
25    #[error("Error making the requests: `{0}`")]
26    RequestError(reqwest::Error),
27    #[error("File hash doesnt match")]
28    FileNotMatch(DownloadableObject),
29    #[error("Files hashes doesnt match")]
30    FilesDontMatch(Vec<DownloadableObject>),
31    #[error("Zip Error: `{0}`")]
32    ZipError(zip::result::ZipError),
33    #[error("Can't compress the modpack")]
34    CantCompress,
35    #[error("Can't remove temp JSON file")]
36    CantRemoveJSON,
37    #[error("Can't read mods dir")]
38    CantReadModsDir,
39    #[error("Error in async task")]
40    AsyncRuntimeError,
41    #[error("Error")]
42    Other,
43    #[error("Error: `{0}`")]
44    OtherWithReason(String),
45}
46
47impl UraniumError {
48    pub fn other(msg: &str) -> Self {
49        Self::OtherWithReason(msg.to_string())
50    }
51}
52
53impl From<reqwest::Error> for UraniumError {
54    fn from(value: reqwest::Error) -> Self {
55        UraniumError::RequestError(value)
56    }
57}
58
59impl From<InvalidHeaderValue> for UraniumError {
60    fn from(_value: InvalidHeaderValue) -> Self {
61        UraniumError::Other
62    }
63}
64
65impl From<std::io::Error> for UraniumError {
66    fn from(value: std::io::Error) -> Self {
67        type Ioe = std::io::ErrorKind;
68        match value.kind() {
69            Ioe::PermissionDenied | Ioe::NotFound => UraniumError::WriteError(value),
70            _ => UraniumError::IOError(value),
71        }
72    }
73}
74
75impl From<zip::result::ZipError> for UraniumError {
76    fn from(value: zip::result::ZipError) -> Self {
77        UraniumError::ZipError(value)
78    }
79}
80
81impl From<JoinError> for UraniumError {
82    // TODO!: Add more variants ?.? ?
83    fn from(_value: JoinError) -> Self {
84        Self::AsyncRuntimeError
85    }
86}