add 500 catcher

This commit is contained in:
Dmitry Belyaev 2023-08-20 12:10:11 +03:00
parent 05073604a9
commit 28c01a7fc6
Signed by: b4tman
GPG Key ID: 41A00BF15EA7E5F3
2 changed files with 25 additions and 1 deletions

View File

@ -38,6 +38,8 @@ impl<T, E> ErrorEmpty for Result<T, E> {
enum WebError { enum WebError {
#[response(status = 404)] #[response(status = 404)]
NotFound(Template), NotFound(Template),
#[response(status = 500)]
ServerError(Template),
} }
impl WebError { impl WebError {
@ -45,6 +47,10 @@ impl WebError {
let context: HashMap<String, String> = HashMap::new(); let context: HashMap<String, String> = HashMap::new();
WebError::NotFound(Template::render("404", context)) WebError::NotFound(Template::render("404", context))
} }
fn server_error() -> Self {
let context: HashMap<String, String> = HashMap::new();
WebError::ServerError(Template::render("500", context))
}
} }
#[derive(Clone)] #[derive(Clone)]
@ -161,6 +167,11 @@ fn not_found(_req: &rocket::Request) -> WebError {
WebError::not_found() WebError::not_found()
} }
#[catch(500)]
fn server_error(_req: &rocket::Request) -> WebError {
WebError::server_error()
}
#[launch] #[launch]
async fn rocket() -> _ { async fn rocket() -> _ {
let state: AppState = async_db::Reader::new(DB_FILENAME) let state: AppState = async_db::Reader::new(DB_FILENAME)
@ -175,7 +186,7 @@ async fn rocket() -> _ {
rocket::build() rocket::build()
.manage(state) .manage(state)
.manage(cache) .manage(cache)
.register("/", catchers![not_found]) .register("/", catchers![not_found, server_error])
.mount( .mount(
"/", "/",
routes![index, show_question, show_answer, question0, answer0], routes![index, show_question, show_answer, question0, answer0],

13
templates/500.html.tera Normal file
View File

@ -0,0 +1,13 @@
{% extends "base" %}
{% block title %}404{% endblock title %}
{% block nav %}
{% endblock nav %}
{% block content %}
<div class="content-block">
<div class="content-block-inner mt-4 px-4 py-2 fs-1 top-50 start-50 translate-middle">
<h1 class="display-4 d-flex justify-content-center text-center fw-bold">500</h1>
<br/>
<p>&#128558; Ой, кажется у нас тут что то сломалось...</p>
</div>
</div>
{% endblock content %}