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

View File

@@ -2,9 +2,10 @@ use std::collections::BTreeMap;
use serde::{Deserialize, Serialize};
use serde_json::json;
use thiserror::Error;
use crate::core::{
protocol::request_validation::{NameValidationError, OwnerValidationError},
protocol::request_validation::AuthorizationError,
types::{DbOrUser, MySQLUser},
};
@@ -12,11 +13,15 @@ pub type DropUsersRequest = Vec<MySQLUser>;
pub type DropUsersResponse = BTreeMap<MySQLUser, Result<(), DropUserError>>;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[derive(Error, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum DropUserError {
SanitizationError(NameValidationError),
OwnershipError(OwnerValidationError),
#[error("Authorization error: {0}")]
AuthorizationError(#[from] AuthorizationError),
#[error("User does not exist")]
UserDoesNotExist,
#[error("MySQL error: {0}")]
MySqlError(String),
}
@@ -60,10 +65,7 @@ pub fn print_drop_users_output_status_json(output: &DropUsersResponse) {
impl DropUserError {
pub fn to_error_message(&self, username: &MySQLUser) -> String {
match self {
DropUserError::SanitizationError(err) => {
err.to_error_message(DbOrUser::User(username.clone()))
}
DropUserError::OwnershipError(err) => {
DropUserError::AuthorizationError(err) => {
err.to_error_message(DbOrUser::User(username.clone()))
}
DropUserError::UserDoesNotExist => {
@@ -77,10 +79,7 @@ impl DropUserError {
pub fn error_type(&self) -> String {
match self {
DropUserError::SanitizationError(err) => {
format!("sanitization-error/{}", err.error_type())
}
DropUserError::OwnershipError(err) => format!("ownership-error/{}", err.error_type()),
DropUserError::AuthorizationError(err) => err.error_type(),
DropUserError::UserDoesNotExist => "user-does-not-exist".to_string(),
DropUserError::MySqlError(_) => "mysql-error".to_string(),
}