1use serde_repr::{Deserialize_repr, Serialize_repr};
2use thiserror::Error;
3
4pub type Result<T, E = Error> = std::result::Result<T, E>;
6
7#[non_exhaustive]
9#[derive(Debug, Error)]
10pub enum Error {
11 #[error("missing user session (consider logging in first)")]
13 MissingUserSession,
14 #[error("invalid URL format")]
16 InvalidUrlFormat,
17 #[error("the URL too short")]
19 UrlTooShort,
20 #[error("invalid algorithm version")]
22 InvalidAlgorithmVersion {
23 version: u8,
25 },
26 #[error("invalid session kind")]
28 InvalidSessionKind,
29 #[error("invalid (or unsupported) public URL format")]
31 InvalidPublicUrlFormat,
32 #[error("invalid node checksum format")]
34 InvalidChecksumFormat,
35 #[error("invalid server response type")]
37 InvalidResponseType,
38 #[error("invalid response format")]
40 InvalidResponseFormat,
41 #[error("missing response field: `{field}`")]
43 MissingResponseField {
44 field: &'static str,
46 },
47 #[error("unknown user login version: `{version}`")]
49 UnknownUserLoginVersion {
50 version: i32,
52 },
53 #[error("invalid RSA private key format")]
55 InvalidRsaPrivateKeyFormat,
56 #[error("condensed MAC mismatch")]
58 CondensedMacMismatch,
59 #[error("failed to find node")]
61 NodeNotFound,
62 #[error("failed to find node attribute")]
64 NodeAttributeNotFound,
65 #[error("could not get a meaningful response after maximum retries")]
67 MaxRetriesReached,
68 #[error("the involved event cursors do not match, continuing would result in inconsistencies")]
70 EventCursorMismatch,
71 #[error("UTF-8 validation error: {source}")]
73 FromUtf8Error {
74 #[from]
76 source: std::string::FromUtf8Error,
77 },
78 #[error("integer parse error: {source}")]
80 ParseIntError {
81 #[from]
83 source: std::num::ParseIntError,
84 },
85 #[cfg(feature = "reqwest")]
87 #[error("`reqwest` error: {source}")]
88 ReqwestError {
89 #[from]
91 source: reqwest::Error,
92 },
93 #[error("URL parse error: {source}")]
95 UrlError {
96 #[from]
98 source: url::ParseError,
99 },
100 #[error("JSON error: {source}")]
102 JsonError {
103 #[from]
105 source: json::Error,
106 },
107 #[error("base64 encode error: {source}")]
109 Base64EncodeError {
110 #[from]
112 source: base64::EncodeSliceError,
113 },
114 #[error("base64 encode error: {source}")]
116 Base64DecodeError {
117 #[from]
119 source: base64::DecodeError,
120 },
121 #[error("HKDF error: {source}")]
123 HkdfInvalidLengthError {
124 #[from]
126 source: hkdf::InvalidLength,
127 },
128 #[error("HKDF error: {source}")]
130 HkdfInvalidPrkLengthError {
131 #[from]
133 source: hkdf::InvalidPrkLength,
134 },
135 #[error("HMAC mismatch (invalid link or wrong password)")]
137 HmacMismatch {
138 #[from]
140 source: hmac::digest::MacError,
141 },
142 #[error("AES-GCM error: {source}")]
144 AesGcmError {
145 #[from]
147 source: aes_gcm::Error,
148 },
149 #[error("MEGA error: {code}")]
151 MegaError {
152 #[from]
154 code: ErrorCode,
155 },
156 #[error("IO error: {source}")]
158 IoError {
159 #[from]
161 source: std::io::Error,
162 },
163 #[error("other error: {source}")]
165 Other {
166 #[from]
168 source: Box<dyn std::error::Error + Send + Sync>,
169 },
170}
171
172#[repr(i8)]
174#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Error, Serialize_repr, Deserialize_repr)]
175pub enum ErrorCode {
176 #[error("no error")]
178 OK = 0,
179 #[error("internal error")]
181 EINTERNAL = -1,
182 #[error("invalid argument")]
184 EARGS = -2,
185 #[error("request failed, retrying")]
187 EAGAIN = -3,
188 #[error("rate limit exceeded")]
190 ERATELIMIT = -4,
191 #[error("failed permanently")]
193 EFAILED = -5,
194 #[error("too many concurrent connections or transfers")]
196 ETOOMANY = -6,
197 #[error("out of range")]
199 ERANGE = -7,
200 #[error("expired")]
202 EEXPIRED = -8,
203 #[error("not found")]
205 ENOENT = -9,
206 #[error("circular linkage detected")]
208 ECIRCULAR = -10,
209 #[error("access denied")]
211 EACCESS = -11,
212 #[error("already exists")]
214 EEXIST = -12,
215 #[error("incomplete")]
217 EINCOMPLETE = -13,
218 #[error("invalid key / decryption error")]
220 EKEY = -14,
221 #[error("bad session ID")]
223 ESID = -15,
224 #[error("blocked")]
226 EBLOCKED = -16,
227 #[error("over quota")]
229 EOVERQUOTA = -17,
230 #[error("temporarily not available")]
232 ETEMPUNAVAIL = -18,
233 #[error("connection overflow")]
235 ETOOMANYCONNECTIONS = -19,
236 #[error("write error")]
238 EWRITE = -20,
239 #[error("read error")]
241 EREAD = -21,
242 #[error("invalid application key")]
244 EAPPKEY = -22,
245 #[error("SSL verification failed")]
247 ESSL = -23,
248 #[error("not enough quota")]
250 EGOINGOVERQUOTA = -24,
251 #[error("multi-factor authentication required")]
253 EMFAREQUIRED = -26,
254 #[error("access denied for users")]
256 EMASTERONLY = -27,
257 #[error("business account has expired")]
259 EBUSINESSPASTDUE = -28,
260 #[error("storage quota exceeded")]
262 EPAYWALL = -29,
263 #[serde(other)]
265 #[error("unknown error")]
266 UNKNOWN = 1,
267}