-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.rs
More file actions
31 lines (28 loc) · 848 Bytes
/
Copy patherror.rs
File metadata and controls
31 lines (28 loc) · 848 Bytes
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
use actix_web::{HttpResponse, ResponseError, http::StatusCode};
use serde_json::json;
use thiserror::Error;
use utoipa::ToSchema;
#[derive(Debug, Error, ToSchema)]
pub enum AppError {
#[error("Database Error")]
Database {
#[from]
#[schema(value_type = String)]
source: sqlx::Error,
},
#[error("Resource not found")]
NotFound,
}
impl ResponseError for AppError {
fn status_code(&self) -> actix_web::http::StatusCode {
match self {
AppError::Database { source: _ } => StatusCode::INTERNAL_SERVER_ERROR,
AppError::NotFound => StatusCode::NOT_FOUND,
}
}
fn error_response(&self) -> actix_web::HttpResponse<actix_web::body::BoxBody> {
HttpResponse::build(self.status_code()).json(json!({
"error": self.to_string()
}))
}
}