diff options
Diffstat (limited to 'src/error.rs')
| -rw-r--r-- | src/error.rs | 91 |
1 files changed, 0 insertions, 91 deletions
diff --git a/src/error.rs b/src/error.rs deleted file mode 100644 index 1eb54ef..0000000 --- a/src/error.rs +++ /dev/null | |||
| @@ -1,91 +0,0 @@ | |||
| 1 | use axum::{ | ||
| 2 | extract::rejection::JsonRejection, | ||
| 3 | response::{IntoResponse, Response}, | ||
| 4 | }; | ||
| 5 | use axum_extra::typed_header::TypedHeaderRejection; | ||
| 6 | |||
| 7 | use crate::response::{ErrorResponse, FailResponse, SuccessResponse}; | ||
| 8 | |||
| 9 | pub type ApiResult<T> = Result<SuccessResponse<T>, ApiError>; | ||
| 10 | |||
| 11 | pub enum ApiError { | ||
| 12 | // 400 | ||
| 13 | BadJsonBody(String), | ||
| 14 | BadAuthTokenHeader(String), | ||
| 15 | UserAlreadyExists { username: String }, | ||
| 16 | InvalidPassword, | ||
| 17 | NotAuthorized, | ||
| 18 | // 500 | ||
| 19 | Database(String), | ||
| 20 | PasswordHash(String), | ||
| 21 | InternalJwt(String), | ||
| 22 | } | ||
| 23 | |||
| 24 | impl From<JsonRejection> for ApiError { | ||
| 25 | fn from(value: JsonRejection) -> Self { | ||
| 26 | Self::BadJsonBody(value.body_text()) | ||
| 27 | } | ||
| 28 | } | ||
| 29 | |||
| 30 | impl From<TypedHeaderRejection> for ApiError { | ||
| 31 | fn from(value: TypedHeaderRejection) -> Self { | ||
| 32 | Self::BadAuthTokenHeader(value.to_string()) | ||
| 33 | } | ||
| 34 | } | ||
| 35 | |||
| 36 | impl From<sea_orm::DbErr> for ApiError { | ||
| 37 | fn from(value: sea_orm::DbErr) -> Self { | ||
| 38 | Self::Database(value.to_string()) | ||
| 39 | } | ||
| 40 | } | ||
| 41 | |||
| 42 | impl From<argon2::password_hash::Error> for ApiError { | ||
| 43 | fn from(value: argon2::password_hash::Error) -> Self { | ||
| 44 | Self::PasswordHash(value.to_string()) | ||
| 45 | } | ||
| 46 | } | ||
| 47 | |||
| 48 | impl ToString for ApiError { | ||
| 49 | fn to_string(&self) -> String { | ||
| 50 | match self { | ||
| 51 | // 400 | ||
| 52 | ApiError::BadJsonBody(..) => "BadJsonBody", | ||
| 53 | ApiError::BadAuthTokenHeader(..) => "BadAuthTokenHeader", | ||
| 54 | ApiError::UserAlreadyExists { .. } => "UserAlreadyExists", | ||
| 55 | ApiError::InvalidPassword => "InvalidPassword", | ||
| 56 | ApiError::NotAuthorized => "NotAuthorized", | ||
| 57 | // 500 | ||
| 58 | ApiError::Database(..) => "Database", | ||
| 59 | ApiError::PasswordHash(..) => "PasswordHash", | ||
| 60 | ApiError::InternalJwt(..) => "InternalJwt", | ||
| 61 | } | ||
| 62 | .to_string() | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | impl IntoResponse for ApiError { | ||
| 67 | fn into_response(self) -> Response { | ||
| 68 | let kind = self.to_string(); | ||
| 69 | match self { | ||
| 70 | // 400 | ||
| 71 | ApiError::BadJsonBody(msg) => FailResponse(kind, msg).into_response(), | ||
| 72 | ApiError::BadAuthTokenHeader(msg) => FailResponse(kind, msg).into_response(), | ||
| 73 | ApiError::UserAlreadyExists { username } => FailResponse( | ||
| 74 | kind, | ||
| 75 | format!("user with username `{}` already exists", username), | ||
| 76 | ) | ||
| 77 | .into_response(), | ||
| 78 | ApiError::InvalidPassword => { | ||
| 79 | FailResponse(kind, "password is invalid".to_string()).into_response() | ||
| 80 | } | ||
| 81 | ApiError::NotAuthorized => { | ||
| 82 | FailResponse(kind, "user is not authorized".to_string()).into_response() | ||
| 83 | } | ||
| 84 | |||
| 85 | // 500 | ||
| 86 | ApiError::Database(msg) => ErrorResponse(kind, msg).into_response(), | ||
| 87 | ApiError::PasswordHash(msg) => ErrorResponse(kind, msg).into_response(), | ||
| 88 | ApiError::InternalJwt(msg) => ErrorResponse(kind, msg).into_response(), | ||
| 89 | } | ||
| 90 | } | ||
| 91 | } | ||
