aboutsummaryrefslogtreecommitdiff
path: root/src/error.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/error.rs')
-rw-r--r--src/error.rs43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/error.rs b/src/error.rs
index 0a36476..3cf6832 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -1,7 +1,12 @@
+use hyper::http::response::Builder;
+use hyper::StatusCode;
use sputnik::hyper_body::FormError;
use sputnik::request::QueryError;
+use sputnik::response::EmptyBuilder;
use std::str::Utf8Error;
+use crate::HyperResponse;
+
/// For convenience this enum also contains nonerroneous variants.
pub enum Error {
/// A 400 bad request error.
@@ -48,3 +53,41 @@ impl From<QueryError> for Error {
Self::BadRequest(e.to_string())
}
}
+
+impl From<Error> for HyperResponse {
+ fn from(err: Error) -> Self {
+ let (status, message) = match err {
+ Error::BadRequest(msg) => (400, msg),
+ Error::Unauthorized(msg) => (401, msg),
+ Error::Forbidden(msg) => (403, msg),
+ Error::NotFound(msg) => (404, msg),
+ Error::Internal(msg) => (500, msg),
+ Error::NotModified => {
+ return Builder::new()
+ .status(StatusCode::NOT_MODIFIED)
+ .empty()
+ .unwrap();
+ }
+ Error::MissingTrailingSlash(path) => {
+ return Builder::new()
+ .status(StatusCode::FOUND)
+ .header("location", format!("{}/", path))
+ .body("redirecting".into())
+ .unwrap();
+ }
+ Error::Redirect(target) => {
+ return Builder::new()
+ .status(StatusCode::FOUND)
+ .header("location", target)
+ .body("redirecting".into())
+ .unwrap();
+ }
+ };
+ // TODO: use Page
+ Builder::new()
+ .status(status)
+ .header("content-type", "text/html")
+ .body(message.into())
+ .unwrap()
+ }
+}