index : static-web-server.git

ascending towards madness

author Jose Quintana <joseluisquintana20@gmail.com> 2021-07-01 13:25:58.0 +00:00:00
committer Jose Quintana <joseluisquintana20@gmail.com> 2021-07-01 13:25:58.0 +00:00:00
commit
55ffd06be14e74f086a244d25b2c4eca94ba35a5 [patch]
tree
3514fba5a83ea07b7c2765b7a7f6e42226393eca
parent
e07c3339f0b7a6f69a44a37d8f9cfb89900e36ba
download
55ffd06be14e74f086a244d25b2c4eca94ba35a5.tar.gz

refactor: handle potencial panic for 404/50x error page responses



Diff

 src/error_page.rs | 37 +++++++++++++++++++++++--------------
 1 file changed, 23 insertions(+), 14 deletions(-)

diff --git a/src/error_page.rs b/src/error_page.rs
index 2777229..88cbde9 100644
--- a/src/error_page.rs
+++ b/src/error_page.rs
@@ -7,10 +7,11 @@ use crate::error::Result;
pub static PAGE_404: OnceCell<String> = OnceCell::new();
pub static PAGE_50X: OnceCell<String> = OnceCell::new();

/// It returns a HTTP error response which also handles available `404` or `50x` HTML content.
pub fn get_error_response(method: &Method, status_code: &StatusCode) -> Result<Response<Body>> {
    tracing::warn!(method = ?method, status = status_code.as_u16(), error = ?status_code.to_string());
    tracing::warn!(method = ?method, status = status_code.as_u16(), error = ?status_code.to_owned());

    // Check for 4xx and 50x status codes
    // Check for 4xx and 50x status codes and handle their corresponding HTML content
    let mut error_page_content = String::new();
    let status_code = match status_code {
        // 4xx
@@ -32,13 +33,17 @@ pub fn get_error_response(method: &Method, status_code: &StatusCode) -> Result<R
        | &StatusCode::UNSUPPORTED_MEDIA_TYPE
        | &StatusCode::RANGE_NOT_SATISFIABLE
        | &StatusCode::EXPECTATION_FAILED => {
            // Extra check for 404 status code and HTML content
            // Extra check for 404 status code and its HTML content
            if status_code == &StatusCode::NOT_FOUND {
                // TODO: handle this potential panic properly
                error_page_content = PAGE_404
                    .get()
                    .expect("PAGE_404 contant is not initialized")
                    .to_string();
                error_page_content = match PAGE_404.get() {
                    Some(s) => s.to_owned(),
                    None => {
                        tracing::error!(
                            "404 error page content is not accessible or `PAGE_404` uninitialized"
                        );
                        String::new()
                    }
                };
            }
            status_code
        }
@@ -52,12 +57,16 @@ pub fn get_error_response(method: &Method, status_code: &StatusCode) -> Result<R
        | &StatusCode::VARIANT_ALSO_NEGOTIATES
        | &StatusCode::INSUFFICIENT_STORAGE
        | &StatusCode::LOOP_DETECTED => {
            // HTML content for error status codes 50x
            // TODO: handle this potential panic properly
            error_page_content = PAGE_50X
                .get()
                .expect("PAGE_50X contant is not initialized")
                .to_string();
            // HTML content for error status codes 50x and their HTML content
            error_page_content = match PAGE_50X.get() {
                Some(s) => s.to_owned(),
                None => {
                    tracing::error!(
                        "50x error page content is not accessible or `PAGE_50X` uninitialized"
                    );
                    String::new()
                }
            };
            status_code
        }
        // other status codes