1use crate::generate_responses_functions;
2use crate::responses::CustomResponse;
3use crate::traits::get_code_trait::GetCode;
4use strum_macros::EnumIter;
5
6generate_responses_functions! {
7 "Service responses",
8 ResponsesServiceCodes,
9 ReadingError => (500, "Internal Server Error", "An error occurred while reading the response or data from the server", 611, "Reading Error"),
10 ConnectionError => (500, "Internal Server Error", "A connection issue occurred, preventing successful communication with the server", 612, "Connection Error"),
11 ReadingTimeExpired => (500, "Internal Server Error", "The reading operation exceeded the allowed time limit, resulting in a timeout", 613, "Reading Time Expired"),
12 SSLHandshakeFailed => (500, "Internal Server Error", "The SSL handshake failed, potentially due to invalid certificates or incompatible protocols", 614, "SSL Handshake Failed"),
13 AnotherReadingError => (500, "Internal Server Error", "A generic error occurred while reading the response or data", 615, "Another Reading Error"),
14 FBAAnomaly => (500, "Internal Server Error", "An anomaly was detected in the Full Body Analyzer process, likely due to unexpected input", 616, "FBA Anomaly"),
15 CodingError => (500, "Internal Server Error", "An error in the implementation or logic caused the request to fail", 617, "Coding Error"),
16 RedirectWithoutRedirectURL => (500, "Internal Server Error", "The server issued a redirect response but did not provide a valid redirect URL", 618, "Redirect Without Redirect URL"),
17 DNSLookupFailed => (500, "Internal Server Error", "The DNS lookup for the specified domain failed, indicating a potential network or configuration issue", 680, "DNS Lookup Failed"),
18 SyntacticallyIncorrectURL => (500, "Internal Server Error", "The provided URL is syntactically incorrect and cannot be processed", 690, "Syntactically Incorrect URL"),
19 LostConnection => (500, "Internal Server Error", "The connection to the server was lost unexpectedly during communication", 691, "Lost Connection"),
20 WriteTimeout => (500, "Internal Server Error", "The operation timed out while attempting to write data to the server", 692, "Write Timeout"),
21 SelectionFailed => (500, "Internal Server Error", "The requested operation failed during a selection or matching process", 693, "Selection Failed"),
22 WriteError => (500, "Internal Server Error", "An error occurred while attempting to write data to the destination", 694, "Write Error"),
23 IncompleteBlockHeader => (500, "Internal Server Error", "A block header was incomplete or malformed, preventing further processing", 695, "Incomplete Block Header"),
24 UnexpectedError => (500, "Internal Server Error", "An unexpected error occurred, often indicative of an unforeseen issue or bug", 699, "Unexpected Error"),
25}
26
27#[cfg(test)]
28mod tests {
29 use crate::helpers::unified_tuple_helper::UnifiedTuple;
30 use crate::responses::ResponsesServiceCodes;
31 use crate::traits::tuple_traits::IntoTwoFieldsTuple;
32 use serde_json::json;
33 use serde_json::to_value;
34
35 #[test]
36 fn test_service_codes_get_code() {
37 assert_eq!(ResponsesServiceCodes::ReadingError.get_code(), 500);
38 assert_eq!(ResponsesServiceCodes::ConnectionError.get_code(), 500);
39 assert_eq!(ResponsesServiceCodes::ReadingTimeExpired.get_code(), 500);
40 assert_eq!(ResponsesServiceCodes::SSLHandshakeFailed.get_code(), 500);
41 }
42
43 #[test]
44 fn test_service_codes_from_u16() {
45 assert_eq!(ResponsesServiceCodes::from_u16(611), Some(ResponsesServiceCodes::ReadingError));
46 assert_eq!(
47 ResponsesServiceCodes::from_u16(613),
48 Some(ResponsesServiceCodes::ReadingTimeExpired)
49 );
50 assert_eq!(
51 ResponsesServiceCodes::from_u16(614),
52 Some(ResponsesServiceCodes::SSLHandshakeFailed)
53 );
54 assert_eq!(ResponsesServiceCodes::from_u16(9999), None);
55 }
56
57 #[test]
58 fn test_connection_error_codes_as_tuple() {
59 let code = ResponsesServiceCodes::ConnectionError;
60 let tuple = UnifiedTuple {
61 standard_code: 500,
62 standard_name: "Internal Server Error",
63 unified_description:
64 "A connection issue occurred, preventing successful communication with the server",
65 internal_code: Some(612),
66 internal_name: Option::from("Connection Error"),
67 };
68 let code_as_tuple = code.as_tuple();
69 assert_eq!(code_as_tuple, tuple);
70 }
71
72 #[test]
73 fn test_service_codes_as_json() {
74 let response_code = ResponsesServiceCodes::ReadingTimeExpired;
75 let json_result = response_code.as_json();
76 let expected_json = json!({
77 "type": "Service responses",
78 "details": {
79 "standard http code": {
80 "code": 500,
81 "name": "Internal Server Error"
82 },
83 "description": "The reading operation exceeded the allowed time limit, resulting in a timeout",
84 "internal http code": {
85 "code": 613,
86 "name": "Reading Time Expired"
87 }
88 }
89 });
90
91 assert_eq!(json_result, expected_json);
92 }
93
94 #[test]
95 fn test_service_codes_into_two_fields_tuple() {
96 let response_code = ResponsesServiceCodes::IncompleteBlockHeader;
97 let tuple = response_code.into_two_fields_tuple();
98 let json_result = to_value(&tuple).unwrap();
99
100 let expected_json = json!({
101 "code": 500,
102 "name": "Internal Server Error"
103 });
104
105 assert_eq!(json_result, expected_json);
106 }
107
108 #[test]
109 fn test_internal_server_error_duplicate_standard_codes() {
110 assert_eq!(
112 ResponsesServiceCodes::from_u16(695),
113 Some(ResponsesServiceCodes::IncompleteBlockHeader)
114 );
115 assert_eq!(
116 ResponsesServiceCodes::from_u16(699),
117 Some(ResponsesServiceCodes::UnexpectedError)
118 );
119 }
120}