improve 404 catcher
This commit is contained in:
parent
b394c7e0f8
commit
05073604a9
40
src/main.rs
40
src/main.rs
@ -10,6 +10,7 @@ use rocket_dyn_templates::Template;
|
|||||||
use rand::distributions::Uniform;
|
use rand::distributions::Uniform;
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
|
|
||||||
|
use std::collections::HashMap;
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
@ -33,6 +34,19 @@ impl<T, E> ErrorEmpty for Result<T, E> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Responder)]
|
||||||
|
enum WebError {
|
||||||
|
#[response(status = 404)]
|
||||||
|
NotFound(Template),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl WebError {
|
||||||
|
fn not_found() -> Self {
|
||||||
|
let context: HashMap<String, String> = HashMap::new();
|
||||||
|
WebError::NotFound(Template::render("404", context))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
struct ArcTemplateData {
|
struct ArcTemplateData {
|
||||||
value: Arc<tera::Value>,
|
value: Arc<tera::Value>,
|
||||||
@ -85,9 +99,9 @@ async fn show_question_details(
|
|||||||
data: &AppState,
|
data: &AppState,
|
||||||
cache: &TemplateCache,
|
cache: &TemplateCache,
|
||||||
id: usize,
|
id: usize,
|
||||||
) -> Template {
|
) -> Option<Template> {
|
||||||
if let Some(value) = cache.get(&id) {
|
if let Some(value) = cache.get(&id) {
|
||||||
return value.render(template_name);
|
return Some(value.render(template_name));
|
||||||
}
|
}
|
||||||
|
|
||||||
match get_question(&data.db, id).await {
|
match get_question(&data.db, id).await {
|
||||||
@ -102,13 +116,9 @@ async fn show_question_details(
|
|||||||
let result = value.render(template_name);
|
let result = value.render(template_name);
|
||||||
cache.insert(id, value);
|
cache.insert(id, value);
|
||||||
|
|
||||||
result
|
Some(result)
|
||||||
}
|
|
||||||
Err(_) => {
|
|
||||||
use std::collections::HashMap;
|
|
||||||
let context: HashMap<String, String> = HashMap::new();
|
|
||||||
Template::render("404", context)
|
|
||||||
}
|
}
|
||||||
|
Err(_) => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -117,12 +127,16 @@ async fn show_question(
|
|||||||
data: &State<AppState>,
|
data: &State<AppState>,
|
||||||
cache: &State<TemplateCache>,
|
cache: &State<TemplateCache>,
|
||||||
id: usize,
|
id: usize,
|
||||||
) -> Template {
|
) -> Option<Template> {
|
||||||
show_question_details("question", data.inner(), cache.inner(), id).await
|
show_question_details("question", data.inner(), cache.inner(), id).await
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/q/<id>/a")]
|
#[get("/q/<id>/a")]
|
||||||
async fn show_answer(data: &State<AppState>, cache: &State<TemplateCache>, id: usize) -> Template {
|
async fn show_answer(
|
||||||
|
data: &State<AppState>,
|
||||||
|
cache: &State<TemplateCache>,
|
||||||
|
id: usize,
|
||||||
|
) -> Option<Template> {
|
||||||
show_question_details("answer", data.inner(), cache.inner(), id).await
|
show_question_details("answer", data.inner(), cache.inner(), id).await
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -143,10 +157,8 @@ fn index(data: &State<AppState>) -> Redirect {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[catch(404)]
|
#[catch(404)]
|
||||||
fn not_found(_req: &rocket::Request) -> Template {
|
fn not_found(_req: &rocket::Request) -> WebError {
|
||||||
use std::collections::HashMap;
|
WebError::not_found()
|
||||||
let context: HashMap<String, String> = HashMap::new();
|
|
||||||
Template::render("404", context)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[launch]
|
#[launch]
|
||||||
|
Loading…
Reference in New Issue
Block a user