|
1 | | -use std::error::Error; |
2 | | -use actix_web::{error::QueryPayloadError, http::header::ContentType, HttpRequest, HttpResponse}; |
| 1 | +use actix_web::{error::QueryPayloadError, HttpRequest}; |
3 | 2 | use serde::{Deserialize, Serialize}; |
4 | | -use std::fmt::Display; |
5 | | -use actix_web::http::StatusCode; |
| 3 | + |
| 4 | +use crate::endpoints::ApiError; |
6 | 5 |
|
7 | 6 | #[derive(Serialize, Deserialize)] |
8 | 7 | pub struct PaginatedData<T> { |
9 | 8 | pub data: Vec<T>, |
10 | 9 | pub count: i64, |
11 | 10 | } |
12 | 11 |
|
13 | | -#[derive(Debug, PartialEq)] |
14 | | -pub enum ApiError { |
15 | | - FilesystemError, |
16 | | - DbAcquireError, |
17 | | - DbError, |
18 | | - TransactionError, |
19 | | - InternalError, |
20 | | - BadRequest(String), |
21 | | - NotFound(String), |
22 | | - Unauthorized, |
23 | | - Forbidden, |
24 | | -} |
25 | | - |
26 | 12 | #[derive(Debug, Serialize, Deserialize)] |
27 | 13 | pub struct ApiResponse<T> { |
28 | 14 | pub error: String, |
29 | 15 | pub payload: T, |
30 | 16 | } |
31 | 17 |
|
32 | | -impl Error for ApiError {} |
33 | | - |
34 | | -impl Display for ApiError { |
35 | | - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
36 | | - match self { |
37 | | - Self::FilesystemError => write!(f, "Unknown filesystem error"), |
38 | | - Self::DbAcquireError => write!(f, "Database is busy"), |
39 | | - Self::DbError => write!(f, "Unknown database error"), |
40 | | - Self::TransactionError => write!(f, "Unknown transaction error"), |
41 | | - Self::BadRequest(message) => write!(f, "{}", message), |
42 | | - Self::NotFound(message) => write!(f, "{}", message), |
43 | | - Self::InternalError => write!(f, "Internal server error"), |
44 | | - Self::Forbidden => write!(f, "You cannot perform this action"), |
45 | | - Self::Unauthorized => write!(f, "You need to be authenticated to perform this action"), |
46 | | - } |
47 | | - } |
48 | | -} |
49 | | - |
50 | | -impl actix_web::ResponseError for ApiError { |
51 | | - fn status_code(&self) -> StatusCode { |
52 | | - match self { |
53 | | - Self::FilesystemError => StatusCode::INTERNAL_SERVER_ERROR, |
54 | | - Self::DbAcquireError => StatusCode::INTERNAL_SERVER_ERROR, |
55 | | - Self::DbError => StatusCode::INTERNAL_SERVER_ERROR, |
56 | | - Self::TransactionError => StatusCode::INTERNAL_SERVER_ERROR, |
57 | | - Self::BadRequest(_) => StatusCode::BAD_REQUEST, |
58 | | - Self::NotFound(_) => StatusCode::NOT_FOUND, |
59 | | - Self::InternalError => StatusCode::INTERNAL_SERVER_ERROR, |
60 | | - Self::Unauthorized => StatusCode::UNAUTHORIZED, |
61 | | - Self::Forbidden => StatusCode::FORBIDDEN, |
62 | | - } |
63 | | - } |
64 | | - fn error_response(&self) -> HttpResponse<actix_web::body::BoxBody> { |
65 | | - HttpResponse::build(self.status_code()) |
66 | | - .append_header(ContentType::json()) |
67 | | - .json(ApiResponse { |
68 | | - error: self.to_string(), |
69 | | - payload: "".to_string(), |
70 | | - }) |
71 | | - } |
72 | | -} |
73 | | - |
74 | 18 | pub fn query_error_handler(err: QueryPayloadError, _req: &HttpRequest) -> actix_web::Error { |
75 | 19 | ApiError::BadRequest(err.to_string()).into() |
76 | 20 | } |
|
0 commit comments