bitque/jirs-server/src/errors.rs

45 lines
1.4 KiB
Rust
Raw Normal View History

2020-03-27 16:17:25 +01:00
use actix_web::HttpResponse;
2020-04-02 16:14:07 +02:00
2020-03-28 21:41:16 +01:00
use jirs_data::ErrorResponse;
2020-03-27 16:17:25 +01:00
const TOKEN_NOT_FOUND: &str = "Token not found";
const DATABASE_CONNECTION_FAILED: &str = "Database connection failed";
pub enum ServiceErrors {
Unauthorized,
DatabaseConnectionLost,
2020-04-02 16:14:07 +02:00
DatabaseQueryFailed(String),
2020-03-27 16:17:25 +01:00
RecordNotFound(String),
}
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()],
})
}
2020-04-02 16:14:07 +02:00
ServiceErrors::DatabaseQueryFailed(error) => {
HttpResponse::BadRequest().json(ErrorResponse {
errors: vec![error.to_owned()],
})
}
2020-03-27 16:17:25 +01:00
ServiceErrors::RecordNotFound(resource_name) => {
HttpResponse::BadRequest().json(ErrorResponse {
errors: vec![format!("Resource not found {}", resource_name)],
})
}
}
}
}