Reorganize module structure
This commit is contained in:
parent
51047ad11c
commit
14dc84cc43
14 changed files with 19 additions and 23 deletions
58
src/router/handlers/error.rs
Normal file
58
src/router/handlers/error.rs
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
use axum::{
|
||||
body::Body,
|
||||
http::{Response, StatusCode, header},
|
||||
};
|
||||
|
||||
use crate::{syntax::serial::populate_graph, router::handlers};
|
||||
|
||||
pub(in crate::router::handlers) fn by_code(
|
||||
code: Option<u16>,
|
||||
message: Option<&str>,
|
||||
) -> Response<Body> {
|
||||
let out_code = code.unwrap_or(500);
|
||||
let out_message = &message.unwrap_or("Unknown error");
|
||||
|
||||
let body = make_body(Some(out_code), Some(out_message));
|
||||
|
||||
handlers::raw::make_response(
|
||||
&body,
|
||||
out_code,
|
||||
&[(header::CONTENT_TYPE, "text/html")],
|
||||
)
|
||||
}
|
||||
|
||||
fn make_body(code: Option<u16>, message: Option<&str>) -> String {
|
||||
let mut context = tera::Context::new();
|
||||
|
||||
let out_code = code.unwrap_or(500);
|
||||
let out_message = &message.unwrap_or("Unknown error");
|
||||
let config = populate_graph().meta.config;
|
||||
|
||||
context.insert(
|
||||
"title",
|
||||
&StatusCode::from_u16(out_code)
|
||||
.unwrap_or(StatusCode::INTERNAL_SERVER_ERROR)
|
||||
.to_string(),
|
||||
);
|
||||
|
||||
context.insert("message", out_message);
|
||||
context.insert("status_code", &out_code.to_string());
|
||||
context.insert("config", &config);
|
||||
|
||||
handlers::template::render(
|
||||
"error.html",
|
||||
&context,
|
||||
Some(&format!(
|
||||
"Failed to render template for Error {out_code}: {out_message}"
|
||||
))
|
||||
.cloned(),
|
||||
)
|
||||
.0
|
||||
}
|
||||
|
||||
pub async fn not_found() -> Response<Body> {
|
||||
by_code(
|
||||
Some(404),
|
||||
Some("The page you tried to access could not be found."),
|
||||
)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue