Switch to ApiResponse struct instead of ErrorModel
This commit is contained in:
parent
1d74b75ca8
commit
4673818894
@ -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<DBError> 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())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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::*;
|
||||
|
22
src/models/api_response.rs
Normal file
22
src/models/api_response.rs
Normal file
@ -0,0 +1,22 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(PartialEq, Debug, Clone, Serialize, Deserialize, new)]
|
||||
pub struct ApiResponse<T> {
|
||||
success: bool,
|
||||
response: T,
|
||||
}
|
||||
|
||||
impl<T> ApiResponse<T> {
|
||||
pub fn success(&self) -> bool {
|
||||
self.success
|
||||
}
|
||||
pub fn response(&self) -> &T {
|
||||
&self.response
|
||||
}
|
||||
pub fn successful(response: T) -> ApiResponse<T> {
|
||||
ApiResponse::new(true, response)
|
||||
}
|
||||
pub fn failure(response: T) -> ApiResponse<T> {
|
||||
ApiResponse::new(false, response)
|
||||
}
|
||||
}
|
@ -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,
|
||||
}
|
@ -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",
|
||||
|
@ -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<String> = 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<String> = 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()
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user