diff --git a/src/errors.rs b/src/errors.rs index df9ec9c9..0bbb6a61 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -26,6 +26,7 @@ use actix_web::{ use argon2_creds::errors::CredsError; //use awc::error::SendRequestError; use derive_more::{Display, Error}; +use lazy_static::lazy_static; use m_captcha::errors::CaptchaError; use serde::{Deserialize, Serialize}; use url::ParseError; @@ -189,3 +190,84 @@ impl From for ServiceError { #[cfg(not(tarpaulin_include))] pub type ServiceResult = std::result::Result; + +#[derive(Debug, Display, Clone, PartialEq, Error)] +#[cfg(not(tarpaulin_include))] +pub enum PageError { + #[display(fmt = "Something weng wrong: Internal server error")] + InternalServerError, +} + +use sailfish::TemplateOnce; + +#[derive(Clone, TemplateOnce)] +#[template(path = "errors/internal-server-error.html")] +struct ErrorPage<'a> { + title: &'a str, + message: &'a str, +} + +const PAGE: &str = "Error"; + +impl<'a> ErrorPage<'a> { + fn new(title: &'a str, message: &'a str) -> Self { + ErrorPage { title, message } + } +} + +lazy_static! { + static ref INTERNAL_SERVER_ERROR: String = ErrorPage::new( + "Internal Server Error", + &format!("{}", PageError::InternalServerError) + ) + .render_once() + .unwrap(); + static ref UNKNOWN_ERROR: String = ErrorPage::new( + "Server Error", + &format!("{}", PageError::InternalServerError) + ) + .render_once() + .unwrap(); +} + +#[cfg(not(tarpaulin_include))] +impl From for PageError { + #[cfg(not(tarpaulin_include))] + fn from(_: sqlx::Error) -> Self { + PageError::InternalServerError + } +} + +impl ResponseError for PageError { + #[cfg(not(tarpaulin_include))] + fn error_response(&self) -> HttpResponse { + let body = match self.status_code() { + StatusCode::INTERNAL_SERVER_ERROR => &*INTERNAL_SERVER_ERROR, + _ => &*UNKNOWN_ERROR, + }; + HttpResponseBuilder::new(self.status_code()) + .content_type("text/html; charset=utf-8") + .body(body) + } + + #[cfg(not(tarpaulin_include))] + fn status_code(&self) -> StatusCode { + match self { + PageError::InternalServerError => StatusCode::INTERNAL_SERVER_ERROR, + } + } +} + +#[cfg(not(tarpaulin_include))] +pub type PageResult = std::result::Result; + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn error_works() { + let resp: HttpResponse = PageError::InternalServerError.error_response(); + assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR); + } +} diff --git a/src/pages/panel/sitekey/list.rs b/src/pages/panel/sitekey/list.rs index dfa710fd..fe263353 100644 --- a/src/pages/panel/sitekey/list.rs +++ b/src/pages/panel/sitekey/list.rs @@ -15,9 +15,14 @@ * along with this program. If not, see . */ -use actix_web::{HttpResponse, Responder}; +use actix_identity::Identity; +use actix_web::{web, HttpResponse, Responder}; use sailfish::TemplateOnce; +//use crate::api::v1::mcaptcha::mcaptcha::MCaptchaDetails; +use crate::errors::*; +use crate::Data; + #[derive(TemplateOnce, Clone)] #[template(path = "panel/site-keys/index.html")] pub struct IndexPage; @@ -30,9 +35,9 @@ impl Default for IndexPage { } } -pub async fn list_sitekeys() -> impl Responder { +pub async fn list_sitekeys(data: web::Data, id: Identity) -> PageResult { let body = IndexPage::default().render_once().unwrap(); - HttpResponse::Ok() + Ok(HttpResponse::Ok() .content_type("text/html; charset=utf-8") - .body(body) + .body(body)) } diff --git a/templates/errors/internal-server-error.html b/templates/errors/internal-server-error.html new file mode 100644 index 00000000..59c8752e --- /dev/null +++ b/templates/errors/internal-server-error.html @@ -0,0 +1,12 @@ +<. include!("../components/headers.html"); .> +
+
+
+

<.= title .>

+

<.= message .>

+
+
+ +
+ +<. include!("../components/footers.html"); .> diff --git a/templates/errors/main.scss b/templates/errors/main.scss new file mode 100644 index 00000000..d37a5228 --- /dev/null +++ b/templates/errors/main.scss @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2021 Aravinth Manivannan + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +@import '../reset'; +@import '../vars'; + +.error-box { + display: flex; + flex-direction: column; + width: 90%; + justify-content: center; + align-items: center; + box-sizing: content-box; + background-color: $white; + margin: auto; + padding-bottom: 30px; +} + +.error-title { + padding-left: 10px; + font-size: 1rem; + padding: 0.75rem 1.25rem; + box-sizing: border-box; + text-align: left; + width: 100%; + border-bottom: 0.1px solid $light-grey; +} + +.error-message { + text-align: center; +}