mirror of
https://github.com/zerotier/ratpack.git
synced 2026-05-22 16:27:23 -07:00
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:
+10
-3
@@ -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
@@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user