Merge pull request 'Add 404 and 500 page' (#12) from add-error-page into main
Reviewed-on: #12
This commit is contained in:
commit
81a9e11774
11
src/lib.rs
11
src/lib.rs
@ -1,3 +1,5 @@
|
|||||||
|
use askama::Template;
|
||||||
|
use askama_web::WebTemplate;
|
||||||
use axum::{
|
use axum::{
|
||||||
Router,
|
Router,
|
||||||
routing::{get, post},
|
routing::{get, post},
|
||||||
@ -29,5 +31,14 @@ pub fn build_app(state: AppState) -> Router {
|
|||||||
.route("/users", post(routes::user::create))
|
.route("/users", post(routes::user::create))
|
||||||
.route("/users/{id}/delete", post(routes::user::delete))
|
.route("/users/{id}/delete", post(routes::user::delete))
|
||||||
.nest("/assets", static_router())
|
.nest("/assets", static_router())
|
||||||
|
.fallback(error_handler)
|
||||||
.with_state(state)
|
.with_state(state)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Template, WebTemplate)]
|
||||||
|
#[template(path = "404.html")]
|
||||||
|
struct NotFoundTemplate {}
|
||||||
|
|
||||||
|
pub async fn error_handler() -> impl axum::response::IntoResponse {
|
||||||
|
NotFoundTemplate {}
|
||||||
|
}
|
||||||
|
|||||||
@ -1,17 +1,17 @@
|
|||||||
use askama::Template;
|
use askama::Template;
|
||||||
use askama_web::WebTemplate;
|
use askama_web::WebTemplate;
|
||||||
use axum::response::{IntoResponse, Response};
|
use axum::response::{IntoResponse, Response};
|
||||||
|
use log::error;
|
||||||
use snafu::prelude::*;
|
use snafu::prelude::*;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
models::{book::BookError, user::UserError},
|
models::{
|
||||||
|
book::{BookError, NotFoundSnafu},
|
||||||
|
user::UserError,
|
||||||
|
},
|
||||||
state::config::ConfigError,
|
state::config::ConfigError,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Template, WebTemplate)]
|
|
||||||
#[template(path = "error.html")]
|
|
||||||
struct ErrorTemplate {}
|
|
||||||
|
|
||||||
#[derive(Snafu, Debug)]
|
#[derive(Snafu, Debug)]
|
||||||
#[snafu(visibility(pub))]
|
#[snafu(visibility(pub))]
|
||||||
pub enum AppStateError {
|
pub enum AppStateError {
|
||||||
@ -38,8 +38,30 @@ pub enum AppStateError {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IntoResponse for AppStateError {
|
#[derive(Template, WebTemplate)]
|
||||||
fn into_response(self) -> Response {
|
#[template(path = "error.html")]
|
||||||
ErrorTemplate {}.into_response()
|
struct ErrorTemplate {
|
||||||
|
state: AppStateErrorContext,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct AppStateErrorContext {
|
||||||
|
pub errors: Vec<AppStateError>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<AppStateError> for AppStateErrorContext {
|
||||||
|
fn from(e: AppStateError) -> Self {
|
||||||
|
error!("{:?}", e);
|
||||||
|
|
||||||
|
Self { errors: vec![e] }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl IntoResponse for AppStateError {
|
||||||
|
fn into_response(self) -> Response {
|
||||||
|
let error_context = AppStateErrorContext::from(self);
|
||||||
|
ErrorTemplate {
|
||||||
|
state: error_context,
|
||||||
|
}
|
||||||
|
.into_response()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
8
templates/404.html
Normal file
8
templates/404.html
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
{% extends "base.html" %}
|
||||||
|
{% block main %}
|
||||||
|
<div class="mt-4 text-center">
|
||||||
|
<h1>Oops ! This page does not exist</h1>
|
||||||
|
<h2 class="fst-italic">404 NOT FOUND</h2>
|
||||||
|
<a href="/" class="mt-3 btn btn-info">Back to home</a>
|
||||||
|
</div>
|
||||||
|
{% endblock %}
|
||||||
@ -1 +1,12 @@
|
|||||||
Error
|
{% extends "base.html" %}
|
||||||
|
{% block main %}
|
||||||
|
<div class="mt-4 text-center">
|
||||||
|
<h1>Oops ! An Error occured</h1>
|
||||||
|
<div class="alert alert-danger">
|
||||||
|
{% for error in state.errors %}
|
||||||
|
<p>{{ error }}</p>
|
||||||
|
{% endfor %}
|
||||||
|
</div>
|
||||||
|
<a href="/" class="mt-3 btn btn-info">Back to home</a>
|
||||||
|
</div>
|
||||||
|
{% endblock %}
|
||||||
|
|||||||
@ -14,7 +14,7 @@ o{% import "components/inputs.html" as form_helpers %}
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="col-md-2">
|
<div class="col-md-2">
|
||||||
<input type="submit" value="Create user" class="btn btn-success">
|
<input type="submit" value="Edit user" class="btn btn-success">
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user