add 500 catcher

This commit is contained in:
2023-08-20 12:10:11 +03:00
parent 05073604a9
commit 28c01a7fc6
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 {
#[response(status = 404)]
NotFound(Template),
#[response(status = 500)]
ServerError(Template),
}
impl WebError {
@@ -45,6 +47,10 @@ impl WebError {
let context: HashMap<String, String> = HashMap::new();
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)]
@@ -161,6 +167,11 @@ fn not_found(_req: &rocket::Request) -> WebError {
WebError::not_found()
}
#[catch(500)]
fn server_error(_req: &rocket::Request) -> WebError {
WebError::server_error()
}
#[launch]
async fn rocket() -> _ {
let state: AppState = async_db::Reader::new(DB_FILENAME)
@@ -175,7 +186,7 @@ async fn rocket() -> _ {
rocket::build()
.manage(state)
.manage(cache)
.register("/", catchers![not_found])
.register("/", catchers![not_found, server_error])
.mount(
"/",
routes![index, show_question, show_answer, question0, answer0],