summaryrefslogtreecommitdiffstats
path: root/src/error.rs
diff options
context:
space:
mode:
authorJokler <jokler@protonmail.com>2020-06-23 16:26:07 +0200
committerJokler <jokler@protonmail.com>2020-06-23 16:26:07 +0200
commitfc7bd403bf41d724bd8dc3bf3b827c592c539171 (patch)
tree1b816f68f1793dc8bf70e490196da6ae58d6f16a /src/error.rs
downloadrgit-master.tar.gz
rgit-master.zip
Initial commitHEADmaster
Diffstat (limited to 'src/error.rs')
-rw-r--r--src/error.rs44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/error.rs b/src/error.rs
new file mode 100644
index 0000000..062bb3d
--- /dev/null
+++ b/src/error.rs
@@ -0,0 +1,44 @@
+use actix_web::{error::ResponseError, HttpResponse};
+use argonautica::Error as HashError;
+use derive_more::Display;
+use diesel::result::Error as DieselError;
+use log::error;
+use std::convert::From;
+
+#[derive(Debug, Display)]
+pub enum ServiceError {
+ #[display(fmt = "Internal Server Error")]
+ InternalServerError,
+
+ #[display(fmt = "Unauthorized")]
+ Unauthorized,
+
+ #[display(fmt = "Not Found")]
+ NotFound,
+}
+
+impl ResponseError for ServiceError {
+ fn error_response(&self) -> HttpResponse {
+ match *self {
+ ServiceError::InternalServerError => {
+ HttpResponse::InternalServerError().body("Internal Server Error")
+ }
+ ServiceError::Unauthorized => HttpResponse::Unauthorized().body("Unauthorized"),
+ ServiceError::NotFound => HttpResponse::NotFound().body("404 Not Found"),
+ }
+ }
+}
+
+impl From<DieselError> for ServiceError {
+ fn from(e: DieselError) -> ServiceError {
+ error!("Database error: {}", e);
+ ServiceError::InternalServerError
+ }
+}
+
+impl From<HashError> for ServiceError {
+ fn from(e: HashError) -> ServiceError {
+ error!("Hash error: {}", e);
+ ServiceError::InternalServerError
+ }
+}