-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.rs
More file actions
38 lines (32 loc) · 1.07 KB
/
Copy patherrors.rs
File metadata and controls
38 lines (32 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
use actix_web::{HttpResponse, ResponseError};
use thiserror::Error;
#[derive(Debug, Error)]
pub(crate) enum TaskError {
#[error("`title` field of `Task` cannot be empty!")]
EmptyTitle,
}
#[derive(Debug, Error)]
pub(crate) enum AppError {
#[error("`{0}`")]
Task(#[from] TaskError),
#[error("`{0}`")]
Database(#[from] sqlx::Error),
#[error("`{0}`")]
Json(#[from] serde_json::Error),
}
impl ResponseError for AppError {
fn status_code(&self) -> actix_web::http::StatusCode {
match self {
AppError::Task(task_error) => match task_error {
TaskError::EmptyTitle => actix_web::http::StatusCode::UNPROCESSABLE_ENTITY,
},
AppError::Database(_) => actix_web::http::StatusCode::INTERNAL_SERVER_ERROR,
AppError::Json(_) => actix_web::http::StatusCode::INTERNAL_SERVER_ERROR,
}
}
fn error_response(&self) -> HttpResponse {
let status_code = self.status_code();
let response = HttpResponse::build(status_code).body(self.to_string());
response
}
}