experimental generic error handler
This commit is contained in:
parent
b29f9d6934
commit
d9ac6c08a2
@ -4,6 +4,8 @@ use rocket::http::Status;
|
||||
use rocket::request::Request;
|
||||
|
||||
|
||||
// Generic Error responders
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct DbResponse {
|
||||
pub err_msg: &'static str
|
||||
@ -30,3 +32,36 @@ impl<'r> Responder<'r> for DbResponse {
|
||||
.ok()
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Generic error that we can both serialize and return as an API response
|
||||
*/
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ResponseErr {
|
||||
pub msg: &'static str,
|
||||
pub status: i16,
|
||||
pub err_type: &'static str,
|
||||
}
|
||||
|
||||
pub type ResponseResult<T, ResponseErr> = std::result::Result<T, ResponseErr>;
|
||||
|
||||
impl fmt::Display for ResponseErr {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, self.err_type)
|
||||
}
|
||||
}
|
||||
|
||||
impl error::Error for ResponseErr {
|
||||
fn source(&self) -> Option(&(dyn error::Error + 'static)> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl error::Error for ResponseErr {
|
||||
fn respond_to(self, _:&Request) -> response::Result<'r> {
|
||||
Response::build()
|
||||
.status(self.status)
|
||||
.raw_header(self.err_type, self.msg)
|
||||
.ok()
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user