From 15628f7682ed09e3e82521994ebf247f3e61dc15 Mon Sep 17 00:00:00 2001 From: h7x4 Date: Thu, 4 Dec 2025 12:18:50 +0900 Subject: [PATCH] client: add error subtypes for name and owner validation in json output --- .../protocol/commands/check_authorization.rs | 9 ++-- .../protocol/commands/create_databases.rs | 10 ++--- src/core/protocol/commands/create_users.rs | 10 ++--- src/core/protocol/commands/drop_databases.rs | 10 ++--- src/core/protocol/commands/drop_users.rs | 10 ++--- .../protocol/commands/list_all_databases.rs | 7 +++ .../protocol/commands/list_all_privileges.rs | 7 +++ src/core/protocol/commands/list_all_users.rs | 7 +++ src/core/protocol/commands/list_databases.rs | 10 ++--- src/core/protocol/commands/list_privileges.rs | 10 ++--- src/core/protocol/commands/list_users.rs | 10 ++--- src/core/protocol/commands/lock_users.rs | 12 ++--- .../protocol/commands/modify_privileges.rs | 44 +++++++++++-------- src/core/protocol/commands/passwd_user.rs | 10 ++--- src/core/protocol/commands/unlock_users.rs | 12 ++--- src/core/protocol/request_validation.rs | 15 +++++++ 16 files changed, 118 insertions(+), 75 deletions(-) diff --git a/src/core/protocol/commands/check_authorization.rs b/src/core/protocol/commands/check_authorization.rs index d68ddfb..ec0c9bf 100644 --- a/src/core/protocol/commands/check_authorization.rs +++ b/src/core/protocol/commands/check_authorization.rs @@ -79,11 +79,12 @@ impl CheckAuthorizationError { } } - pub fn error_type(&self) -> &'static str { + pub fn error_type(&self) -> String { match self { - CheckAuthorizationError::SanitizationError(_) => "sanitization-error", - CheckAuthorizationError::OwnershipError(_) => "ownership-error", - // CheckAuthorizationError::AuthorizationHandlerError(_) => "authorization-handler-error", + CheckAuthorizationError::SanitizationError(err) => format!("sanitization-error/{}", err.error_type()), + CheckAuthorizationError::OwnershipError(err) => format!("ownership-error/{}", err.error_type()), + + // CheckAuthorizationError::AuthorizationHandlerError(_) => "authorization-handler-error".to_string(), } } } diff --git a/src/core/protocol/commands/create_databases.rs b/src/core/protocol/commands/create_databases.rs index 8f25a53..1f57cbe 100644 --- a/src/core/protocol/commands/create_databases.rs +++ b/src/core/protocol/commands/create_databases.rs @@ -75,12 +75,12 @@ impl CreateDatabaseError { } } - pub fn error_type(&self) -> &'static str { + pub fn error_type(&self) -> String { match self { - CreateDatabaseError::SanitizationError(_) => "sanitization-error", - CreateDatabaseError::OwnershipError(_) => "ownership-error", - CreateDatabaseError::DatabaseAlreadyExists => "database-already-exists", - CreateDatabaseError::MySqlError(_) => "mysql-error", + CreateDatabaseError::SanitizationError(err) => format!("sanitization-error/{}", err.error_type()), + CreateDatabaseError::OwnershipError(err) => format!("ownership-error/{}", err.error_type()), + CreateDatabaseError::DatabaseAlreadyExists => "database-already-exists".to_string(), + CreateDatabaseError::MySqlError(_) => "mysql-error".to_string(), } } } diff --git a/src/core/protocol/commands/create_users.rs b/src/core/protocol/commands/create_users.rs index fb6efb1..341f21a 100644 --- a/src/core/protocol/commands/create_users.rs +++ b/src/core/protocol/commands/create_users.rs @@ -75,12 +75,12 @@ impl CreateUserError { } } - pub fn error_type(&self) -> &'static str { + pub fn error_type(&self) -> String { match self { - CreateUserError::SanitizationError(_) => "sanitization-error", - CreateUserError::OwnershipError(_) => "ownership-error", - CreateUserError::UserAlreadyExists => "user-already-exists", - CreateUserError::MySqlError(_) => "mysql-error", + CreateUserError::SanitizationError(err) => format!("sanitization-error/{}", err.error_type()), + CreateUserError::OwnershipError(err) => format!("ownership-error/{}", err.error_type()), + CreateUserError::UserAlreadyExists => "user-already-exists".to_string(), + CreateUserError::MySqlError(_) => "mysql-error".to_string(), } } } diff --git a/src/core/protocol/commands/drop_databases.rs b/src/core/protocol/commands/drop_databases.rs index 40e323c..5f2bee4 100644 --- a/src/core/protocol/commands/drop_databases.rs +++ b/src/core/protocol/commands/drop_databases.rs @@ -78,12 +78,12 @@ impl DropDatabaseError { } } - pub fn error_type(&self) -> &'static str { + pub fn error_type(&self) -> String { match self { - DropDatabaseError::SanitizationError(_) => "sanitization-error", - DropDatabaseError::OwnershipError(_) => "ownership-error", - DropDatabaseError::DatabaseDoesNotExist => "database-does-not-exist", - DropDatabaseError::MySqlError(_) => "mysql-error", + DropDatabaseError::SanitizationError(err) => format!("sanitization-error/{}", err.error_type()), + DropDatabaseError::OwnershipError(err) => format!("ownership-error/{}", err.error_type()), + DropDatabaseError::DatabaseDoesNotExist => "database-does-not-exist".to_string(), + DropDatabaseError::MySqlError(_) => "mysql-error".to_string(), } } } diff --git a/src/core/protocol/commands/drop_users.rs b/src/core/protocol/commands/drop_users.rs index ce7cc69..1c84f25 100644 --- a/src/core/protocol/commands/drop_users.rs +++ b/src/core/protocol/commands/drop_users.rs @@ -75,12 +75,12 @@ impl DropUserError { } } - pub fn error_type(&self) -> &'static str { + pub fn error_type(&self) -> String { match self { - DropUserError::SanitizationError(_) => "sanitization-error", - DropUserError::OwnershipError(_) => "ownership-error", - DropUserError::UserDoesNotExist => "user-does-not-exist", - DropUserError::MySqlError(_) => "mysql-error", + DropUserError::SanitizationError(err) => format!("sanitization-error/{}", err.error_type()), + DropUserError::OwnershipError(err) => format!("ownership-error/{}", err.error_type()), + DropUserError::UserDoesNotExist => "user-does-not-exist".to_string(), + DropUserError::MySqlError(_) => "mysql-error".to_string(), } } } diff --git a/src/core/protocol/commands/list_all_databases.rs b/src/core/protocol/commands/list_all_databases.rs index 89108f0..e146bb9 100644 --- a/src/core/protocol/commands/list_all_databases.rs +++ b/src/core/protocol/commands/list_all_databases.rs @@ -15,4 +15,11 @@ impl ListAllDatabasesError { ListAllDatabasesError::MySqlError(err) => format!("MySQL error: {}", err), } } + + #[allow(dead_code)] + pub fn error_type(&self) -> String { + match self { + ListAllDatabasesError::MySqlError(_) => "mysql-error".to_string(), + } + } } diff --git a/src/core/protocol/commands/list_all_privileges.rs b/src/core/protocol/commands/list_all_privileges.rs index ef30456..86f094a 100644 --- a/src/core/protocol/commands/list_all_privileges.rs +++ b/src/core/protocol/commands/list_all_privileges.rs @@ -16,4 +16,11 @@ impl GetAllDatabasesPrivilegeDataError { GetAllDatabasesPrivilegeDataError::MySqlError(err) => format!("MySQL error: {}", err), } } + + #[allow(dead_code)] + pub fn error_type(&self) -> String { + match self { + GetAllDatabasesPrivilegeDataError::MySqlError(_) => "mysql-error".to_string(), + } + } } diff --git a/src/core/protocol/commands/list_all_users.rs b/src/core/protocol/commands/list_all_users.rs index 19d303c..53b1523 100644 --- a/src/core/protocol/commands/list_all_users.rs +++ b/src/core/protocol/commands/list_all_users.rs @@ -15,4 +15,11 @@ impl ListAllUsersError { ListAllUsersError::MySqlError(err) => format!("MySQL error: {}", err), } } + + #[allow(dead_code)] + pub fn error_type(&self) -> String { + match self { + ListAllUsersError::MySqlError(_) => "mysql-error".to_string(), + } + } } diff --git a/src/core/protocol/commands/list_databases.rs b/src/core/protocol/commands/list_databases.rs index 3232928..e0b320a 100644 --- a/src/core/protocol/commands/list_databases.rs +++ b/src/core/protocol/commands/list_databases.rs @@ -94,12 +94,12 @@ impl ListDatabasesError { } } - pub fn error_type(&self) -> &'static str { + pub fn error_type(&self) -> String { match self { - ListDatabasesError::SanitizationError(_) => "sanitization-error", - ListDatabasesError::OwnershipError(_) => "ownership-error", - ListDatabasesError::DatabaseDoesNotExist => "database-does-not-exist", - ListDatabasesError::MySqlError(_) => "mysql-error", + ListDatabasesError::SanitizationError(err) => format!("sanitization-error/{}", err.error_type()), + ListDatabasesError::OwnershipError(err) => format!("ownership-error/{}", err.error_type()), + ListDatabasesError::DatabaseDoesNotExist => "database-does-not-exist".to_string(), + ListDatabasesError::MySqlError(_) => "mysql-error".to_string(), } } } diff --git a/src/core/protocol/commands/list_privileges.rs b/src/core/protocol/commands/list_privileges.rs index 24dd37e..b005863 100644 --- a/src/core/protocol/commands/list_privileges.rs +++ b/src/core/protocol/commands/list_privileges.rs @@ -128,12 +128,12 @@ impl GetDatabasesPrivilegeDataError { } } - pub fn error_type(&self) -> &'static str { + pub fn error_type(&self) -> String { match self { - GetDatabasesPrivilegeDataError::SanitizationError(_) => "sanitization-error", - GetDatabasesPrivilegeDataError::OwnershipError(_) => "ownership-error", - GetDatabasesPrivilegeDataError::DatabaseDoesNotExist => "database-does-not-exist", - GetDatabasesPrivilegeDataError::MySqlError(_) => "mysql-error", + GetDatabasesPrivilegeDataError::SanitizationError(err) => format!("sanitization-error/{}", err.error_type()), + GetDatabasesPrivilegeDataError::OwnershipError(err) => format!("ownership-error/{}", err.error_type()), + GetDatabasesPrivilegeDataError::DatabaseDoesNotExist => "database-does-not-exist".to_string(), + GetDatabasesPrivilegeDataError::MySqlError(_) => "mysql-error".to_string(), } } } diff --git a/src/core/protocol/commands/list_users.rs b/src/core/protocol/commands/list_users.rs index 2c2f1ee..8a09929 100644 --- a/src/core/protocol/commands/list_users.rs +++ b/src/core/protocol/commands/list_users.rs @@ -109,12 +109,12 @@ impl ListUsersError { } } - pub fn error_type(&self) -> &'static str { + pub fn error_type(&self) -> String { match self { - ListUsersError::SanitizationError(_) => "sanitization-error", - ListUsersError::OwnershipError(_) => "ownership-error", - ListUsersError::UserDoesNotExist => "user-does-not-exist", - ListUsersError::MySqlError(_) => "mysql-error", + ListUsersError::SanitizationError(err) => format!("sanitization-error/{}", err.error_type()), + ListUsersError::OwnershipError(err) => format!("ownership-error/{}", err.error_type()), + ListUsersError::UserDoesNotExist => "user-does-not-exist".to_string(), + ListUsersError::MySqlError(_) => "mysql-error".to_string(), } } } diff --git a/src/core/protocol/commands/lock_users.rs b/src/core/protocol/commands/lock_users.rs index 28af361..2a8f890 100644 --- a/src/core/protocol/commands/lock_users.rs +++ b/src/core/protocol/commands/lock_users.rs @@ -79,13 +79,13 @@ impl LockUserError { } } - pub fn error_type(&self) -> &'static str { + pub fn error_type(&self) -> String { match self { - LockUserError::SanitizationError(_) => "sanitization-error", - LockUserError::OwnershipError(_) => "ownership-error", - LockUserError::UserDoesNotExist => "user-does-not-exist", - LockUserError::UserIsAlreadyLocked => "user-is-already-locked", - LockUserError::MySqlError(_) => "mysql-error", + LockUserError::SanitizationError(err) => format!("sanitization-error/{}", err.error_type()), + LockUserError::OwnershipError(err) => format!("ownership-error/{}", err.error_type()), + LockUserError::UserDoesNotExist => "user-does-not-exist".to_string(), + LockUserError::UserIsAlreadyLocked => "user-is-already-locked".to_string(), + LockUserError::MySqlError(_) => "mysql-error".to_string(), } } } diff --git a/src/core/protocol/commands/modify_privileges.rs b/src/core/protocol/commands/modify_privileges.rs index 80bf3f5..599d232 100644 --- a/src/core/protocol/commands/modify_privileges.rs +++ b/src/core/protocol/commands/modify_privileges.rs @@ -81,27 +81,23 @@ impl ModifyDatabasePrivilegesError { } #[allow(dead_code)] - pub fn error_type(&self) -> &'static str { + pub fn error_type(&self) -> String { match self { - ModifyDatabasePrivilegesError::DatabaseSanitizationError(_) => { - "database-sanitization-error" + ModifyDatabasePrivilegesError::DatabaseSanitizationError(err) => { + format!("database-sanitization-error/{}", err.error_type()) } - ModifyDatabasePrivilegesError::DatabaseOwnershipError(_) => "database-ownership-error", - ModifyDatabasePrivilegesError::UserSanitizationError(_) => "user-sanitization-error", - ModifyDatabasePrivilegesError::UserOwnershipError(_) => "user-ownership-error", - ModifyDatabasePrivilegesError::DatabaseDoesNotExist => "database-does-not-exist", - ModifyDatabasePrivilegesError::DiffDoesNotApply(err) => match err { - DiffDoesNotApplyError::RowAlreadyExists(_, _) => { - "diff-does-not-apply/row-already-exists" - } - DiffDoesNotApplyError::RowDoesNotExist(_, _) => { - "diff-does-not-apply/row-does-not-exist" - } - DiffDoesNotApplyError::RowPrivilegeChangeDoesNotApply(_, _) => { - "diff-does-not-apply/row-privilege-change-does-not-apply" - } - }, - ModifyDatabasePrivilegesError::MySqlError(_) => "mysql-error", + ModifyDatabasePrivilegesError::DatabaseOwnershipError(err) => { + format!("database-ownership-error/{}", err.error_type()) + } + ModifyDatabasePrivilegesError::UserSanitizationError(err) => { + format!("user-sanitization-error/{}", err.error_type()) + } + ModifyDatabasePrivilegesError::UserOwnershipError(err) => { + format!("user-ownership-error/{}", err.error_type()) + } + ModifyDatabasePrivilegesError::DatabaseDoesNotExist => "database-does-not-exist".to_string(), + ModifyDatabasePrivilegesError::DiffDoesNotApply(err) => format!("diff-does-not-apply/{}", err.error_type()), + ModifyDatabasePrivilegesError::MySqlError(_) => "mysql-error".to_string(), } } } @@ -129,4 +125,14 @@ impl DiffDoesNotApplyError { } } } + + pub fn error_type(&self) -> String { + match self { + DiffDoesNotApplyError::RowAlreadyExists(_, _) => "row-already-exists".to_string(), + DiffDoesNotApplyError::RowDoesNotExist(_, _) => "row-does-not-exist".to_string(), + DiffDoesNotApplyError::RowPrivilegeChangeDoesNotApply(_, _) => { + "row-privilege-change-does-not-apply".to_string() + } + } + } } diff --git a/src/core/protocol/commands/passwd_user.rs b/src/core/protocol/commands/passwd_user.rs index 1af1d1f..965495b 100644 --- a/src/core/protocol/commands/passwd_user.rs +++ b/src/core/protocol/commands/passwd_user.rs @@ -48,12 +48,12 @@ impl SetPasswordError { } #[allow(dead_code)] - pub fn error_type(&self) -> &'static str { + pub fn error_type(&self) -> String { match self { - SetPasswordError::SanitizationError(_) => "sanitization-error", - SetPasswordError::OwnershipError(_) => "ownership-error", - SetPasswordError::UserDoesNotExist => "user-does-not-exist", - SetPasswordError::MySqlError(_) => "mysql-error", + SetPasswordError::SanitizationError(err) => format!("sanitization-error/{}", err.error_type()), + SetPasswordError::OwnershipError(err) => format!("ownership-error/{}", err.error_type()), + SetPasswordError::UserDoesNotExist => "user-does-not-exist".to_string(), + SetPasswordError::MySqlError(_) => "mysql-error".to_string(), } } } diff --git a/src/core/protocol/commands/unlock_users.rs b/src/core/protocol/commands/unlock_users.rs index ac2fe63..6445baa 100644 --- a/src/core/protocol/commands/unlock_users.rs +++ b/src/core/protocol/commands/unlock_users.rs @@ -79,13 +79,13 @@ impl UnlockUserError { } } - pub fn error_type(&self) -> &'static str { + pub fn error_type(&self) -> String { match self { - UnlockUserError::SanitizationError(_) => "sanitization-error", - UnlockUserError::OwnershipError(_) => "ownership-error", - UnlockUserError::UserDoesNotExist => "user-does-not-exist", - UnlockUserError::UserIsAlreadyUnlocked => "user-is-already-unlocked", - UnlockUserError::MySqlError(_) => "mysql-error", + UnlockUserError::SanitizationError(err) => format!("sanitization-error/{}", err.error_type()), + UnlockUserError::OwnershipError(err) => format!("ownership-error/{}", err.error_type()), + UnlockUserError::UserDoesNotExist => "user-does-not-exist".to_string(), + UnlockUserError::UserIsAlreadyUnlocked => "user-is-already-unlocked".to_string(), + UnlockUserError::MySqlError(_) => "mysql-error".to_string(), } } } diff --git a/src/core/protocol/request_validation.rs b/src/core/protocol/request_validation.rs index 2d834f3..5e9d323 100644 --- a/src/core/protocol/request_validation.rs +++ b/src/core/protocol/request_validation.rs @@ -34,6 +34,14 @@ impl NameValidationError { .to_owned(), } } + + pub fn error_type(&self) -> &'static str { + match self { + NameValidationError::EmptyString => "empty-string", + NameValidationError::InvalidCharacters => "invalid-characters", + NameValidationError::TooLong => "too-long", + } + } } impl OwnerValidationError { @@ -81,6 +89,13 @@ impl OwnerValidationError { .to_string(), } } + + pub fn error_type(&self) -> &'static str { + match self { + OwnerValidationError::NoMatch => "no-match", + OwnerValidationError::StringEmpty => "string-empty", + } + } } #[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize, Deserialize)]