mega/
error.rs

1use serde_repr::{Deserialize_repr, Serialize_repr};
2use thiserror::Error;
3
4/// The `Result` type for this library.
5pub type Result<T, E = Error> = std::result::Result<T, E>;
6
7/// The main error type for this library.
8#[non_exhaustive]
9#[derive(Debug, Error)]
10pub enum Error {
11    /// Missing user session.
12    #[error("missing user session (consider logging in first)")]
13    MissingUserSession,
14    /// Invalid URL format.
15    #[error("invalid URL format")]
16    InvalidUrlFormat,
17    /// The URL too short.
18    #[error("the URL too short")]
19    UrlTooShort,
20    /// Invalid algorithm version.
21    #[error("invalid algorithm version")]
22    InvalidAlgorithmVersion {
23        /// The encountered algorithm version.
24        version: u8,
25    },
26    /// Invalid session kind.
27    #[error("invalid session kind")]
28    InvalidSessionKind,
29    /// Invalid (or unsupported) public URL format.
30    #[error("invalid (or unsupported) public URL format")]
31    InvalidPublicUrlFormat,
32    /// Invalid node checksum format.
33    #[error("invalid node checksum format")]
34    InvalidChecksumFormat,
35    /// Invalid server response type.
36    #[error("invalid server response type")]
37    InvalidResponseType,
38    /// Invalid response format.
39    #[error("invalid response format")]
40    InvalidResponseFormat,
41    /// Invalid response format.
42    #[error("missing response field: `{field}`")]
43    MissingResponseField {
44        /// The name of the missing field.
45        field: &'static str,
46    },
47    /// Unknown user login version.
48    #[error("unknown user login version: `{version}`")]
49    UnknownUserLoginVersion {
50        /// The encountered login version.
51        version: i32,
52    },
53    /// Invalid RSA private key format.
54    #[error("invalid RSA private key format")]
55    InvalidRsaPrivateKeyFormat,
56    /// Failed condensed MAC verification.
57    #[error("condensed MAC mismatch")]
58    CondensedMacMismatch,
59    /// Failed to find node.
60    #[error("failed to find node")]
61    NodeNotFound,
62    /// Failed to find node attribute.
63    #[error("failed to find node attribute")]
64    NodeAttributeNotFound,
65    /// Could not get a meaningful response after maximum retries.
66    #[error("could not get a meaningful response after maximum retries")]
67    MaxRetriesReached,
68    /// The involved event cursors do not match, continuing would result in inconsistencies.
69    #[error("the involved event cursors do not match, continuing would result in inconsistencies")]
70    EventCursorMismatch,
71    /// UTF-8 validation error.
72    #[error("UTF-8 validation error: {source}")]
73    FromUtf8Error {
74        /// The source error.
75        #[from]
76        source: std::string::FromUtf8Error,
77    },
78    /// Integer parsing error.
79    #[error("integer parse error: {source}")]
80    ParseIntError {
81        /// The source error.
82        #[from]
83        source: std::num::ParseIntError,
84    },
85    /// Reqwest error.
86    #[cfg(feature = "reqwest")]
87    #[error("`reqwest` error: {source}")]
88    ReqwestError {
89        /// The source `reqwest` error.
90        #[from]
91        source: reqwest::Error,
92    },
93    /// URL parse error.
94    #[error("URL parse error: {source}")]
95    UrlError {
96        /// The source `url` error.
97        #[from]
98        source: url::ParseError,
99    },
100    /// JSON error.
101    #[error("JSON error: {source}")]
102    JsonError {
103        /// The source `serde_json` error.
104        #[from]
105        source: json::Error,
106    },
107    /// Base64 encode error.
108    #[error("base64 encode error: {source}")]
109    Base64EncodeError {
110        /// The source `base64` encode error.
111        #[from]
112        source: base64::EncodeSliceError,
113    },
114    /// Base64 decode error.
115    #[error("base64 encode error: {source}")]
116    Base64DecodeError {
117        /// The source `base64` decode error.
118        #[from]
119        source: base64::DecodeError,
120    },
121    /// HKDF error (invalid length).
122    #[error("HKDF error: {source}")]
123    HkdfInvalidLengthError {
124        /// The source `hkdf` invalid length error.
125        #[from]
126        source: hkdf::InvalidLength,
127    },
128    /// HKDF error (invalid PRK length).
129    #[error("HKDF error: {source}")]
130    HkdfInvalidPrkLengthError {
131        /// The source `hkdf` invalid PRK length error.
132        #[from]
133        source: hkdf::InvalidPrkLength,
134    },
135    /// HMAC verification error.
136    #[error("HMAC mismatch (invalid link or wrong password)")]
137    HmacMismatch {
138        /// The source `hmac` verification error.
139        #[from]
140        source: hmac::digest::MacError,
141    },
142    /// AES-GCM error.
143    #[error("AES-GCM error: {source}")]
144    AesGcmError {
145        /// The source `aes_gcm` error.
146        #[from]
147        source: aes_gcm::Error,
148    },
149    /// MEGA error (error codes from API).
150    #[error("MEGA error: {code}")]
151    MegaError {
152        /// The MEGA error code.
153        #[from]
154        code: ErrorCode,
155    },
156    /// I/O error.
157    #[error("IO error: {source}")]
158    IoError {
159        /// The source `std::io` error.
160        #[from]
161        source: std::io::Error,
162    },
163    /// Other errors.
164    #[error("other error: {source}")]
165    Other {
166        /// The source error.
167        #[from]
168        source: Box<dyn std::error::Error + Send + Sync>,
169    },
170}
171
172/// Error code originating from MEGA's API.
173#[repr(i8)]
174#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Error, Serialize_repr, Deserialize_repr)]
175pub enum ErrorCode {
176    /// Success
177    #[error("no error")]
178    OK = 0,
179    /// Internal Error
180    #[error("internal error")]
181    EINTERNAL = -1,
182    /// Invalid arguments
183    #[error("invalid argument")]
184    EARGS = -2,
185    /// Request failed (but may be retried)
186    #[error("request failed, retrying")]
187    EAGAIN = -3,
188    /// Rate-limited
189    #[error("rate limit exceeded")]
190    ERATELIMIT = -4,
191    /// Upload failed
192    #[error("failed permanently")]
193    EFAILED = -5,
194    /// Too many IPs are trying to access this resource
195    #[error("too many concurrent connections or transfers")]
196    ETOOMANY = -6,
197    /// The file packet is out of range
198    #[error("out of range")]
199    ERANGE = -7,
200    /// The upload target url has expired
201    #[error("expired")]
202    EEXPIRED = -8,
203    /// Object not found
204    #[error("not found")]
205    ENOENT = -9,
206    /// Attempted circular link
207    #[error("circular linkage detected")]
208    ECIRCULAR = -10,
209    /// Access violation (like writing to a read-only share)
210    #[error("access denied")]
211    EACCESS = -11,
212    /// Tried to create an object that already exists
213    #[error("already exists")]
214    EEXIST = -12,
215    /// Tried to access an incomplete resource
216    #[error("incomplete")]
217    EINCOMPLETE = -13,
218    /// A decryption operation failed
219    #[error("invalid key / decryption error")]
220    EKEY = -14,
221    /// Invalid or expired user session
222    #[error("bad session ID")]
223    ESID = -15,
224    /// User blocked
225    #[error("blocked")]
226    EBLOCKED = -16,
227    /// Request over quota
228    #[error("over quota")]
229    EOVERQUOTA = -17,
230    /// Resource temporarily unavailable
231    #[error("temporarily not available")]
232    ETEMPUNAVAIL = -18,
233    /// Too many connections to this resource
234    #[error("connection overflow")]
235    ETOOMANYCONNECTIONS = -19,
236    /// Write failed
237    #[error("write error")]
238    EWRITE = -20,
239    /// Read failed
240    #[error("read error")]
241    EREAD = -21,
242    /// Invalid App key
243    #[error("invalid application key")]
244    EAPPKEY = -22,
245    /// SSL verification failed
246    #[error("SSL verification failed")]
247    ESSL = -23,
248    /// Not enough quota
249    #[error("not enough quota")]
250    EGOINGOVERQUOTA = -24,
251    /// Need multifactor authentication
252    #[error("multi-factor authentication required")]
253    EMFAREQUIRED = -26,
254    /// Access denied for sub-users (buisness accounts only)
255    #[error("access denied for users")]
256    EMASTERONLY = -27,
257    /// Business account expired
258    #[error("business account has expired")]
259    EBUSINESSPASTDUE = -28,
260    /// Over Disk Quota Paywall
261    #[error("storage quota exceeded")]
262    EPAYWALL = -29,
263    /// Unknown error
264    #[serde(other)]
265    #[error("unknown error")]
266    UNKNOWN = 1,
267}