unify DB errors
This commit is contained in:
18
src/tags.rs
18
src/tags.rs
@@ -11,7 +11,7 @@ use rusqlite::{
|
||||
use serde::{Deserialize, Serialize};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::{ISE_MSG, database};
|
||||
use crate::database::{self, DatabaseError};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct Tag {
|
||||
@@ -61,25 +61,21 @@ pub enum TagError {
|
||||
#[error("No tag found with name {0}")]
|
||||
NoTagWithName(TagName),
|
||||
#[error("Database error: {0}")]
|
||||
DatabaseError(String),
|
||||
DatabaseError(#[from] DatabaseError),
|
||||
}
|
||||
impl From<rusqlite::Error> for TagError {
|
||||
fn from(error: rusqlite::Error) -> Self {
|
||||
TagError::DatabaseError(error.to_string())
|
||||
TagError::DatabaseError(DatabaseError::from(error))
|
||||
}
|
||||
}
|
||||
impl IntoResponse for TagError {
|
||||
fn into_response(self) -> Response {
|
||||
match self {
|
||||
Self::DatabaseError(e) => {
|
||||
log::error!("[ERROR] Database error occured: {e}");
|
||||
(StatusCode::INTERNAL_SERVER_ERROR, ISE_MSG.into())
|
||||
Self::DatabaseError(e) => e.into_response(),
|
||||
Self::TagNameError(_) => (StatusCode::BAD_REQUEST, self.to_string()).into_response(),
|
||||
Self::NoTagWithId(_) => (StatusCode::BAD_REQUEST, self.to_string()).into_response(),
|
||||
Self::NoTagWithName(_) => (StatusCode::BAD_REQUEST, self.to_string()).into_response(),
|
||||
}
|
||||
Self::TagNameError(_) => (StatusCode::BAD_REQUEST, self.to_string()),
|
||||
Self::NoTagWithId(_) => (StatusCode::BAD_REQUEST, self.to_string()),
|
||||
Self::NoTagWithName(_) => (StatusCode::BAD_REQUEST, self.to_string()),
|
||||
}
|
||||
.into_response()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -11,7 +11,8 @@ use rusqlite::OptionalExtension;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::{
|
||||
ISE_MSG, database,
|
||||
ISE_MSG,
|
||||
database::{self, DatabaseError},
|
||||
users::{
|
||||
User,
|
||||
auth::{
|
||||
@@ -44,17 +45,19 @@ impl IntoResponse for AuthError {
|
||||
Self::InvalidFormat => (StatusCode::BAD_REQUEST, self.to_string()).into_response(),
|
||||
Self::InvalidBase64(_) => (StatusCode::BAD_REQUEST, self.to_string()).into_response(),
|
||||
Self::InvalidUtf8(_) => (StatusCode::BAD_REQUEST, self.to_string()).into_response(),
|
||||
Self::DatabaseError(e) => {
|
||||
log::error!("[ERROR] Database error occured: {e}");
|
||||
(StatusCode::INTERNAL_SERVER_ERROR, ISE_MSG.to_string()).into_response()
|
||||
}
|
||||
Self::DatabaseError(e) => e.into_response(),
|
||||
Self::PassHashError(e) => {
|
||||
log::error!("[ERROR] A passwordhash error occured: {e}");
|
||||
log::error!("[PASSHASH] A passwordhash error occured: {e}");
|
||||
(StatusCode::INTERNAL_SERVER_ERROR, ISE_MSG.to_string()).into_response()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
impl From<rusqlite::Error> for AuthError {
|
||||
fn from(value: rusqlite::Error) -> Self {
|
||||
AuthError::DatabaseError(DatabaseError::from(value))
|
||||
}
|
||||
}
|
||||
|
||||
impl UserAuthRequired for Option<User> {
|
||||
fn required(self) -> Result<User, AuthError> {
|
||||
|
||||
@@ -4,9 +4,12 @@ use argon2::{Argon2, PasswordHasher, password_hash::SaltString};
|
||||
use axum::http::HeaderMap;
|
||||
use rand08::{RngCore, rngs::OsRng};
|
||||
|
||||
use crate::users::{
|
||||
use crate::{
|
||||
database::DatabaseError,
|
||||
users::{
|
||||
User, UserError,
|
||||
sessions::{Session, SessionError},
|
||||
},
|
||||
};
|
||||
|
||||
pub mod implementation;
|
||||
@@ -64,7 +67,7 @@ pub enum AuthError {
|
||||
#[error("Invalid UTF-8 in credentials")]
|
||||
InvalidUtf8(#[from] std::string::FromUtf8Error),
|
||||
#[error("Database error: {0}")]
|
||||
DatabaseError(#[from] rusqlite::Error),
|
||||
DatabaseError(#[from] DatabaseError),
|
||||
#[error("Argon2 passhash error: {0}")]
|
||||
PassHashError(argon2::password_hash::Error),
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ use uuid::Uuid;
|
||||
|
||||
use crate::{
|
||||
ISE_MSG,
|
||||
database::{self},
|
||||
database::{self, DatabaseError},
|
||||
users::{
|
||||
auth::UserPasswordHashing,
|
||||
handle::{UserHandle, UserHandleError},
|
||||
@@ -37,8 +37,8 @@ pub enum UserError {
|
||||
NoUserWithHandle(UserHandle),
|
||||
#[error("A user with handle {0} already exists")]
|
||||
HandleAlreadyExists(UserHandle),
|
||||
#[error("Database error: {0}")]
|
||||
DatabaseError(String),
|
||||
#[error("{0}")]
|
||||
DatabaseError(#[from] DatabaseError),
|
||||
#[error("Argon2 passhash error: {0}")]
|
||||
PassHashError(argon2::password_hash::Error),
|
||||
}
|
||||
@@ -192,7 +192,7 @@ impl User {
|
||||
|
||||
impl From<rusqlite::Error> for UserError {
|
||||
fn from(error: rusqlite::Error) -> Self {
|
||||
UserError::DatabaseError(error.to_string())
|
||||
UserError::DatabaseError(DatabaseError::from(error))
|
||||
}
|
||||
}
|
||||
impl From<argon2::password_hash::Error> for UserError {
|
||||
@@ -203,19 +203,19 @@ impl From<argon2::password_hash::Error> for UserError {
|
||||
impl IntoResponse for UserError {
|
||||
fn into_response(self) -> Response {
|
||||
match self {
|
||||
Self::DatabaseError(e) => {
|
||||
log::error!("[ERROR] Database error occured: {e}");
|
||||
(StatusCode::INTERNAL_SERVER_ERROR, ISE_MSG.into())
|
||||
}
|
||||
Self::DatabaseError(e) => e.into_response(),
|
||||
Self::PassHashError(e) => {
|
||||
log::error!("[ERROR] A passwordhash error occured: {e}");
|
||||
(StatusCode::INTERNAL_SERVER_ERROR, ISE_MSG.into())
|
||||
log::error!("[PASSHASH] A passwordhash error occured: {e}");
|
||||
(StatusCode::INTERNAL_SERVER_ERROR, ISE_MSG.to_string()).into_response()
|
||||
}
|
||||
Self::UserHandleError(_) => (StatusCode::BAD_REQUEST, self.to_string()).into_response(),
|
||||
Self::NoUserWithId(_) => (StatusCode::BAD_REQUEST, self.to_string()).into_response(),
|
||||
Self::NoUserWithHandle(_) => {
|
||||
(StatusCode::BAD_REQUEST, self.to_string()).into_response()
|
||||
}
|
||||
Self::HandleAlreadyExists(_) => {
|
||||
(StatusCode::CONFLICT, self.to_string()).into_response()
|
||||
}
|
||||
Self::UserHandleError(_) => (StatusCode::BAD_REQUEST, self.to_string()),
|
||||
Self::NoUserWithId(_) => (StatusCode::BAD_REQUEST, self.to_string()),
|
||||
Self::NoUserWithHandle(_) => (StatusCode::BAD_REQUEST, self.to_string()),
|
||||
Self::HandleAlreadyExists(_) => (StatusCode::CONFLICT, self.to_string()),
|
||||
}
|
||||
.into_response()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ use sha2::{Digest, Sha256};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::{
|
||||
ISE_MSG, database,
|
||||
database::{self, DatabaseError},
|
||||
users::{User, auth},
|
||||
};
|
||||
|
||||
@@ -34,10 +34,10 @@ pub enum SessionStatus {
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, thiserror::Error, Serialize)]
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum SessionError {
|
||||
#[error("Database error: {0}")]
|
||||
DatabaseError(String),
|
||||
DatabaseError(#[from] DatabaseError),
|
||||
#[error("No session found with id: {0}")]
|
||||
NoSessionWithId(Uuid),
|
||||
#[error("No session found with token: {0}")]
|
||||
@@ -45,20 +45,18 @@ pub enum SessionError {
|
||||
}
|
||||
impl From<rusqlite::Error> for SessionError {
|
||||
fn from(error: rusqlite::Error) -> Self {
|
||||
SessionError::DatabaseError(error.to_string())
|
||||
SessionError::DatabaseError(DatabaseError::from(error))
|
||||
}
|
||||
}
|
||||
impl IntoResponse for SessionError {
|
||||
fn into_response(self) -> Response {
|
||||
match self {
|
||||
Self::DatabaseError(e) => {
|
||||
log::error!("[ERROR] Database error occured: {e}");
|
||||
(StatusCode::INTERNAL_SERVER_ERROR, ISE_MSG.into())
|
||||
Self::DatabaseError(e) => e.into_response(),
|
||||
Self::NoSessionWithId(_) => (StatusCode::BAD_REQUEST, self.to_string()).into_response(),
|
||||
Self::NoSessionWithToken(_) => {
|
||||
(StatusCode::BAD_REQUEST, self.to_string()).into_response()
|
||||
}
|
||||
Self::NoSessionWithId(_) => (StatusCode::BAD_REQUEST, self.to_string()),
|
||||
Self::NoSessionWithToken(_) => (StatusCode::BAD_REQUEST, self.to_string()),
|
||||
}
|
||||
.into_response()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user