Skip to content

Commit 1d2d7e5

Browse files
committed
remove duplication with error messages
1 parent 519ab06 commit 1d2d7e5

File tree

3 files changed

+16
-46
lines changed

3 files changed

+16
-46
lines changed

src/http/error.rs

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -89,33 +89,17 @@ impl From<anyhow::Error> for ApiError {
8989
impl From<crate::secrets::SecretError> for ApiError {
9090
fn from(e: crate::secrets::SecretError) -> Self {
9191
use crate::secrets::SecretError;
92-
match e {
93-
SecretError::NotFound(name) => {
94-
ApiError::not_found(format!("Secret '{}' not found", name))
92+
let constructor = match &e {
93+
SecretError::NotFound(_) => ApiError::not_found,
94+
SecretError::AlreadyExists(_) | SecretError::CreationInProgress(_) => {
95+
ApiError::conflict
9596
}
96-
SecretError::AlreadyExists(name) => {
97-
ApiError::conflict(format!("Secret '{}' already exists", name))
97+
SecretError::NotConfigured => ApiError::service_unavailable,
98+
SecretError::InvalidName(_) => ApiError::bad_request,
99+
SecretError::Backend(_) | SecretError::InvalidUtf8 | SecretError::Database(_) => {
100+
ApiError::internal_error
98101
}
99-
SecretError::CreationInProgress(name) => {
100-
ApiError::conflict(format!(
101-
"Secret '{}' is being created by another process; delete it first if you want to retry",
102-
name
103-
))
104-
}
105-
SecretError::NotConfigured => {
106-
ApiError::service_unavailable("Secret manager not configured")
107-
}
108-
SecretError::InvalidName(name) => ApiError::bad_request(format!(
109-
"Invalid secret name '{}': must be 1-128 characters, alphanumeric with _ and - only",
110-
name
111-
)),
112-
SecretError::Backend(msg) => {
113-
ApiError::internal_error(format!("Backend error: {}", msg))
114-
}
115-
SecretError::InvalidUtf8 => ApiError::internal_error("Invalid UTF-8 in secret"),
116-
SecretError::Database(msg) => {
117-
ApiError::internal_error(format!("Database error: {}", msg))
118-
}
119-
}
102+
};
103+
constructor(e.to_string())
120104
}
121105
}

src/secrets/mod.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub use encrypted_catalog_backend::{
88
EncryptedCatalogBackend, PROVIDER_TYPE as ENCRYPTED_PROVIDER_TYPE,
99
};
1010
pub use encryption::{decrypt, encrypt, DecryptError, EncryptError};
11-
pub use validation::{validate_and_normalize_name, ValidationError};
11+
pub use validation::validate_and_normalize_name;
1212

1313
use crate::catalog::CatalogManager;
1414
use chrono::Utc;
@@ -94,14 +94,6 @@ pub enum SecretError {
9494
Database(String),
9595
}
9696

97-
impl From<ValidationError> for SecretError {
98-
fn from(e: ValidationError) -> Self {
99-
match e {
100-
ValidationError::InvalidSecretName(name) => SecretError::InvalidName(name),
101-
}
102-
}
103-
}
104-
10597
impl From<BackendError> for SecretError {
10698
fn from(e: BackendError) -> Self {
10799
match e {

src/secrets/validation.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,22 @@
1+
use super::SecretError;
2+
13
/// Validates a secret name and returns the normalized (lowercase) form.
24
/// Returns an error if the name doesn't match the allowed pattern.
35
///
46
/// Valid names: 1-128 characters, alphanumeric with _ and - only.
5-
pub fn validate_and_normalize_name(name: &str) -> Result<String, ValidationError> {
7+
pub fn validate_and_normalize_name(name: &str) -> Result<String, SecretError> {
68
if name.is_empty() || name.len() > 128 {
7-
return Err(ValidationError::InvalidSecretName(name.to_string()));
9+
return Err(SecretError::InvalidName(name.to_string()));
810
}
911
if !name
1012
.chars()
1113
.all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-')
1214
{
13-
return Err(ValidationError::InvalidSecretName(name.to_string()));
15+
return Err(SecretError::InvalidName(name.to_string()));
1416
}
1517
Ok(name.to_lowercase())
1618
}
1719

18-
#[derive(Debug, thiserror::Error)]
19-
pub enum ValidationError {
20-
#[error(
21-
"Invalid secret name '{0:.128}': must be 1-128 characters, alphanumeric with _ and - only"
22-
)]
23-
InvalidSecretName(String),
24-
}
25-
2620
#[cfg(test)]
2721
mod tests {
2822
use super::*;

0 commit comments

Comments
 (0)