error page

This commit is contained in:
realaravinth 2021-05-04 16:19:24 +05:30
parent fe02c43c2c
commit 266b8dea88
No known key found for this signature in database
GPG key ID: AD9F0F08E855ED88
4 changed files with 148 additions and 4 deletions

View file

@ -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<sqlx::Error> for ServiceError {
#[cfg(not(tarpaulin_include))]
pub type ServiceResult<V> = std::result::Result<V, ServiceError>;
#[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<sqlx::Error> 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<V> = std::result::Result<V, PageError>;
#[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);
}
}

View file

@ -15,9 +15,14 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
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<Data>, id: Identity) -> PageResult<impl Responder> {
let body = IndexPage::default().render_once().unwrap();
HttpResponse::Ok()
Ok(HttpResponse::Ok()
.content_type("text/html; charset=utf-8")
.body(body)
.body(body))
}

View file

@ -0,0 +1,12 @@
<. include!("../components/headers.html"); .>
<main>
<div class="inner-container">
<div class="error-box">
<h1 class="error-title"><.= title .></h1>
<p class="error-message"><.= message .></p>
</div>
</div>
<!-- end of container -->
</main>
<. include!("../components/footers.html"); .>

View file

@ -0,0 +1,45 @@
/*
* Copyright (C) 2021 Aravinth Manivannan <realaravinth@batsense.net>
*
* 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 <https://www.gnu.org/licenses/>.
*/
@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;
}