ferron/util/
error_pages.rs

1use ferron_common::format_page;
2
3use super::anti_xss;
4
5/// Generates a default error page
6pub fn generate_default_error_page(status_code: hyper::StatusCode, server_administrator_email: Option<&str>) -> String {
7  let status_code_name = match status_code.canonical_reason() {
8    Some(reason) => format!("{} {}", status_code.as_u16(), reason),
9    None => format!("{}", status_code.as_u16()),
10  };
11
12  let error_500 = format!("The server encountered an unexpected error. You may need to contact the server administrator{} to resolve the error.", match server_administrator_email {
13        Some(email_address) => format!(" at {email_address}"),
14        None => String::from("")
15    });
16  let status_code_description = match status_code.as_u16() {
17    200 => "The request was successful!",
18    201 => "A new resource was successfully created.",
19    202 => "The request was accepted but hasn't been fully processed yet.",
20    400 => "The request was invalid.",
21    401 => "Authentication is required to access the resource.",
22    402 => "Payment is required to access the resource.",
23    403 => "You're not authorized to access this resource.",
24    404 => "The requested resource wasn't found. Double-check the URL if entered manually.",
25    405 => "The request method is not allowed for this resource.",
26    406 => "The server cannot provide a response in an acceptable format.",
27    407 => "Proxy authentication is required.",
28    408 => "The request took too long and timed out.",
29    409 => "There's a conflict with the current state of the server.",
30    410 => "The requested resource has been permanently removed.",
31    411 => "The request must include a Content-Length header.",
32    412 => "The request doesn't meet the server's preconditions.",
33    413 => "The request is too large for the server to process.",
34    414 => "The requested URL is too long.",
35    415 => "The server doesn't support the request's media type.",
36    416 => "The requested content range is invalid or unavailable.",
37    417 => "The expectation in the Expect header couldn't be met.",
38    418 => "This server (a teapot) refuses to make coffee! ☕",
39    421 => "The request was directed to the wrong server.",
40    422 => "The server couldn't process the provided content.",
41    423 => "The requested resource is locked.",
42    424 => "The request failed due to a dependency on another failed request.",
43    425 => "The server refuses to process a request that might be replayed.",
44    426 => "The client must upgrade its protocol to proceed.",
45    428 => "A precondition is required for this request, but it wasn't included.",
46    429 => "Too many requests were sent in a short period.",
47    431 => "The request headers are too large.",
48    451 => "Access to this resource is restricted due to legal reasons.",
49    497 => "A non-TLS request was sent to an HTTPS server.",
50    500 => &error_500,
51    501 => "The server doesn't support the requested functionality.",
52    502 => "The server, acting as a gateway, received an invalid response.",
53    503 => "The server is temporarily unavailable (e.g., maintenance or overload). Try again later.",
54    504 => "The server, acting as a gateway, timed out waiting for a response.",
55    505 => "The HTTP version used in the request isn't supported.",
56    506 => "The Variant header caused a content negotiation loop.",
57    507 => "The server lacks sufficient storage to complete the request.",
58    508 => "The server detected an infinite loop while processing the request.",
59    509 => "Bandwidth limit exceeded on the server.",
60    510 => "The server requires an extended HTTP request, but the client didn't send one.",
61    511 => "Authentication is required to access the network.",
62    598 => "The proxy server didn't receive a response in time.",
63    599 => "The proxy server couldn't establish a connection in time.",
64    _ => "No description found for the status code.",
65  };
66
67  format_page!(
68    format!(
69      "<main class=\"error-container\">
70      <h1>
71          <span class=\"error-code\">{}</span>
72          {}
73      </h1>
74      <p class=\"error-description\">{}</p>
75  </main>",
76      status_code.as_u16(),
77      status_code.canonical_reason().map_or("".to_string(), |r| format!(
78        "<span class=\"error-message\">{}</span>",
79        anti_xss(r)
80      )),
81      status_code_description
82    ),
83    &status_code_name,
84    vec![
85      include_str!("../../../assets/common.css"),
86      include_str!("../../../assets/error.css")
87    ]
88  )
89}