summaryrefslogtreecommitdiff
path: root/src/errors.rs
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2021-03-13 10:17:02 +0100
committerSanto Cariotti <santo@dcariotti.me>2021-03-13 10:17:02 +0100
commit44a35e651741afb6c417da47d636e4380cdd225f (patch)
tree72302a7bea06350b1e7175a6dddb08daebbbb135 /src/errors.rs
parent48a9ac895b6e8b01622810ec4bf2f3a423426ca3 (diff)
feat: add get all repos from db
Diffstat (limited to 'src/errors.rs')
-rw-r--r--src/errors.rs80
1 files changed, 80 insertions, 0 deletions
diff --git a/src/errors.rs b/src/errors.rs
new file mode 100644
index 0000000..ee3ca71
--- /dev/null
+++ b/src/errors.rs
@@ -0,0 +1,80 @@
+use actix_web::{error::ResponseError, http::StatusCode, HttpResponse};
+use deadpool_postgres::PoolError;
+use serde::Serialize;
+use std::fmt;
+use tokio_postgres::error::Error;
+
+#[derive(Debug)]
+pub enum AppErrorType {
+ DbError,
+ NotFoundError,
+}
+
+#[derive(Debug)]
+pub struct AppError {
+ pub message: Option<String>,
+ pub cause: Option<String>,
+ pub error_type: AppErrorType,
+}
+
+impl AppError {
+ pub fn message(&self) -> String {
+ match &*self {
+ AppError {
+ message: Some(message),
+ ..
+ } => message.clone(),
+ AppError {
+ message: None,
+ error_type: AppErrorType::NotFoundError,
+ ..
+ } => "The requested item was not found".to_string(),
+ _ => "An unexpected error has occurred".to_string(),
+ }
+ }
+}
+
+impl From<PoolError> for AppError {
+ fn from(error: PoolError) -> AppError {
+ AppError {
+ message: None,
+ cause: Some(error.to_string()),
+ error_type: AppErrorType::DbError,
+ }
+ }
+}
+
+impl From<Error> for AppError {
+ fn from(error: Error) -> AppError {
+ AppError {
+ message: None,
+ cause: Some(error.to_string()),
+ error_type: AppErrorType::DbError,
+ }
+ }
+}
+
+impl fmt::Display for AppError {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
+ write!(f, "{:?}", self)
+ }
+}
+
+#[derive(Serialize)]
+pub struct AppErrorResponse {
+ pub error: String,
+}
+
+impl ResponseError for AppError {
+ fn status_code(&self) -> StatusCode {
+ match self.error_type {
+ AppErrorType::DbError => StatusCode::INTERNAL_SERVER_ERROR,
+ AppErrorType::NotFoundError => StatusCode::NOT_FOUND,
+ }
+ }
+ fn error_response(&self) -> HttpResponse {
+ HttpResponse::build(self.status_code()).json(AppErrorResponse {
+ error: self.message(),
+ })
+ }
+}