Skip to content

Commit 83dcfdd

Browse files
committed
reorder Http errors
Signed-off-by: Luminita Voicu <[email protected]>
1 parent a5fdf92 commit 83dcfdd

File tree

1 file changed

+69
-69
lines changed

1 file changed

+69
-69
lines changed

src/common/mod.rs

Lines changed: 69 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -17,48 +17,48 @@ pub mod ascii {
1717
///Errors associated with a header that is invalid.
1818
#[derive(Debug, PartialEq)]
1919
pub enum HttpHeaderError {
20+
/// The header is misformatted.
21+
InvalidFormat(String),
22+
/// The specified header contains illegal characters.
23+
InvalidUtf8String(Utf8Error),
24+
///The value specified is not valid.
25+
InvalidValue(String, String),
2026
/// The content length specified is longer than the limit imposed by Micro Http.
2127
SizeLimitExceeded(String),
28+
/// The requested feature is not currently supported.
29+
UnsupportedFeature(String, String),
2230
/// The header specified is not supported.
2331
UnsupportedName(String),
2432
/// The value for the specified header is not supported.
2533
UnsupportedValue(String, String),
26-
/// The specified header contains illegal characters.
27-
InvalidUtf8String(Utf8Error),
28-
/// The requested feature is not currently supported.
29-
UnsupportedFeature(String, String),
30-
/// The header is misformatted.
31-
InvalidFormat(String),
32-
///The value specified is not valid.
33-
InvalidValue(String, String),
3434
}
3535

3636
impl Display for HttpHeaderError {
3737
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
3838
match self {
39-
Self::SizeLimitExceeded(inner) => {
40-
write!(f, "Invalid content length. Header: {}", inner)
39+
Self::InvalidFormat(header_key) => {
40+
write!(f, "Header is incorrectly formatted. Key: {}", header_key)
4141
}
42-
Self::UnsupportedName(inner) => write!(f, "Unsupported header name. Key: {}", inner),
43-
Self::UnsupportedValue(header_key, header_value) => write!(
44-
f,
45-
"Unsupported value. Key:{}; Value:{}",
46-
header_key, header_value
47-
),
4842
Self::InvalidUtf8String(header_key) => {
4943
write!(f, "Header contains invalid characters. Key: {}", header_key)
5044
}
45+
Self::InvalidValue(header_name, value) => {
46+
write!(f, "Invalid value. Key:{}; Value:{}", header_name, value)
47+
}
48+
Self::SizeLimitExceeded(inner) => {
49+
write!(f, "Invalid content length. Header: {}", inner)
50+
}
5151
Self::UnsupportedFeature(header_key, header_value) => write!(
5252
f,
5353
"Unsupported feature. Key: {}; Value: {}",
5454
header_key, header_value
5555
),
56-
Self::InvalidFormat(header_key) => {
57-
write!(f, "Header is incorrectly formatted. Key: {}", header_key)
58-
}
59-
Self::InvalidValue(header_name, value) => {
60-
write!(f, "Invalid value. Key:{}; Value:{}", header_name, value)
61-
}
56+
Self::UnsupportedName(inner) => write!(f, "Unsupported header name. Key: {}", inner),
57+
Self::UnsupportedValue(header_key, header_value) => write!(
58+
f,
59+
"Unsupported value. Key:{}; Value:{}",
60+
header_key, header_value
61+
),
6262
}
6363
}
6464
}
@@ -68,18 +68,18 @@ impl Display for HttpHeaderError {
6868
pub enum RequestError {
6969
/// No request was pending while the request body was being parsed.
7070
BodyWithoutPendingRequest,
71+
/// Header specified is either invalid or not supported by this HTTP implementation.
72+
HeaderError(HttpHeaderError),
7173
/// No request was pending while the request headers were being parsed.
7274
HeadersWithoutPendingRequest,
7375
/// The HTTP Method is not supported or it is invalid.
7476
InvalidHttpMethod(&'static str),
75-
/// Request URI is invalid.
76-
InvalidUri(&'static str),
7777
/// The HTTP Version in the Request is not supported or it is invalid.
7878
InvalidHttpVersion(&'static str),
79-
/// Header specified is either invalid or not supported by this HTTP implementation.
80-
HeaderError(HttpHeaderError),
8179
/// The Request is invalid and cannot be served.
8280
InvalidRequest,
81+
/// Request URI is invalid.
82+
InvalidUri(&'static str),
8383
/// Overflow occurred when parsing a request.
8484
Overflow,
8585
/// Underflow occurred when parsing a request.
@@ -93,15 +93,15 @@ impl Display for RequestError {
9393
f,
9494
"No request was pending while the request body was being parsed."
9595
),
96+
Self::HeaderError(inner) => write!(f, "Invalid header. Reason: {}", inner),
9697
Self::HeadersWithoutPendingRequest => write!(
9798
f,
9899
"No request was pending while the request headers were being parsed."
99100
),
100101
Self::InvalidHttpMethod(inner) => write!(f, "Invalid HTTP Method: {}", inner),
101-
Self::InvalidUri(inner) => write!(f, "Invalid URI: {}", inner),
102102
Self::InvalidHttpVersion(inner) => write!(f, "Invalid HTTP Version: {}", inner),
103-
Self::HeaderError(inner) => write!(f, "Invalid header. Reason: {}", inner),
104103
Self::InvalidRequest => write!(f, "Invalid request."),
104+
Self::InvalidUri(inner) => write!(f, "Invalid URI: {}", inner),
105105
Self::Overflow => write!(f, "Overflow occurred when parsing a request."),
106106
Self::Underflow => write!(f, "Underflow occurred when parsing a request."),
107107
}
@@ -111,23 +111,23 @@ impl Display for RequestError {
111111
/// Errors associated with a HTTP Connection.
112112
#[derive(Debug)]
113113
pub enum ConnectionError {
114-
/// The request parsing has failed.
115-
ParseError(RequestError),
116-
/// Could not perform a stream operation successfully.
117-
StreamError(std::io::Error),
118114
/// Attempted to read or write on a closed connection.
119115
ConnectionClosed,
120116
/// Attempted to write on a stream when there was nothing to write.
121117
InvalidWrite,
118+
/// The request parsing has failed.
119+
ParseError(RequestError),
120+
/// Could not perform a stream operation successfully.
121+
StreamError(std::io::Error),
122122
}
123123

124124
impl Display for ConnectionError {
125125
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
126126
match self {
127-
Self::ParseError(inner) => write!(f, "Parsing error: {}", inner),
128-
Self::StreamError(inner) => write!(f, "Stream error: {}", inner),
129127
Self::ConnectionClosed => write!(f, "Connection closed."),
130128
Self::InvalidWrite => write!(f, "Invalid write attempt."),
129+
Self::ParseError(inner) => write!(f, "Parsing error: {}", inner),
130+
Self::StreamError(inner) => write!(f, "Stream error: {}", inner),
131131
}
132132
}
133133
}
@@ -151,25 +151,25 @@ impl Display for RouteError {
151151
/// Errors pertaining to `HttpServer`.
152152
#[derive(Debug)]
153153
pub enum ServerError {
154-
/// Epoll operations failed.
155-
IOError(std::io::Error),
156154
/// Error from one of the connections.
157155
ConnectionError(ConnectionError),
158-
/// Server maximum capacity has been reached.
159-
ServerFull,
156+
/// Epoll operations failed.
157+
IOError(std::io::Error),
160158
/// Overflow occured while processing messages.
161159
Overflow,
160+
/// Server maximum capacity has been reached.
161+
ServerFull,
162162
/// Underflow occured while processing mesagges.
163163
Underflow,
164164
}
165165

166166
impl Display for ServerError {
167167
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
168168
match self {
169-
Self::IOError(inner) => write!(f, "IO error: {}", inner),
170169
Self::ConnectionError(inner) => write!(f, "Connection error: {}", inner),
171-
Self::ServerFull => write!(f, "Server is full."),
170+
Self::IOError(inner) => write!(f, "IO error: {}", inner),
172171
Self::Overflow => write!(f, "Overflow occured while processing messages."),
172+
Self::ServerFull => write!(f, "Server is full."),
173173
Self::Underflow => write!(f, "Underflow occured while processing messages."),
174174
}
175175
}
@@ -419,36 +419,26 @@ mod tests {
419419
assert_eq!(
420420
format!(
421421
"{}",
422-
RequestError::HeaderError(HttpHeaderError::SizeLimitExceeded("test".to_string()))
423-
),
424-
"Invalid header. Reason: Invalid content length. Header: test"
425-
);
426-
assert_eq!(
427-
format!(
428-
"{}",
429-
RequestError::HeaderError(HttpHeaderError::UnsupportedName("test".to_string()))
422+
RequestError::HeaderError(HttpHeaderError::InvalidFormat("test".to_string()))
430423
),
431-
"Invalid header. Reason: Unsupported header name. Key: test"
424+
"Invalid header. Reason: Header is incorrectly formatted. Key: test"
432425
);
426+
let value = String::from_utf8(vec![0, 159]);
433427
assert_eq!(
434428
format!(
435429
"{}",
436-
RequestError::HeaderError(HttpHeaderError::UnsupportedValue(
437-
"test".to_string(),
438-
"test".to_string()
430+
RequestError::HeaderError(HttpHeaderError::InvalidUtf8String(
431+
value.unwrap_err().utf8_error()
439432
))
440433
),
441-
"Invalid header. Reason: Unsupported value. Key:test; Value:test"
434+
"Invalid header. Reason: Header contains invalid characters. Key: invalid utf-8 sequence of 1 bytes from index 1"
442435
);
443-
let value = String::from_utf8(vec![0, 159]);
444436
assert_eq!(
445437
format!(
446438
"{}",
447-
RequestError::HeaderError(HttpHeaderError::InvalidUtf8String(
448-
value.unwrap_err().utf8_error()
449-
))
439+
RequestError::HeaderError(HttpHeaderError::SizeLimitExceeded("test".to_string()))
450440
),
451-
"Invalid header. Reason: Header contains invalid characters. Key: invalid utf-8 sequence of 1 bytes from index 1"
441+
"Invalid header. Reason: Invalid content length. Header: test"
452442
);
453443
assert_eq!(
454444
format!(
@@ -463,36 +453,46 @@ mod tests {
463453
assert_eq!(
464454
format!(
465455
"{}",
466-
RequestError::HeaderError(HttpHeaderError::InvalidFormat("test".to_string()))
456+
RequestError::HeaderError(HttpHeaderError::UnsupportedName("test".to_string()))
467457
),
468-
"Invalid header. Reason: Header is incorrectly formatted. Key: test"
458+
"Invalid header. Reason: Unsupported header name. Key: test"
459+
);
460+
assert_eq!(
461+
format!(
462+
"{}",
463+
RequestError::HeaderError(HttpHeaderError::UnsupportedValue(
464+
"test".to_string(),
465+
"test".to_string()
466+
))
467+
),
468+
"Invalid header. Reason: Unsupported value. Key:test; Value:test"
469469
);
470470
}
471471

472472
#[test]
473473
fn test_display_connection_error() {
474+
assert_eq!(
475+
format!("{}", ConnectionError::ConnectionClosed),
476+
"Connection closed."
477+
);
474478
assert_eq!(
475479
format!(
476480
"{}",
477481
ConnectionError::ParseError(RequestError::InvalidRequest)
478482
),
479483
"Parsing error: Invalid request."
480484
);
485+
assert_eq!(
486+
format!("{}", ConnectionError::InvalidWrite),
487+
"Invalid write attempt."
488+
);
481489
assert_eq!(
482490
format!(
483491
"{}",
484492
ConnectionError::StreamError(std::io::Error::from_raw_os_error(11))
485493
),
486494
"Stream error: Resource temporarily unavailable (os error 11)"
487495
);
488-
assert_eq!(
489-
format!("{}", ConnectionError::ConnectionClosed),
490-
"Connection closed."
491-
);
492-
assert_eq!(
493-
format!("{}", ConnectionError::InvalidWrite),
494-
"Invalid write attempt."
495-
);
496496
}
497497

498498
#[test]
@@ -504,7 +504,6 @@ mod tests {
504504
),
505505
"Connection error: Connection closed."
506506
);
507-
assert_eq!(format!("{}", ServerError::ServerFull), "Server is full.");
508507
assert_eq!(
509508
format!(
510509
"{}",
@@ -516,6 +515,7 @@ mod tests {
516515
format!("{}", ServerError::Overflow),
517516
"Overflow occured while processing messages."
518517
);
518+
assert_eq!(format!("{}", ServerError::ServerFull), "Server is full.");
519519
assert_eq!(
520520
format!("{}", ServerError::Underflow),
521521
"Underflow occured while processing messages."

0 commit comments

Comments
 (0)