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)]
|
2020-12-18 22:31:29 +01:00
|
|
|
pub enum HighlightError {
|
|
|
|
UnknownLanguage,
|
|
|
|
UnknownTheme,
|
|
|
|
ResultUnserializable,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum ServiceError {
|
2020-04-17 20:44:55 +02:00
|
|
|
Unauthorized,
|
|
|
|
DatabaseConnectionLost,
|
|
|
|
DatabaseQueryFailed(String),
|
|
|
|
RecordNotFound(String),
|
|
|
|
RegisterCollision,
|
2020-10-21 23:59:17 +02:00
|
|
|
Error(WsError),
|
2020-12-18 22:31:29 +01:00
|
|
|
Highlight(HighlightError),
|
2020-04-17 20:44:55 +02:00
|
|
|
}
|
|
|
|
|
2020-12-18 22:31:29 +01:00
|
|
|
impl ServiceError {
|
2020-04-17 20:44:55 +02:00
|
|
|
pub fn into_http_response(self) -> HttpResponse {
|
|
|
|
self.into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-18 22:31:29 +01:00
|
|
|
impl Into<HttpResponse> for ServiceError {
|
2020-04-17 20:44:55 +02:00
|
|
|
fn into(self) -> HttpResponse {
|
|
|
|
match self {
|
2020-12-18 22:31:29 +01:00
|
|
|
ServiceError::Unauthorized => HttpResponse::Unauthorized().json(ErrorResponse {
|
2020-04-17 20:44:55 +02:00
|
|
|
errors: vec![TOKEN_NOT_FOUND.to_owned()],
|
|
|
|
}),
|
2020-12-18 22:31:29 +01:00
|
|
|
ServiceError::DatabaseConnectionLost => {
|
2020-04-17 20:44:55 +02:00
|
|
|
HttpResponse::InternalServerError().json(ErrorResponse {
|
|
|
|
errors: vec![DATABASE_CONNECTION_FAILED.to_owned()],
|
|
|
|
})
|
|
|
|
}
|
2020-12-18 22:31:29 +01:00
|
|
|
ServiceError::DatabaseQueryFailed(error) => {
|
2020-04-17 20:44:55 +02:00
|
|
|
HttpResponse::BadRequest().json(ErrorResponse {
|
|
|
|
errors: vec![error],
|
|
|
|
})
|
|
|
|
}
|
2020-12-18 22:31:29 +01:00
|
|
|
ServiceError::RecordNotFound(resource_name) => {
|
2020-04-17 20:44:55 +02:00
|
|
|
HttpResponse::BadRequest().json(ErrorResponse {
|
|
|
|
errors: vec![format!("Resource not found {}", resource_name)],
|
|
|
|
})
|
|
|
|
}
|
2020-12-18 22:31:29 +01:00
|
|
|
ServiceError::RegisterCollision => HttpResponse::Unauthorized().json(ErrorResponse {
|
2020-04-17 20:44:55 +02:00
|
|
|
errors: vec!["Register collision".to_string()],
|
|
|
|
}),
|
2020-12-18 22:31:29 +01:00
|
|
|
ServiceError::Error(error) => HttpResponse::BadRequest().json(ErrorResponse {
|
2020-10-21 23:59:17 +02:00
|
|
|
errors: vec![error.to_str().to_string()],
|
|
|
|
}),
|
2020-12-18 22:31:29 +01:00
|
|
|
ServiceError::Highlight(HighlightError::UnknownTheme) => HttpResponse::BadRequest()
|
|
|
|
.json(ErrorResponse::single(
|
|
|
|
"Code highlight Failed. Unexpected theme",
|
|
|
|
)),
|
|
|
|
ServiceError::Highlight(HighlightError::UnknownLanguage) => HttpResponse::BadRequest()
|
|
|
|
.json(ErrorResponse::single(
|
|
|
|
"Can't highlight in given language. It's unknown",
|
|
|
|
)),
|
|
|
|
ServiceError::Highlight(HighlightError::ResultUnserializable) => {
|
|
|
|
HttpResponse::BadRequest().json(ErrorResponse::single(
|
|
|
|
"Highlight succeed but result can't be send",
|
|
|
|
))
|
|
|
|
}
|
2020-04-17 20:44:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|