2020-04-17 20:44:55 +02:00
|
|
|
use actix_web::HttpResponse;
|
|
|
|
|
2020-10-21 23:59:17 +02:00
|
|
|
use jirs_data::{msg::WsError, ErrorResponse};
|
2020-04-17 20:44:55 +02:00
|
|
|
|
|
|
|
const TOKEN_NOT_FOUND: &str = "Token not found";
|
|
|
|
const DATABASE_CONNECTION_FAILED: &str = "Database connection failed";
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum ServiceErrors {
|
|
|
|
Unauthorized,
|
|
|
|
DatabaseConnectionLost,
|
|
|
|
DatabaseQueryFailed(String),
|
|
|
|
RecordNotFound(String),
|
|
|
|
RegisterCollision,
|
2020-10-21 23:59:17 +02:00
|
|
|
Error(WsError),
|
2020-04-17 20:44:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ServiceErrors {
|
|
|
|
pub fn into_http_response(self) -> HttpResponse {
|
|
|
|
self.into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Into<HttpResponse> for ServiceErrors {
|
|
|
|
fn into(self) -> HttpResponse {
|
|
|
|
match self {
|
|
|
|
ServiceErrors::Unauthorized => HttpResponse::Unauthorized().json(ErrorResponse {
|
|
|
|
errors: vec![TOKEN_NOT_FOUND.to_owned()],
|
|
|
|
}),
|
|
|
|
ServiceErrors::DatabaseConnectionLost => {
|
|
|
|
HttpResponse::InternalServerError().json(ErrorResponse {
|
|
|
|
errors: vec![DATABASE_CONNECTION_FAILED.to_owned()],
|
|
|
|
})
|
|
|
|
}
|
|
|
|
ServiceErrors::DatabaseQueryFailed(error) => {
|
|
|
|
HttpResponse::BadRequest().json(ErrorResponse {
|
|
|
|
errors: vec![error],
|
|
|
|
})
|
|
|
|
}
|
|
|
|
ServiceErrors::RecordNotFound(resource_name) => {
|
|
|
|
HttpResponse::BadRequest().json(ErrorResponse {
|
|
|
|
errors: vec![format!("Resource not found {}", resource_name)],
|
|
|
|
})
|
|
|
|
}
|
|
|
|
ServiceErrors::RegisterCollision => HttpResponse::Unauthorized().json(ErrorResponse {
|
|
|
|
errors: vec!["Register collision".to_string()],
|
|
|
|
}),
|
2020-10-21 23:59:17 +02:00
|
|
|
ServiceErrors::Error(error) => HttpResponse::BadRequest().json(ErrorResponse {
|
|
|
|
errors: vec![error.to_str().to_string()],
|
|
|
|
}),
|
2020-04-17 20:44:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|