Añade el error 404 integrado en el servidor web

This commit is contained in:
Manuel Cillero 2022-07-22 18:39:23 +02:00
parent 1543ab2960
commit 1363c3da1f
3 changed files with 51 additions and 34 deletions

View file

@ -1,6 +1,8 @@
use super::AppTrait; use super::{fatal_error::FatalError, AppTrait};
use crate::config::SETTINGS; use crate::config::SETTINGS;
use crate::core::{module, theme}; use crate::core::{module, theme};
use crate::html::Markup;
use crate::response::page::{Page, ResultPage};
use crate::{base, trace, Lazy}; use crate::{base, trace, Lazy};
use actix_web::dev::Server; use actix_web::dev::Server;
@ -57,6 +59,7 @@ impl Application {
.wrap(tracing_actix_web::TracingLogger::default()) .wrap(tracing_actix_web::TracingLogger::default())
.configure(&module::all::modules) .configure(&module::all::modules)
.configure(&theme::all::themes) .configure(&theme::all::themes)
.default_service(super::web::route().to(service_not_found))
}) })
.bind(format!( .bind(format!(
"{}:{}", "{}:{}",
@ -71,3 +74,23 @@ impl Application {
Ok(self.server) Ok(self.server)
} }
} }
async fn service_not_found() -> ResultPage<Markup, FatalError> {
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<Markup, FatalError> {
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()
}

View file

@ -34,35 +34,22 @@ impl ThemeTrait for Bootsier {
.alter_context(InContextOp::AddJQuery); .alter_context(InContextOp::AddJQuery);
} }
fn render_error_page(&self, mut s: app::http::StatusCode) -> ResultPage<Markup, FatalError> { fn error_404_not_found(&self) -> Container {
let mut description = "e500-description"; Container::new()
let mut message = "e500-description"; .with_component(
match s {
app::http::StatusCode::NOT_FOUND => {
description = "e404-description";
message = "e404-message";
}
_ => {
s = app::http::StatusCode::INTERNAL_SERVER_ERROR;
}
}
Page::new()
.with_title(format!("Error {}", s.as_str()).as_str())
.add_to(
"content",
Chunck::with(html! { Chunck::with(html! {
div class="jumbotron" { div class="jumbotron" {
div class="media" { div class="media" {
img img
src="/static/bootsier/images/caution.png" src="/bootsier/images/caution.png"
class="mr-4" class="mr-4"
style="width: 20%; max-width: 188px" style="width: 20%; max-width: 188px"
alt="Caution!"; alt="Caution!";
div class="media-body" { div class="media-body" {
h1 class="display-4" { (s.as_str()) } h1 class="display-4" { ("RESOURCE NOT FOUND") }
p class="lead" { (l(description)) } p class="lead" { (l("e404-description")) }
hr class="my-4"; hr class="my-4";
p { (l(message)) } p { (l("e404-description")) }
a a
class="btn btn-primary btn-lg" class="btn btn-primary btn-lg"
href="/" href="/"
@ -73,8 +60,7 @@ impl ThemeTrait for Bootsier {
} }
} }
} }
}), })
) )
.render()
} }
} }

View file

@ -1,10 +1,10 @@
use crate::base::component::Chunck; use crate::app;
use crate::base::component::{Chunck, Container};
use crate::config::SETTINGS; use crate::config::SETTINGS;
use crate::core::component::{ComponentTrait, InContext, InContextOp}; use crate::core::component::{ComponentTrait, InContext, InContextOp};
use crate::html::{html, Favicon, Markup}; use crate::html::{html, Favicon, Markup};
use crate::response::page::Page;
use crate::{concat_string, util}; use crate::{concat_string, util};
use crate::{app, app::fatal_error::FatalError};
use crate::response::page::{Page, ResultPage};
pub trait BaseTheme { pub trait BaseTheme {
fn single_name(&self) -> &'static str; fn single_name(&self) -> &'static str;
@ -117,18 +117,26 @@ pub trait ThemeTrait: BaseTheme + Send + Sync {
*/ */
} }
fn render_error_page(&self, s: app::http::StatusCode) -> ResultPage<Markup, FatalError> { fn error_404_not_found(&self) -> Container {
Page::new() Container::new()
.with_title(format!("Error {}", s.as_str()).as_str()) .with_component(
.add_to(
"content",
Chunck::with(html! { Chunck::with(html! {
div { div {
h1 { (s.as_str()) } h1 { ("RESOURCE NOT FOUND") }
} }
}), })
)
}
fn error_403_access_denied(&self) -> Container {
Container::new()
.with_component(
Chunck::with(html! {
div {
h1 { ("FORBIDDEN ACCESS") }
}
})
) )
.render()
} }
} }