aboutsummaryrefslogtreecommitdiff
path: root/src/error
diff options
context:
space:
mode:
Diffstat (limited to 'src/error')
-rw-r--r--src/error/client.rs33
-rw-r--r--src/error/mod.rs50
-rw-r--r--src/error/server.rs24
3 files changed, 107 insertions, 0 deletions
diff --git a/src/error/client.rs b/src/error/client.rs
new file mode 100644
index 0000000..980e3d2
--- /dev/null
+++ b/src/error/client.rs
@@ -0,0 +1,33 @@
1pub enum ClientError {
2 BadJsonBody(String),
3 BadAuthTokenHeader(String),
4 UserAlreadyExists { username: String },
5 InvalidPassword,
6 NotAuthorized,
7}
8
9impl ClientError {
10 pub fn kind(&self) -> String {
11 match self {
12 Self::BadJsonBody(..) => "BadJsonBody",
13 Self::BadAuthTokenHeader(..) => "BadAuthTokenHeader",
14 Self::UserAlreadyExists { .. } => "UserAlreadyExists",
15 Self::InvalidPassword => "InvalidPassword",
16 Self::NotAuthorized => "NotAuthorized",
17 }
18 .to_string()
19 }
20
21 pub fn into_message(self) -> String {
22 match self {
23 Self::BadJsonBody(msg) => msg,
24 Self::BadAuthTokenHeader(msg) => msg,
25 Self::UserAlreadyExists { username } => {
26 format!("user with username `{}` already exists", username)
27 }
28 Self::InvalidPassword => "password is invalid".to_string(),
29
30 Self::NotAuthorized => "user is not authorized".to_string(),
31 }
32 }
33}
diff --git a/src/error/mod.rs b/src/error/mod.rs
new file mode 100644
index 0000000..55d7250
--- /dev/null
+++ b/src/error/mod.rs
@@ -0,0 +1,50 @@
1mod client;
2mod server;
3
4pub use client::ClientError;
5pub use server::ServerError;
6
7use argon2::password_hash::Error as PasswordHashError;
8use axum::extract::rejection::JsonRejection;
9use axum_extra::typed_header::TypedHeaderRejection;
10use sea_orm::DbErr;
11
12pub enum ApiError {
13 Client(ClientError),
14 Server(ServerError),
15}
16
17impl From<ClientError> for ApiError {
18 fn from(value: ClientError) -> Self {
19 Self::Client(value)
20 }
21}
22impl From<ServerError> for ApiError {
23 fn from(value: ServerError) -> Self {
24 Self::Server(value)
25 }
26}
27
28impl From<JsonRejection> for ApiError {
29 fn from(value: JsonRejection) -> Self {
30 Self::Client(ClientError::BadJsonBody(value.body_text()))
31 }
32}
33
34impl From<TypedHeaderRejection> for ApiError {
35 fn from(value: TypedHeaderRejection) -> Self {
36 Self::Client(ClientError::BadAuthTokenHeader(value.to_string()))
37 }
38}
39
40impl From<DbErr> for ApiError {
41 fn from(value: DbErr) -> Self {
42 Self::Server(ServerError::Database(value.to_string()))
43 }
44}
45
46impl From<PasswordHashError> for ApiError {
47 fn from(value: PasswordHashError) -> Self {
48 Self::Server(ServerError::PasswordHash(value.to_string()))
49 }
50}
diff --git a/src/error/server.rs b/src/error/server.rs
new file mode 100644
index 0000000..e67714d
--- /dev/null
+++ b/src/error/server.rs
@@ -0,0 +1,24 @@
1pub enum ServerError {
2 Database(String),
3 PasswordHash(String),
4 Token(String),
5}
6
7impl ServerError {
8 pub fn kind(&self) -> String {
9 match self {
10 Self::Database(..) => "Database",
11 Self::PasswordHash(..) => "PasswordHash",
12 Self::Token(..) => "Token",
13 }
14 .to_string()
15 }
16
17 pub fn into_message(self) -> String {
18 match self {
19 Self::Database(msg) => msg,
20 Self::PasswordHash(msg) => msg,
21 Self::Token(msg) => msg,
22 }
23 }
24}