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 "Server errors",
8 ResponsesServerCodes,
9 InternalServerError => (500, "Internal Server Error", "The server encountered an unexpected condition that prevented it from fulfilling the request. This could be due to a misconfiguration, an unhandled exception, or resource exhaustion", 500, "Internal Server Error"),
10 NotImplemented => (501, "Not Implemented", "The server does not support the functionality required to fulfill the request. This might be because the server does not recognize the request method or lacks the capability to process it", 501, "Not Implemented"),
11 BadGateway => (502, "Bad Gateway", "The server, while acting as a gateway or proxy, received an invalid response from an upstream server. This could be due to the upstream server being down or misconfigured", 502, "Bad Gateway"),
12 ServiceUnavailable => (503, "Service Unavailable", "The server is currently unable to handle the request due to temporary overloading or maintenance. This is usually a temporary state", 503, "Service Unavailable"),
13 GatewayTimeout => (504, "Gateway Timeout", "The server, while acting as a gateway or proxy, did not receive a timely response from the upstream server. This could be due to network congestion or the upstream server being overloaded", 504, "Gateway Timeout"),
14 HTTPVersionNotSupported => (505, "HTTP Version Not Supported", "The server does not support the HTTP protocol version used in the request. This prevents the server from processing the request", 505, "HTTP Version Not Supported"),
15 VariantAlsoNegotiates => (506, "Variant Also Negotiates", "The server encountered a configuration error in transparent content negotiation. This resulted in a circular reference that prevents the server from serving the requested content", 506, "Variant Also Negotiates"),
16 InsufficientStorage => (507, "Insufficient Storage", "The server is unable to store the representation needed to complete the request. This could be due to storage limits being reached or allocation constraints", 507, "Insufficient Storage"),
17 LoopDetected => (508, "Loop Detected", "The server detected an infinite loop while processing a request. This is often due to circular references or recursive function calls in WebDAV configurations, RFC 5842", 508, "Loop Detected"),
18 BandwidthLimitExceeded => (509, "Bandwidth Limit Exceeded", "The server's bandwidth limit has been exceeded. This limit is typically set by the administrator and prevents further data transfer until the limit resets, often used by hosting providers to prevent abuse, apache, unofficial, Cpanel", 509, "Bandwidth Limit Exceeded"),
19 NotExtended => (510, "Not Extended", "The server requires further extensions to fulfill the request. This could mean additional client conditions or protocol extensions are necessary before the server can process the request", 510, "Not Extended"),
20 NetworkAuthenticationRequired => (511, "Network Authentication Required", "The network connection requires authentication before accessing the requested resources. This is often used by captive portals to redirect users to a login page", 511, "Network Authentication Required"),
21 UnknownError => (500, "Internal Server Error", "An unspecified error occurred, and the server was unable to provide more details. This is a catch-all for unexpected conditions, (Cloudflare extension)", 520, "Unknown Error"),
22 WebServerIsDown => (502, "Bad Gateway", "Cloudflare, unofficial is currently unreachable, likely due to downtime or maintenance. This prevents the server from processing the request, and the client should try again later", 521, "Web Server Is Down"),
23 ConnectionTimedOut => (504, "Gateway Timeout", "The connection to the server timed out before a response could be received. This could be due to network issues or server overload", 522, "Connection Timed Out"),
24 OriginIsUnreachable => (502, "Bad Gateway", "The origin server could not be contacted. This might be due to network issues or misconfiguration", 523, "Origin Is Unreachable"),
25 TimeoutOccurred => (504, "Gateway Timeout", "The operation timed out while waiting for a response from the server. This could be due to network congestion or server overload", 524, "Timeout Occurred"),
26 SSLHandshakeFailed => (525, "SSL Handshake Failed", "The SSL/TLS handshake failed, preventing a secure connection from being established. This could be due to certificate issues or network problems", 525, "SSL Handshake Failed"),
27 InvalidSSLCertificate => (526, "Invalid SSL Certificate", "The SSL/TLS certificate provided by the server is invalid, expired, or does not match the requested domain. This prevents the secure connection from being established", 526, "Invalid SSL Certificate"),
28 RailgunError => (527, "Railgun Error", "An error occurred in the Railgun service, which accelerates connections between Cloudflare and the origin server. This may indicate a misconfiguration or temporary service unavailability", 527, "Railgun Error"),
29 SiteIsOverloaded => (529, "Site Is Overloaded", "Indicates the Qualys server cannot process the request, likely due to high traffic or resource constraints. This is a Qualys-specific status code, unofficial", 529, "Site Is Overloaded"),
30 SiteIsFrozen => (530, "Site Is Frozen", "Indicates the Pantheon server has been frozen due to inactivity, preventing further requests from being processed. This is a Pantheon-specific status code, unofficial", 530, "Site Is Frozen"),
31 OriginDNSError => (531, "Origin DNS Error", "The origin server encountered a DNS resolution error while attempting to process the request. This typically occurs when the domain name cannot be resolved to an IP address, possibly due to a misconfiguration or network issue", 531, "Origin DNS Error"),
32 NoSiteDetected => (500, "Internal Server Error", "This error is specific to certain hosting environments. For AWS, it indicates an HTTP Authentication failure, whereas for Pantheon, it means there is a problem with the site configuration, no site detected / AWS or Pantheon config error.", 561, "No Site Detected"),
33 NetworkReadTimeoutError => (598, "Network Read Timeout Error", "This unofficial status code indicates that the HTTP requests executed by the code failed because no local network was found or the HTTP connections to the local network returned read timeouts", 598, "Network Read Timeout Error"),
34 NetworkConnectTimeoutError => (599, "Network Connect Timeout Error", "This unofficial status code indicates that the HTTP requests executed by the code failed because no local network was found or the HTTP connections to the local network timed out", 599, "Network Connect Timeout Error"),
35}
36
37#[cfg(test)]
38mod tests {
39 use crate::helpers::unified_tuple_helper::UnifiedTuple;
40 use crate::responses::ResponsesServerCodes;
41 use crate::traits::tuple_traits::IntoTwoFieldsTuple;
42 use serde_json::json;
43 use serde_json::to_value;
44
45 #[test]
46 fn test_server_codes_get_code() {
47 assert_eq!(ResponsesServerCodes::InternalServerError.get_code(), 500);
48 assert_eq!(ResponsesServerCodes::NotImplemented.get_code(), 501);
49 assert_eq!(ResponsesServerCodes::BadGateway.get_code(), 502);
50 assert_eq!(ResponsesServerCodes::ServiceUnavailable.get_code(), 503);
51 }
52
53 #[test]
54 fn test_server_codes_from_u16() {
55 assert_eq!(ResponsesServerCodes::from_u16(501), Some(ResponsesServerCodes::NotImplemented));
56 assert_eq!(ResponsesServerCodes::from_u16(502), Some(ResponsesServerCodes::BadGateway));
57 assert_eq!(
58 ResponsesServerCodes::from_u16(503),
59 Some(ResponsesServerCodes::ServiceUnavailable)
60 );
61 assert_eq!(ResponsesServerCodes::from_u16(9999), None);
62 }
63
64 #[test]
65 fn test_no_site_detected_codes_as_tuple() {
66 let code = ResponsesServerCodes::NoSiteDetected;
67 let tuple = UnifiedTuple {
68 standard_code: 500,
69 standard_name: "Internal Server Error",
70 unified_description: "This error is specific to certain hosting environments. For AWS, it indicates an HTTP Authentication failure, whereas for Pantheon, it means there is a problem with the site configuration, no site detected / AWS or Pantheon config error.",
71 internal_code: Some(561),
72 internal_name: Option::from("No Site Detected"),
73 };
74 let code_as_tuple = code.as_tuple();
75 assert_eq!(code_as_tuple, tuple);
76 }
77
78 #[test]
79 fn test_web_server_is_down_codes_as_json() {
80 let response_code = ResponsesServerCodes::WebServerIsDown;
81 let json_result = response_code.as_json();
82 let expected_json = json!({
83 "type": "Server errors",
84 "details": {
85 "standard http code": {
86 "code": 502,
87 "name": "Bad Gateway"
88 },
89 "description": "Cloudflare, unofficial is currently unreachable, likely due to downtime or maintenance. This prevents the server from processing the request, and the client should try again later",
90 "internal http code": {
91 "code": 521,
92 "name": "Web Server Is Down"
93 }
94 }
95 });
96
97 assert_eq!(json_result, expected_json);
98 }
99
100 #[test]
101 fn test_server_codes_into_two_fields_tuple() {
102 let response_code = ResponsesServerCodes::ServiceUnavailable;
103 let tuple = response_code.into_two_fields_tuple();
104 let json_result = to_value(&tuple).unwrap();
105
106 let expected_json = json!({
107 "code": 503,
108 "name": "Service Unavailable"
109 });
110
111 assert_eq!(json_result, expected_json);
112 }
113
114 #[test]
115 fn test_bad_gateway_duplicate_standard_codes() {
116 assert_eq!(
118 ResponsesServerCodes::from_u16(521),
119 Some(ResponsesServerCodes::WebServerIsDown)
120 );
121 assert_eq!(
122 ResponsesServerCodes::from_u16(523),
123 Some(ResponsesServerCodes::OriginIsUnreachable)
124 );
125 }
126}