diff options
Diffstat (limited to 'src/error.rs')
-rw-r--r-- | src/error.rs | 28 |
1 files changed, 22 insertions, 6 deletions
diff --git a/src/error.rs b/src/error.rs index 351c01a..2824e49 100644 --- a/src/error.rs +++ b/src/error.rs @@ -20,6 +20,9 @@ pub enum Error { #[error("Json error: {0}")] Json(#[from] serde_json::Error), + #[error("JWT error: {0}")] + JWT(#[from] jsonwebtoken::errors::Error), + #[error("Database error: {0}")] Sqlx(#[from] sqlx::Error), @@ -27,7 +30,7 @@ pub enum Error { Migration(#[from] sqlx::migrate::MigrateError), #[error("Failed to hash password: {0}")] - PasswordHash(#[from] argon2::password_hash::Error), + PasswordHash(#[source] argon2::password_hash::Error), #[error("User not found")] UserNotFound, @@ -45,12 +48,22 @@ pub enum Error { Other(String), } +impl From<argon2::password_hash::Error> for Error { + fn from(value: argon2::password_hash::Error) -> Self { + match value { + argon2::password_hash::Error::Password => Self::LoginInvalid, + _ => Self::PasswordHash(value), + } + } +} + impl From<&Error> for StatusCode { fn from(value: &Error) -> Self { match value { Error::UserNotFound => StatusCode::NOT_FOUND, Error::EmailExists => StatusCode::CONFLICT, Error::EmailInvalid(_) => StatusCode::UNPROCESSABLE_ENTITY, + Error::LoginInvalid => StatusCode::UNAUTHORIZED, _ => StatusCode::INTERNAL_SERVER_ERROR, } } @@ -60,10 +73,13 @@ impl axum::response::IntoResponse for Error { fn into_response(self) -> axum::response::Response { // TODO: implement [rfc7807](https://www.rfc-editor.org/rfc/rfc7807.html) - Json(json!({ - "status": StatusCode::from(&self).to_string(), - "detail": self.to_string(), - })) - .into_response() + ( + StatusCode::from(&self), + Json(json!({ + "status": StatusCode::from(&self).to_string(), + "detail": self.to_string(), + })), + ) + .into_response() } } |