1use thiserror::Error;
2
3#[derive(Error, Debug, Clone, PartialEq)]
4pub enum Aria2Error {
5 #[error("Network error: {0}")]
6 Network(String),
7
8 #[error("IO error: {0}")]
9 Io(String),
10
11 #[error("Parse error: {0}")]
12 Parse(String),
13
14 #[error("Checksum failed: {0}")]
15 Checksum(String),
16
17 #[error("Download failed: {0}")]
18 DownloadFailed(String),
19
20 #[error("Recoverable error: {0}")]
21 Recoverable(#[from] RecoverableError),
22
23 #[error("Fatal error: {0}")]
24 Fatal(#[from] FatalError),
25}
26
27impl From<serde_json::Error> for Aria2Error {
28 fn from(err: serde_json::Error) -> Self {
29 Aria2Error::Parse(err.to_string())
30 }
31}
32
33impl From<std::io::Error> for Aria2Error {
34 fn from(err: std::io::Error) -> Self {
35 Aria2Error::Io(err.to_string())
36 }
37}
38
39impl From<std::string::FromUtf8Error> for Aria2Error {
40 fn from(err: std::string::FromUtf8Error) -> Self {
41 Aria2Error::Parse(err.to_string())
42 }
43}
44
45impl From<base64::DecodeError> for Aria2Error {
46 fn from(err: base64::DecodeError) -> Self {
47 Aria2Error::Parse(err.to_string())
48 }
49}
50
51#[derive(Error, Debug, Clone, PartialEq)]
52pub enum RecoverableError {
53 #[error("Connection timeout")]
54 Timeout,
55
56 #[error("Server returned error: {code}")]
57 ServerError { code: u16 },
58
59 #[error("Range not satisfiable: {range}")]
60 RangeNotSatisfiable { range: String },
61
62 #[error("Temporary network failure: {message}")]
63 TemporaryNetworkFailure { message: String },
64
65 #[error("Max retries reached: {attempts} attempts")]
66 MaxTriesReached { attempts: u32 },
67
68 #[error("Invalid piece index: {index} (max: {max_index})")]
69 InvalidPieceIndex { index: u32, max_index: u32 },
70}
71
72#[derive(Error, Debug, Clone, PartialEq)]
73pub enum FatalError {
74 #[error("Configuration error: {0}")]
75 Config(String),
76
77 #[error("Insufficient disk space")]
78 DiskSpaceExhausted,
79
80 #[error("Permission denied: {path}")]
81 PermissionDenied { path: String },
82
83 #[error("File not found: {path}")]
84 FileNotFound { path: String },
85
86 #[error("Unsupported protocol: {protocol}")]
87 UnsupportedProtocol { protocol: String },
88}
89
90pub type Result<T> = std::result::Result<T, Aria2Error>;