1
0
Fork 0
mirror of https://git.42l.fr/neil/sncf.git synced 2024-04-26 11:30:28 +02:00
sncf/src/errors.rs
2020-08-19 01:21:42 +02:00

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,
}
}
}