mega/types/
error_code.rs

1/// An API Error
2#[derive(Debug, Eq, PartialEq, Hash, Copy, Clone, serde::Deserialize, serde::Serialize)]
3pub struct ErrorCode(i32);
4
5impl ErrorCode {
6    /// Success
7    pub const OK: Self = ErrorCode(0);
8
9    /// Internal Error
10    pub const EINTERNAL: Self = ErrorCode(-1);
11
12    /// Invalid arguments
13    pub const EARGS: Self = ErrorCode(-2);
14
15    /// Invalid arguments
16    pub const EAGAIN: Self = ErrorCode(-3);
17
18    /// Ratelimited
19    pub const ERATELIMIT: Self = ErrorCode(-4);
20
21    /// Upload failed
22    pub const EFAILED: Self = ErrorCode(-5);
23
24    /// Too many ips are trying to access this resource
25    pub const ETOOMANY: Self = ErrorCode(-6);
26
27    /// The file packet is out of range
28    pub const ERANGE: Self = ErrorCode(-7);
29
30    /// The upload target url has expired
31    pub const EEXPIRED: Self = ErrorCode(-8);
32
33    /// Object not found
34    pub const ENOENT: Self = ErrorCode(-9);
35
36    /// Attempted circular link
37    pub const ECIRCULAR: Self = ErrorCode(-10);
38
39    /// Access violation (like writing to a read-only share)
40    pub const EACCESS: Self = ErrorCode(-11);
41
42    /// Tried to create an object that already exists
43    pub const EEXIST: Self = ErrorCode(-12);
44
45    /// Tried to access an incomplete resource
46    pub const EINCOMPLETE: Self = ErrorCode(-13);
47
48    /// A decryption operation failed
49    pub const EKEY: Self = ErrorCode(-14);
50
51    /// Invalid or expired user session
52    pub const ESID: Self = ErrorCode(-15);
53
54    /// User blocked
55    pub const EBLOCKED: Self = ErrorCode(-16);
56
57    /// Request over quota
58    pub const EOVERQUOTA: Self = ErrorCode(-17);
59
60    /// Resource temporarily unavailable
61    pub const ETEMPUNAVAIL: Self = ErrorCode(-18);
62
63    /// Too many connections to this resource
64    pub const ETOOMANYCONNECTIONS: Self = ErrorCode(-19);
65
66    /// Write failed
67    pub const EWRITE: Self = ErrorCode(-20);
68
69    /// Read failed
70    pub const EREAD: Self = ErrorCode(-21);
71
72    /// Invalid App key
73    pub const EAPPKEY: Self = ErrorCode(-22);
74
75    /// SSL verification failed
76    pub const ESSL: Self = ErrorCode(-23);
77
78    /// Not enough quota
79    pub const EGOINGOVERQUOTA: Self = ErrorCode(-24);
80
81    /// Need multifactor authentication
82    pub const EMFAREQUIRED: Self = ErrorCode(-26);
83
84    /// Access denied for sub-users (buisness accounts only)
85    pub const EMASTERONLY: Self = ErrorCode(-27);
86
87    /// Business account expired
88    pub const EBUSINESSPASTDUE: Self = ErrorCode(-28);
89
90    /// Over Disk Quota Paywall
91    pub const EPAYWALL: Self = ErrorCode(-29);
92
93    /// Get a human-friendly description if the error
94    pub fn description(self) -> &'static str {
95        match self {
96            Self::OK => "No error",
97            Self::EINTERNAL => "Internal error",
98            Self::EARGS => "Invalid argument",
99            Self::EAGAIN => "Request failed, retrying",
100            Self::ERATELIMIT => "Rate limit exceeded",
101            Self::EFAILED => "Failed permanently",
102            Self::ETOOMANY => "Too many concurrent connections or transfers", // TODO: This can switch on a context variable
103            Self::ERANGE => "Out of range",
104            Self::EEXPIRED => "Expired",
105            Self::ENOENT => "Not found",
106            Self::ECIRCULAR => "Circular linkage detected", // TODO: This can switch on a context variable
107            Self::EACCESS => "Access denied",
108            Self::EEXIST => "Already exists",
109            Self::EINCOMPLETE => "Incomplete",
110            Self::EKEY => "Invalid key/Decryption error",
111            Self::ESID => "Bad session ID",
112            Self::EBLOCKED => "Blocked", // TODO: This can switch on a context variable
113            Self::EOVERQUOTA => "Over quota",
114            Self::ETEMPUNAVAIL => "Temporarily not available",
115            Self::ETOOMANYCONNECTIONS => "Connection overflow",
116            Self::EWRITE => "Write error",
117            Self::EREAD => "Read error",
118            Self::EAPPKEY => "Invalid application key",
119            Self::ESSL => "SSL verification failed",
120            Self::EGOINGOVERQUOTA => "Not enough quota",
121            Self::EMFAREQUIRED => "Multi-factor authentication required",
122            Self::EMASTERONLY => "Access denied for users",
123            Self::EBUSINESSPASTDUE => "Business account has expired",
124            Self::EPAYWALL => "Storage Quota Exceeded. Upgrade now",
125            _ => "Unknown error",
126        }
127    }
128}
129
130impl std::fmt::Display for ErrorCode {
131    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
132        write!(f, "{}", self.description())
133    }
134}
135
136impl std::error::Error for ErrorCode {}