Skip to content

Commit c44d46f

Browse files
MTP Error implementation
Implemented Error reponses for MTPResponse Added documentation for Error and ProtocolError
1 parent 4377581 commit c44d46f

File tree

1 file changed

+282
-0
lines changed

1 file changed

+282
-0
lines changed

net/src/protocol/error.rs

Lines changed: 282 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,282 @@
1+
/// Enum representing various errors in the Message Transfer Protocol (MTP).
2+
/// These errors are categorized into Client Errors (100-115) and Server Errors (120-128).
3+
/// These errors are also sent along with Response
4+
/// These errors indicate issues on the client side of the protocol.
5+
6+
/// - 100 - Bad Request: The request could not be understood or was missing required parameters.
7+
/// BadRequest100(Error),
8+
9+
/// - 101 - Unauthorized: Authentication is required and has failed or has not been provided.
10+
/// Unauthorized101(Error),
11+
12+
/// - 102 - Forbidden: The request is understood, but it has been refused or access is not allowed.
13+
/// Forbidden102(Error),
14+
15+
/// - 103 - Not Found: The requested resource could not be found.
16+
/// NotFound103(Error),
17+
18+
/// - 104 - Method Not Allowed: The method specified in the request is not allowed for the resource.
19+
/// MethodNotAllowed104(Error),
20+
21+
/// - 105 - Not Acceptable: The resource is capable of generating only content not acceptable according to the Accept headers sent in the request.
22+
/// NotAcceptable105(Error),
23+
24+
/// - 106 - Proxy Authentication Required: Authentication with a proxy is required.
25+
/// ProxyAuthenticationRequired106(Error),
26+
27+
/// - 107 - Request Timeout: The server timed out waiting for the request.
28+
/// RequestTimeout107(Error),
29+
30+
/// - 108 - Conflict: The request could not be processed because of conflict in the current state of the resource.
31+
/// Conflict108(Error),
32+
33+
/// - 109 - Gone: The requested resource is no longer available and will not be available again.
34+
/// Gone109(Error),
35+
36+
/// - 110 - Precondition Failed: The server does not meet one of the preconditions that the requester put on the request.
37+
/// PreconditionFailed110(Error),
38+
39+
/// - 111 - Payload Too Large: The request is larger than the server is willing or able to process.
40+
/// PayloadTooLarge111(Error),
41+
42+
/// - 112 - Unprocessable Content: The server understands the content type of the request entity, but was unable to process the contained instructions.
43+
/// UnprocessableContent112(Error),
44+
45+
/// - 113 - Locked: The resource is currently locked and cannot be accessed.
46+
/// Locked113(Error),
47+
48+
/// - 114 - Too Many Requests: The user has sent too many requests in a given amount of time.
49+
/// TooManyRequests114(Error),
50+
51+
/// - 115 - Request Header Too Large: The request headers are too large for the server to process.
52+
/// RequestHeaderTooLarge115(Error),
53+
54+
/// These errors indicate issues on the server side of the protocol.
55+
/// - 120 - Internal Server Error: An unexpected condition was encountered on the server.
56+
/// InternalServerError120(Error),
57+
58+
/// - 121 - Bad Gateway: The server received an invalid response from the upstream server.
59+
/// BadGateway121(Error),
60+
61+
/// - 123 - Service Unavailable: The server is currently unable to handle the request due to a temporary overload or maintenance.
62+
/// ServiceUnavailable123(Error),
63+
64+
/// - 124 - Gateway Timeout: The server did not receive a timely response from the upstream server or some other auxiliary server.
65+
/// GatewayTimeout124(Error),
66+
67+
/// - 125 - MTP Version Not Supported: The MTP version used in the request is not supported by the server.
68+
/// MTPVersionNotSupported125(Error),
69+
70+
/// - 126 - Insufficient Storage: The server is unable to store the representation needed to complete the request.
71+
/// InsufficientStorage126(Error),
72+
73+
/// - 127 - Loop Detected: The server detected an infinite loop while processing the request.
74+
/// LoopDetected127(Error),
75+
76+
/// - 128 - Network Authentication Required: The request requires network authentication.
77+
/// NetworkAuthenticationRequired128(Error),
78+
pub enum ProtocolError {
79+
80+
/// **Client Errors (100-115)**
81+
///
82+
/// These errors indicate issues on the client side of the protocol.
83+
84+
/// 100 - Bad Request: The request could not be understood or was missing required parameters.
85+
BadRequest100(Error),
86+
87+
/// 101 - Unauthorized: Authentication is required and has failed or has not been provided.
88+
Unauthorized101(Error),
89+
90+
/// 102 - Forbidden: The request is understood, but it has been refused or access is not allowed.
91+
Forbidden102(Error),
92+
93+
/// 103 - Not Found: The requested resource could not be found.
94+
NotFound103(Error),
95+
96+
/// 104 - Method Not Allowed: The method specified in the request is not allowed for the resource.
97+
MethodNotAllowed104(Error),
98+
99+
/// 105 - Not Acceptable: The resource is capable of generating only content not acceptable according to the Accept headers sent in the request.
100+
NotAcceptable105(Error),
101+
102+
/// 106 - Proxy Authentication Required: Authentication with a proxy is required.
103+
ProxyAuthenticationRequired106(Error),
104+
105+
/// 107 - Request Timeout: The server timed out waiting for the request.
106+
RequestTimeout107(Error),
107+
108+
/// 108 - Conflict: The request could not be processed because of conflict in the current state of the resource.
109+
Conflict108(Error),
110+
111+
/// 109 - Gone: The requested resource is no longer available and will not be available again.
112+
Gone109(Error),
113+
114+
/// 110 - Precondition Failed: The server does not meet one of the preconditions that the requester put on the request.
115+
PreconditionFailed110(Error),
116+
117+
/// 111 - Payload Too Large: The request is larger than the server is willing or able to process.
118+
PayloadTooLarge111(Error),
119+
120+
/// 112 - Unprocessable Content: The server understands the content type of the request entity, but was unable to process the contained instructions.
121+
UnprocessableContent112(Error),
122+
123+
/// 113 - Locked: The resource is currently locked and cannot be accessed.
124+
Locked113(Error),
125+
126+
/// 114 - Too Many Requests: The user has sent too many requests in a given amount of time.
127+
TooManyRequests114(Error),
128+
129+
/// 115 - Request Header Too Large: The request headers are too large for the server to process.
130+
RequestHeaderTooLarge115(Error),
131+
132+
133+
/// **Server Errors (120-128)**
134+
///
135+
/// These errors indicate issues on the server side of the protocol.
136+
137+
/// 120 - Internal Server Error: An unexpected condition was encountered on the server.
138+
InternalServerError120(Error),
139+
140+
/// 121 - Bad Gateway: The server received an invalid response from the upstream server.
141+
BadGateway121(Error),
142+
143+
/// 123 - Service Unavailable: The server is currently unable to handle the request due to a temporary overload or maintenance.
144+
ServiceUnavailable123(Error),
145+
146+
/// 124 - Gateway Timeout: The server did not receive a timely response from the upstream server or some other auxiliary server.
147+
GatewayTimeout124(Error),
148+
149+
/// 125 - MTP Version Not Supported: The MTP version used in the request is not supported by the server.
150+
MTPVersionNotSupported125(Error),
151+
152+
/// 126 - Insufficient Storage: The server is unable to store the representation needed to complete the request.
153+
InsufficientStorage126(Error),
154+
155+
/// 127 - Loop Detected: The server detected an infinite loop while processing the request.
156+
LoopDetected127(Error),
157+
158+
/// 128 - Network Authentication Required: The request requires network authentication.
159+
NetworkAuthenticationRequired128(Error),
160+
}
161+
162+
163+
/// Error type for a Protocol error
164+
/// Handles additional information about the error that occured and is sent to the client
165+
pub struct Error{
166+
info:String
167+
}
168+
169+
impl ProtocolError {
170+
/// Returns the numeric code associated with the error.
171+
///
172+
/// # Examples
173+
///
174+
/// ```
175+
/// let error = ProtocolError::BadRequest100(Error { message: "Invalid request".to_string() });
176+
/// assert_eq!(error.code(), 100);
177+
/// ```
178+
fn code(&self) -> u32 {
179+
match self {
180+
ProtocolError::BadRequest100(_) => 100,
181+
ProtocolError::Unauthorized101(_) => 101,
182+
ProtocolError::Forbidden102(_) => 102,
183+
ProtocolError::NotFound103(_) => 103,
184+
ProtocolError::MethodNotAllowed104(_) => 104,
185+
ProtocolError::NotAcceptable105(_) => 105,
186+
ProtocolError::ProxyAuthenticationRequired106(_) => 106,
187+
ProtocolError::RequestTimeout107(_) => 107,
188+
ProtocolError::Conflict108(_) => 108,
189+
ProtocolError::Gone109(_) => 109,
190+
ProtocolError::PreconditionFailed110(_) => 110,
191+
ProtocolError::PayloadTooLarge111(_) => 111,
192+
ProtocolError::UnprocessableContent112(_) => 112,
193+
ProtocolError::Locked113(_) => 113,
194+
ProtocolError::TooManyRequests114(_) => 114,
195+
ProtocolError::RequestHeaderTooLarge115(_) => 115,
196+
ProtocolError::InternalServerError120(_) => 120,
197+
ProtocolError::BadGateway121(_) => 121,
198+
ProtocolError::ServiceUnavailable123(_) => 123,
199+
ProtocolError::GatewayTimeout124(_) => 124,
200+
ProtocolError::MTPVersionNotSupported125(_) => 125,
201+
ProtocolError::InsufficientStorage126(_) => 126,
202+
ProtocolError::LoopDetected127(_) => 127,
203+
ProtocolError::NetworkAuthenticationRequired128(_) => 128,
204+
}
205+
}
206+
207+
/// Returns a description of the error.
208+
///
209+
/// # Examples
210+
///
211+
/// ```
212+
/// let error = ProtocolError::BadRequest100(Error { message: "Invalid request".to_string() });
213+
/// assert_eq!(error.description(), "100 - Bad Request: The request could not be understood or was missing required parameters.");
214+
/// ```
215+
fn description(&self) -> &'static str {
216+
match self {
217+
ProtocolError::BadRequest100(_) => "100 - Bad Request: The request could not be understood or was missing required parameters.",
218+
ProtocolError::Unauthorized101(_) => "101 - Unauthorized: Authentication is required and has failed or has not been provided.",
219+
ProtocolError::Forbidden102(_) => "102 - Forbidden: The request is understood, but it has been refused or access is not allowed.",
220+
ProtocolError::NotFound103(_) => "103 - Not Found: The requested resource could not be found.",
221+
ProtocolError::MethodNotAllowed104(_) => "104 - Method Not Allowed: The method specified in the request is not allowed for the resource.",
222+
ProtocolError::NotAcceptable105(_) => "105 - Not Acceptable: The resource is capable of generating only content not acceptable according to the Accept headers sent in the request.",
223+
ProtocolError::ProxyAuthenticationRequired106(_) => "106 - Proxy Authentication Required: Authentication with a proxy is required.",
224+
ProtocolError::RequestTimeout107(_) => "107 - Request Timeout: The server timed out waiting for the request.",
225+
ProtocolError::Conflict108(_) => "108 - Conflict: The request could not be processed because of conflict in the current state of the resource.",
226+
ProtocolError::Gone109(_) => "109 - Gone: The requested resource is no longer available and will not be available again.",
227+
ProtocolError::PreconditionFailed110(_) => "110 - Precondition Failed: The server does not meet one of the preconditions that the requester put on the request.",
228+
ProtocolError::PayloadTooLarge111(_) => "111 - Payload Too Large: The request is larger than the server is willing or able to process.",
229+
ProtocolError::UnprocessableContent112(_) => "112 - Unprocessable Content: The server understands the content type of the request entity, but was unable to process the contained instructions.",
230+
ProtocolError::Locked113(_) => "113 - Locked: The resource is currently locked and cannot be accessed.",
231+
ProtocolError::TooManyRequests114(_) => "114 - Too Many Requests: The user has sent too many requests in a given amount of time.",
232+
ProtocolError::RequestHeaderTooLarge115(_) => "115 - Request Header Too Large: The request headers are too large for the server to process.",
233+
ProtocolError::InternalServerError120(_) => "120 - Internal Server Error: An unexpected condition was encountered on the server.",
234+
ProtocolError::BadGateway121(_) => "121 - Bad Gateway: The server received an invalid response from the upstream server.",
235+
ProtocolError::ServiceUnavailable123(_) => "123 - Service Unavailable: The server is currently unable to handle the request due to a temporary overload or maintenance.",
236+
ProtocolError::GatewayTimeout124(_) => "124 - Gateway Timeout: The server did not receive a timely response from the upstream server or some other auxiliary server.",
237+
ProtocolError::MTPVersionNotSupported125(_) => "125 - MTP Version Not Supported: The MTP version used in the request is not supported by the server.",
238+
ProtocolError::InsufficientStorage126(_) => "126 - Insufficient Storage: The server is unable to store the representation needed to complete the request.",
239+
ProtocolError::LoopDetected127(_) => "127 - Loop Detected: The server detected an infinite loop while processing the request.",
240+
ProtocolError::NetworkAuthenticationRequired128(_) => "128 - Network Authentication Required: The request requires network authentication.",
241+
}
242+
}
243+
}
244+
245+
/// Clone implementation for [`ProtocolError`]
246+
impl Clone for ProtocolError{
247+
fn clone(&self) -> Self {
248+
match self {
249+
Self::BadRequest100(arg0) => Self::BadRequest100(arg0.clone()),
250+
Self::Unauthorized101(arg0) => Self::Unauthorized101(arg0.clone()),
251+
Self::Forbidden102(arg0) => Self::Forbidden102(arg0.clone()),
252+
Self::NotFound103(arg0) => Self::NotFound103(arg0.clone()),
253+
Self::MethodNotAllowed104(arg0) => Self::MethodNotAllowed104(arg0.clone()),
254+
Self::NotAcceptable105(arg0) => Self::NotAcceptable105(arg0.clone()),
255+
Self::ProxyAuthenticationRequired106(arg0) => Self::ProxyAuthenticationRequired106(arg0.clone()),
256+
Self::RequestTimeout107(arg0) => Self::RequestTimeout107(arg0.clone()),
257+
Self::Conflict108(arg0) => Self::Conflict108(arg0.clone()),
258+
Self::Gone109(arg0) => Self::Gone109(arg0.clone()),
259+
Self::PreconditionFailed110(arg0) => Self::PreconditionFailed110(arg0.clone()),
260+
Self::PayloadTooLarge111(arg0) => Self::PayloadTooLarge111(arg0.clone()),
261+
Self::UnprocessableContent112(arg0) => Self::UnprocessableContent112(arg0.clone()),
262+
Self::Locked113(arg0) => Self::Locked113(arg0.clone()),
263+
Self::TooManyRequests114(arg0) => Self::TooManyRequests114(arg0.clone()),
264+
Self::RequestHeaderTooLarge115(arg0) => Self::RequestHeaderTooLarge115(arg0.clone()),
265+
Self::InternalServerError120(arg0) => Self::InternalServerError120(arg0.clone()),
266+
Self::BadGateway121(arg0) => Self::BadGateway121(arg0.clone()),
267+
Self::ServiceUnavailable123(arg0) => Self::ServiceUnavailable123(arg0.clone()),
268+
Self::GatewayTimeout124(arg0) => Self::GatewayTimeout124(arg0.clone()),
269+
Self::MTPVersionNotSupported125(arg0) => Self::MTPVersionNotSupported125(arg0.clone()),
270+
Self::InsufficientStorage126(arg0) => Self::InsufficientStorage126(arg0.clone()),
271+
Self::LoopDetected127(arg0) => Self::LoopDetected127(arg0.clone()),
272+
Self::NetworkAuthenticationRequired128(arg0) => Self::NetworkAuthenticationRequired128(arg0.clone()),
273+
}
274+
}
275+
}
276+
277+
/// Clone implementation for [Error]
278+
impl Clone for Error{
279+
fn clone(&self) -> Self {
280+
Self { info: self.info.clone() }
281+
}
282+
}

0 commit comments

Comments
 (0)