freechat/server/src/err.rs
2020-03-31 17:04:01 -07:00

33 lines
781 B
Rust

use std::{error, fmt};
use rocket::response::{self, Responder, Response};
use rocket::http::Status;
use rocket::request::Request;
#[derive(Debug, Clone)]
pub struct DbResponse {
err_msg: &'static str
}
pub type DbError<T, DbErr> = std::result::Result<T, DbErr>;
impl fmt::Display for DbResponse {
fn fmt(&self, f:&mut fmt::Formatter) -> fmt::Result {
write!(f, "Database error")
}
}
impl error::Error for DbResponse {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
None
}
}
impl<'r> Responder<'r> for DbResponse {
fn respond_to(self, _:&Request) -> response::Result<'r> {
Response::build()
.status(Status::InternalServerError)
.raw_header("db-error", self.err_msg)
.ok()
}
}