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("Temporary network failure: {message}")]
60 TemporaryNetworkFailure { message: String },
61
62 #[error("Max retries reached: {attempts} attempts")]
63 MaxTriesReached { attempts: u32 },
64
65 #[error("Invalid piece index: {index} (max: {max_index})")]
66 InvalidPieceIndex { index: u32, max_index: u32 },
67}
68
69#[derive(Error, Debug, Clone, PartialEq)]
70pub enum FatalError {
71 #[error("Configuration error: {0}")]
72 Config(String),
73
74 #[error("Insufficient disk space")]
75 DiskSpaceExhausted,
76
77 #[error("Permission denied: {path}")]
78 PermissionDenied { path: String },
79
80 #[error("File not found: {path}")]
81 FileNotFound { path: String },
82
83 #[error("Unsupported protocol: {protocol}")]
84 UnsupportedProtocol { protocol: String },
85}
86
87pub type Result<T> = std::result::Result<T, Aria2Error>;