Make errors an enum w/ http status, where the string form is a server error.

Signed-off-by: Erik Hollensbe <linux@hollensbe.org>
This commit is contained in:
Erik Hollensbe
2022-01-19 16:11:09 -08:00
parent 46c4b11a68
commit 474a686f71
2 changed files with 13 additions and 6 deletions
+10 -3
View File
@@ -10,11 +10,14 @@ use std::{collections::BTreeMap, pin::Pin};
pub(crate) type PinBox<F> = Pin<Box<F>>;
#[derive(Clone, Debug)]
pub struct Error(String);
pub enum Error {
StatusCode(http::StatusCode),
InternalServerError(String),
}
impl Default for Error {
fn default() -> Self {
Self(String::from("internal server error"))
Self::InternalServerError("internal server error".to_string())
}
}
@@ -23,7 +26,11 @@ impl Error {
where
T: ToString,
{
Self(message.to_string())
Self::InternalServerError(message.to_string())
}
pub fn new_status(error: http::StatusCode) -> Self {
Self::StatusCode(error)
}
}
+3 -3
View File
@@ -48,7 +48,7 @@ impl Route {
let params = self.path.extract(provided)?;
if self.method != req.method() {
return Err(Error(http::StatusCode::NOT_FOUND.to_string()));
return Err(Error::StatusCode(http::StatusCode::NOT_FOUND));
}
self.handler.perform(req, None, params).await
@@ -78,14 +78,14 @@ impl Router {
let params = route.path.extract(path)?;
let (_, response) = route.handler.perform(req, None, params).await?;
if response.is_none() {
return Err(Error(http::StatusCode::INTERNAL_SERVER_ERROR.to_string()));
return Err(Error::StatusCode(http::StatusCode::INTERNAL_SERVER_ERROR));
}
return Ok(response.unwrap());
}
}
Err(Error(http::StatusCode::NOT_FOUND.to_string()))
Err(Error::StatusCode(http::StatusCode::NOT_FOUND))
}
}