From bd5ccdef8ae4ddf9f253a880473b23dbde69c66f Mon Sep 17 00:00:00 2001 From: Manuel Cillero Date: Sat, 23 Jul 2022 08:00:42 +0200 Subject: [PATCH] =?UTF-8?q?Corrige=20localizaci=C3=B3n=20errores=20NOT=5FF?= =?UTF-8?q?OUND=20y=20FORBIDDEN?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pagetop/src/app/application.rs | 20 ++------------------ pagetop/src/app/fatal_error.rs | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 19 deletions(-) diff --git a/pagetop/src/app/application.rs b/pagetop/src/app/application.rs index 31ce03d8..d1a56ce4 100644 --- a/pagetop/src/app/application.rs +++ b/pagetop/src/app/application.rs @@ -2,7 +2,7 @@ use super::{fatal_error::FatalError, AppTrait}; use crate::config::SETTINGS; use crate::core::{module, theme}; use crate::html::Markup; -use crate::response::page::{Page, ResultPage}; +use crate::response::page::ResultPage; use crate::{base, trace, Lazy}; use actix_web::dev::Server; @@ -76,21 +76,5 @@ impl Application { } async fn service_not_found() -> ResultPage { - let mut page = Page::new(); - let content_error = page.context().theme().error_404_not_found(); - page - .with_title("Error RESOURCE NOT FOUND") - .using_template("error") - .add_to("content", content_error) - .render() -} - -async fn _access_denied() -> ResultPage { - let mut page = Page::new(); - let content_error = page.context().theme().error_403_access_denied(); - page - .with_title("Error FORBIDDEN ACCESS") - .using_template("error") - .add_to("content", content_error) - .render() + Err(FatalError::NotFound) } diff --git a/pagetop/src/app/fatal_error.rs b/pagetop/src/app/fatal_error.rs index 079e3047..71f730ae 100644 --- a/pagetop/src/app/fatal_error.rs +++ b/pagetop/src/app/fatal_error.rs @@ -1,11 +1,13 @@ use crate::app::HttpResponse; use crate::app::http::{header::ContentType, StatusCode}; -use crate::response::ResponseError; +use crate::response::{page::Page, ResponseError}; use std::fmt; #[derive(Debug)] pub enum FatalError { + NotFound, + AccessDenied, InternalError, BadClientData, Timeout, @@ -14,6 +16,34 @@ pub enum FatalError { impl fmt::Display for FatalError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match *self { + FatalError::NotFound => { + let mut error_page = Page::new(); + let error_content = error_page.context().theme().error_404_not_found(); + if let Ok(page) = error_page + .with_title("Error RESOURCE NOT FOUND") + .using_template("error") + .add_to("content", error_content) + .render() + { + write!(f, "{}", page.into_string()) + } else { + write!(f, "Not Found") + } + }, + FatalError::AccessDenied => { + let mut error_page = Page::new(); + let error_content = error_page.context().theme().error_403_access_denied(); + if let Ok(page) = error_page + .with_title("Error FORBIDDEN") + .using_template("error") + .add_to("content", error_content) + .render() + { + write!(f, "{}", page.into_string()) + } else { + write!(f, "Access Denied") + } + }, FatalError::InternalError => write!(f, "Internal Error"), FatalError::BadClientData => write!(f, "Bad Client Data"), FatalError::Timeout => write!(f, "Timeout"), @@ -30,6 +60,8 @@ impl ResponseError for FatalError { fn status_code(&self) -> StatusCode { match *self { + FatalError::NotFound => StatusCode::NOT_FOUND, + FatalError::AccessDenied => StatusCode::FORBIDDEN, FatalError::InternalError => StatusCode::INTERNAL_SERVER_ERROR, FatalError::BadClientData => StatusCode::BAD_REQUEST, FatalError::Timeout => StatusCode::GATEWAY_TIMEOUT,