diff --git a/src/errors/domain_error.rs b/src/errors/domain_error.rs index cb0587f..bff0c68 100644 --- a/src/errors/domain_error.rs +++ b/src/errors/domain_error.rs @@ -3,7 +3,7 @@ use bcrypt::BcryptError; use custom_error::custom_error; // use derive_more::Display; // use diesel::result::DatabaseErrorKind; -use crate::models::errors::*; +use crate::models::api_response::*; use std::convert::From; // impl From for DomainError { @@ -37,57 +37,36 @@ impl ResponseError for DomainError { let err = self; match self { DomainError::PwdHashError { source: _ } => { - HttpResponse::InternalServerError().json(ErrorModel { - // error_code: 500, - success: false, - reason: err.to_string(), - }) + HttpResponse::InternalServerError() + .json(ApiResponse::failure(err.to_string())) } DomainError::DbError { source: _ } => { tracing::error!("{}", err); - HttpResponse::InternalServerError().json(ErrorModel { - // error_code: 500, - success: false, - reason: "Error in database".to_owned(), - }) + HttpResponse::InternalServerError() + .json(ApiResponse::failure("Error in database".to_owned())) } DomainError::DbPoolError { source: _ } => { tracing::error!("{}", err); - HttpResponse::InternalServerError().json(ErrorModel { - // error_code: 500, - success: false, - reason: "Error getting database pool".to_owned(), - }) + HttpResponse::InternalServerError().json(ApiResponse::failure( + "Error getting database pool".to_owned(), + )) } DomainError::PasswordError { cause: _ } => { - HttpResponse::BadRequest().json(ErrorModel { - // error_code: 400, - success: false, - reason: err.to_string(), - }) + HttpResponse::BadRequest() + .json(ApiResponse::failure(err.to_string())) } DomainError::EntityDoesNotExistError { message: _ } => { - HttpResponse::NotFound().json(ErrorModel { - // error_code: 400, - success: false, - reason: err.to_string(), - }) + HttpResponse::NotFound() + .json(ApiResponse::failure(err.to_string())) } DomainError::ThreadPoolError { message: _ } => { tracing::error!("{}", err); - HttpResponse::InternalServerError().json(ErrorModel { - // error_code: 400, - success: false, - reason: "Thread pool error occurred".to_owned(), - }) - } - DomainError::AuthError { message: _ } => { - HttpResponse::Forbidden().json(ErrorModel { - // error_code: 400, - success: false, - reason: err.to_string(), - }) + HttpResponse::InternalServerError().json(ApiResponse::failure( + "Thread pool error occurred".to_owned(), + )) } + DomainError::AuthError { message: _ } => HttpResponse::Forbidden() + .json(ApiResponse::failure(err.to_string())), } } } diff --git a/src/models.rs b/src/models.rs index fdc876b..05b2c1a 100644 --- a/src/models.rs +++ b/src/models.rs @@ -1,4 +1,4 @@ pub mod users; pub use self::users::*; -pub mod errors; -pub use self::errors::*; +pub mod api_response; +pub use self::api_response::*; diff --git a/src/models/api_response.rs b/src/models/api_response.rs new file mode 100644 index 0000000..dbea9fa --- /dev/null +++ b/src/models/api_response.rs @@ -0,0 +1,22 @@ +use serde::{Deserialize, Serialize}; + +#[derive(PartialEq, Debug, Clone, Serialize, Deserialize, new)] +pub struct ApiResponse { + success: bool, + response: T, +} + +impl ApiResponse { + pub fn success(&self) -> bool { + self.success + } + pub fn response(&self) -> &T { + &self.response + } + pub fn successful(response: T) -> ApiResponse { + ApiResponse::new(true, response) + } + pub fn failure(response: T) -> ApiResponse { + ApiResponse::new(false, response) + } +} diff --git a/src/models/errors.rs b/src/models/errors.rs deleted file mode 100644 index de6b5b5..0000000 --- a/src/models/errors.rs +++ /dev/null @@ -1,14 +0,0 @@ -use serde::{Deserialize, Serialize}; - -#[derive(Debug, Clone, Serialize, new)] -pub struct JsonErrorModel<'a> { - status_code: i16, - pub line: String, - pub reason: &'a str, -} -#[derive(PartialEq, Debug, Clone, Serialize, Deserialize, new)] -pub struct ErrorModel { - // pub error_code: i16, - pub success: bool, - pub reason: String, -} diff --git a/src/routes/users.rs b/src/routes/users.rs index 100d29b..31dcb55 100644 --- a/src/routes/users.rs +++ b/src/routes/users.rs @@ -1,4 +1,4 @@ -use actix_web::{get, post, web, HttpResponse}; +use actix_web::{get, web, HttpResponse}; use crate::services::UserService; use crate::{actions, models}; @@ -120,7 +120,7 @@ pub async fn add_user( // cause: e.to_string(), // }; // Err(res2) - let res = HttpResponse::BadRequest().body(e.to_string()); + let res = HttpResponse::BadRequest().json(e); // .json(models::ErrorModel::new( // 40, // "Error registering user due to validation errors", diff --git a/tests/integration/users.rs b/tests/integration/users.rs index 5254163..1714f68 100644 --- a/tests/integration/users.rs +++ b/tests/integration/users.rs @@ -3,7 +3,7 @@ use crate::common; mod tests { use super::*; - use actix_demo::models::ErrorModel; + use actix_demo::models::ApiResponse; use actix_web::dev::Service as _; use actix_web::http::StatusCode; use actix_web::test; @@ -13,14 +13,13 @@ mod tests { let req = test::TestRequest::get().uri("/api/users").to_request(); let resp = common::test_app().await.unwrap().call(req).await.unwrap(); assert_eq!(resp.status(), StatusCode::NOT_FOUND); - let body: ErrorModel = test::read_body_json(resp).await; + let body: ApiResponse = test::read_body_json(resp).await; tracing::debug!("{:?}", body); assert_eq!( body, - ErrorModel { - success: false, - reason: "Entity does not exist - No users available".to_owned() - } + ApiResponse::failure( + "Entity does not exist - No users available".to_owned() + ) ); } @@ -30,15 +29,13 @@ mod tests { let req = test::TestRequest::get().uri("/api/users/1").to_request(); let resp = common::test_app().await.unwrap().call(req).await.unwrap(); assert_eq!(resp.status(), StatusCode::NOT_FOUND); - let body: ErrorModel = test::read_body_json(resp).await; + let body: ApiResponse = test::read_body_json(resp).await; tracing::debug!("{:?}", body); assert_eq!( body, - ErrorModel { - success: false, - reason: "Entity does not exist - No user found with uid: 1" - .to_owned() - } + ApiResponse::failure( + "Entity does not exist - No user found with uid: 1".to_owned() + ) ); } }