core/protocol: use thiserror, use common authorization error struct

This commit is contained in:
2025-12-15 14:25:22 +09:00
parent 1991e7bfd8
commit bf6027f507
23 changed files with 367 additions and 317 deletions
+55 -7
View File
@@ -1,13 +1,21 @@
use indoc::indoc;
use itertools::Itertools;
use serde::{Deserialize, Serialize};
use thiserror::Error;
use crate::core::{common::UnixUser, types::DbOrUser};
#[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize, Deserialize)]
#[derive(Error, Debug, PartialEq, Eq, Clone, Copy, Serialize, Deserialize)]
pub enum NameValidationError {
#[error("Name cannot be empty.")]
EmptyString,
#[error(
"Name contains invalid characters. Only A-Z, a-z, 0-9, _ (underscore) and - (dash) are permitted."
)]
InvalidCharacters,
#[error("Name is too long. Maximum length is 64 characters.")]
TooLong,
}
@@ -44,6 +52,15 @@ impl NameValidationError {
}
}
#[derive(Error, Debug, PartialEq, Eq, Clone, Copy, Serialize, Deserialize)]
pub enum OwnerValidationError {
#[error("No matching owner prefix found")]
NoMatch,
#[error("Name cannot be empty")]
StringEmpty,
}
impl OwnerValidationError {
pub fn to_error_message(self, db_or_user: DbOrUser) -> String {
let user = UnixUser::from_enviroment();
@@ -98,11 +115,42 @@ impl OwnerValidationError {
}
}
#[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize, Deserialize)]
pub enum OwnerValidationError {
// The name is valid, but none of the given prefixes matched the name
NoMatch,
#[derive(Error, Debug, PartialEq, Eq, Clone, Copy, Serialize, Deserialize)]
pub enum AuthorizationError {
#[error("Sanitization error: {0}")]
SanitizationError(NameValidationError),
// The name is empty, which is invalid
StringEmpty,
#[error("Ownership error: {0}")]
OwnershipError(OwnerValidationError),
// AuthorizationHandlerError(String),
}
impl AuthorizationError {
pub fn to_error_message(&self, db_or_user: DbOrUser) -> String {
match self {
AuthorizationError::SanitizationError(err) => err.to_error_message(db_or_user),
AuthorizationError::OwnershipError(err) => err.to_error_message(db_or_user),
// AuthorizationError::AuthorizationHandlerError(msg) => {
// format!(
// "Authorization handler error for '{}': {}",
// db_or_user.name(),
// msg
// )
// }
}
}
pub fn error_type(&self) -> String {
match self {
AuthorizationError::SanitizationError(err) => {
format!("sanitization-error/{}", err.error_type())
}
// TODO: maybe rename this to authorization error?
AuthorizationError::OwnershipError(err) => {
format!("ownership-error/{}", err.error_type())
} // AuthorizationError::AuthorizationHandlerError(_) => {
// "authorization-handler-error".to_string()
// }
}
}
}