zero4rs 2.0.0

zero4rs is a powerful, pragmatic, and extremely fast web framework for Rust
Documentation
use actix_web::{HttpResponse, ResponseError};

#[derive(thiserror::Error)]
pub enum ThrowError {
    ValidationError(String),
    #[error(transparent)]
    UnexpectedError(#[from] anyhow::Error),
}

impl ResponseError for ThrowError {
    fn error_response(&self) -> HttpResponse {
        log::error!("{:?}", self);
        HttpResponse::build(actix_web::http::StatusCode::INTERNAL_SERVER_ERROR)
            .content_type("text/html;charset=utf-8")
            .body(render_error(self))
    }
}

impl std::fmt::Debug for ThrowError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        crate::commons::error_chain_fmt(self, f)
    }
}

impl std::fmt::Display for ThrowError {
    fn fmt(&self, _: &mut std::fmt::Formatter) -> std::fmt::Result {
        // write!(f, "{}", self)
        // f.write_str(&self.to_string())?;
        Ok(())
    }
}

/// A generic method for rendering an error to present to the browser.
/// This should only be called in non-production settings.
pub(crate) fn render_error<E: std::fmt::Debug>(e: E) -> String
where
    E: std::fmt::Display,
{
    format!(
        r#"<!DOCTYPE html>
        <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, maximum-scale=1.0">
            <title>Jelly: An Error Occurred</title>
            <style>
                html, body {{
                    margin: 0;
                    padding: 0;
                    background: #F0DEE0;
                    color: #111;
                    font-family: -apple-system, "Helvetica Neue", Helvetica, "Segoe UI", Ubuntu, arial, sans-serif;
                }}
                
                h1 {{ margin: 0; background: #F05758; border-bottom: 1px solid #C7484A; padding: 20px; font-size: 30px; font-weight: 600; line-height: 40px; }}
                
                pre {{
                    display: block;
                    font-family: "Anonymous Pro", Consolas, Menlo, Monaco, Lucida Console, Liberation Mono, DejaVu Sans Mono, Bitstream Vera Sans Mono, Courier New, monospace, serif; 
                    font-size: 16px;
                    line-height: 20px;
                    padding: 20px;
                }}
            </style>
        </head>
        <body>
            <h1>Error: {}</h1>
            <pre>{:#?}<pre>
        </body>
        </html>
    "#,
        e, e
    )
}