mirror of
https://git.42l.fr/neil/sncf.git
synced 2024-06-17 06:55:05 +02:00
46 lines
1.2 KiB
Rust
46 lines
1.2 KiB
Rust
use crate::templates::TplError;
|
|
|
|
use std::fmt;
|
|
use actix_web::dev::HttpResponseBuilder;
|
|
use actix_web::{error, http::header, http::StatusCode, HttpResponse};
|
|
use askama::Template;
|
|
|
|
pub fn crash(lang: String, error_msg: &'static str) -> TrainCrash {
|
|
TrainCrash { lang, error_msg }
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct TrainCrash {
|
|
pub error_msg: &'static str,
|
|
pub lang: String,
|
|
}
|
|
|
|
// gonna avoid using failure crate
|
|
// by implementing display
|
|
impl fmt::Display for TrainCrash {
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
write!(f, "{:?}", self.error_msg)
|
|
}
|
|
}
|
|
|
|
impl error::ResponseError for TrainCrash {
|
|
fn error_response(&self) -> HttpResponse {
|
|
eprintln!("Error reached: {}", self.error_msg);
|
|
HttpResponseBuilder::new(self.status_code())
|
|
.set_header(header::CONTENT_TYPE, "text/html; charset=utf-8")
|
|
.body(
|
|
TplError {
|
|
lang: &self.lang,
|
|
error_msg: self.error_msg,
|
|
}
|
|
.render()
|
|
.expect("error_tplrender (TplError). Empty page sent to client."))
|
|
|
|
}
|
|
fn status_code(&self) -> StatusCode {
|
|
match *self {
|
|
_ => StatusCode::INTERNAL_SERVER_ERROR,
|
|
}
|
|
}
|
|
}
|