error: added error catchers, error responder to respond with template, basic error templates
This commit is contained in:
parent
27cc4c0956
commit
a85f29d3a8
8 changed files with 128 additions and 0 deletions
22
crates/ezidam/src/error/catchers.rs
Normal file
22
crates/ezidam/src/error/catchers.rs
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
use super::{Error, StatusCode};
|
||||||
|
use rocket::http::Status;
|
||||||
|
use rocket::{catch, catchers, Build, Request, Rocket};
|
||||||
|
|
||||||
|
#[catch(404)]
|
||||||
|
fn not_found(req: &Request) -> Error {
|
||||||
|
Error::new(StatusCode::NotFound, req.uri())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[catch(500)]
|
||||||
|
fn internal_server_error(req: &Request) -> Error {
|
||||||
|
Error::new(StatusCode::InternalServerError, req.uri())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[catch(default)]
|
||||||
|
fn default(status: Status, req: &Request) -> Error {
|
||||||
|
Error::new(StatusCode::Unknown(status), req.uri())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn register(rocket_builder: Rocket<Build>) -> Rocket<Build> {
|
||||||
|
rocket_builder.register("/", catchers![default, not_found, internal_server_error])
|
||||||
|
}
|
||||||
32
crates/ezidam/src/error/mod.rs
Normal file
32
crates/ezidam/src/error/mod.rs
Normal file
|
|
@ -0,0 +1,32 @@
|
||||||
|
pub(super) mod catchers;
|
||||||
|
mod conversion;
|
||||||
|
mod responder;
|
||||||
|
mod status_code;
|
||||||
|
|
||||||
|
use self::status_code::StatusCode;
|
||||||
|
use rocket_dyn_templates::{context, Template};
|
||||||
|
|
||||||
|
pub struct Error {
|
||||||
|
status: StatusCode,
|
||||||
|
template: Template,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Error {
|
||||||
|
fn new<M: std::fmt::Display>(status: StatusCode, message: M) -> Self {
|
||||||
|
Self {
|
||||||
|
status,
|
||||||
|
template: Template::render(
|
||||||
|
status.template_name(),
|
||||||
|
context! {
|
||||||
|
message: message.to_string(),
|
||||||
|
version: env!("CARGO_PKG_VERSION"),
|
||||||
|
http_code: status.http_code().code,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn internal_server_error<E: std::error::Error>(error: E) -> Self {
|
||||||
|
Self::new(StatusCode::InternalServerError, error)
|
||||||
|
}
|
||||||
|
}
|
||||||
15
crates/ezidam/src/error/responder.rs
Normal file
15
crates/ezidam/src/error/responder.rs
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
use rocket::{response, Request, Response};
|
||||||
|
use rocket::response::Responder;
|
||||||
|
use super::Error;
|
||||||
|
|
||||||
|
impl<'r, 'o: 'r> Responder<'r, 'o> for Error {
|
||||||
|
fn respond_to(self, req: &Request) -> response::Result<'o> {
|
||||||
|
// Response for template
|
||||||
|
let template_response = self.template.respond_to(req)?;
|
||||||
|
|
||||||
|
// Set status for final response
|
||||||
|
Response::build_from(template_response)
|
||||||
|
.status(self.status.http_code())
|
||||||
|
.ok()
|
||||||
|
}
|
||||||
|
}
|
||||||
26
crates/ezidam/src/error/status_code.rs
Normal file
26
crates/ezidam/src/error/status_code.rs
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
use rocket::http::Status;
|
||||||
|
|
||||||
|
#[derive(Copy, Clone)]
|
||||||
|
pub(super) enum StatusCode {
|
||||||
|
NotFound,
|
||||||
|
InternalServerError,
|
||||||
|
Unknown(Status),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl StatusCode {
|
||||||
|
pub fn http_code(self) -> Status {
|
||||||
|
match self {
|
||||||
|
StatusCode::NotFound => Status::NotFound,
|
||||||
|
StatusCode::InternalServerError => Status::InternalServerError,
|
||||||
|
StatusCode::Unknown(status) => status,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn template_name(self) -> &'static str {
|
||||||
|
match self {
|
||||||
|
StatusCode::NotFound => "errors/404",
|
||||||
|
StatusCode::InternalServerError => "errors/500",
|
||||||
|
StatusCode::Unknown(_) => "errors/unknown",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
14
crates/ezidam/templates/base.html.tera
Normal file
14
crates/ezidam/templates/base.html.tera
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
<html>
|
||||||
|
|
||||||
|
|
||||||
|
<body>
|
||||||
|
{% block content %}{% endblock content %}
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<div>ezidam version {{ version }}</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
|
||||||
|
|
||||||
|
</html>
|
||||||
6
crates/ezidam/templates/errors/404.html.tera
Normal file
6
crates/ezidam/templates/errors/404.html.tera
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
{% extends "base" %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
<div>404 not found error!</div>
|
||||||
|
<div>{{ message }}</div>
|
||||||
|
{% endblock content %}
|
||||||
6
crates/ezidam/templates/errors/500.html.tera
Normal file
6
crates/ezidam/templates/errors/500.html.tera
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
{% extends "base" %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
<div>500 internal error!</div>
|
||||||
|
<div>{{ message }}</div>
|
||||||
|
{% endblock content %}
|
||||||
7
crates/ezidam/templates/errors/unknown.html.tera
Normal file
7
crates/ezidam/templates/errors/unknown.html.tera
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
{% extends "base" %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
<div>unknown error!</div>
|
||||||
|
<div>http code {{ http_code }}</div>
|
||||||
|
<div>{{ message }}</div>
|
||||||
|
{% endblock content %}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue