Refactor error handling in IAM and CMS crates

- Introduced `RepositoryError` and `ServiceError` enums in both IAM and CMS domains for better error management.
- Updated domain traits and services to return specific error types instead of `anyhow::Result`.
- Enhanced session and OAuth repository implementations to handle errors more explicitly.
- Refactored session service methods to return `Result<T, ServiceError>` for improved error handling.
- Updated HTTP handlers to utilize the new error types.
- Modified password hashing functions to run in a blocking context using `tokio::task::spawn_blocking`.
- Added tests for new error handling mechanisms and async password functions.
This commit is contained in:
asepharyana
2026-07-20 06:14:23 +07:00
parent 5aaedbf787
commit ab1a54b72e
47 changed files with 630 additions and 347 deletions
@@ -18,7 +18,7 @@
//! response and setting HTTP status codes.
use anyhow::{Context, Result};
use tracing;
use tracing::instrument;
use crate::domain::memory::Memory;
use crate::domain::service::{MemoryService, SettingsService};
@@ -31,6 +31,7 @@ use super::dto::{MemoryCreateRequest, MemoryResponse, SettingsResponse, Settings
/// Returns the current settings as a `SettingsResponse`.
///
/// Flow: load settings from service → convert to DTO → return.
#[instrument(skip(service))]
pub fn handle_get_settings<S: SettingsService>(service: &S) -> Result<SettingsResponse> {
tracing::debug!("handling GET /settings");
let settings = service.load_settings().context("failed to load settings")?;
@@ -46,6 +47,7 @@ pub fn handle_get_settings<S: SettingsService>(service: &S) -> Result<SettingsRe
///
/// ## Validation
/// - `internet_mode` must be "Off", "ReadOnly", or "Full" (case-sensitive).
#[instrument(skip(service))]
pub fn handle_update_settings<S: SettingsService>(
service: &S,
req: SettingsUpdateRequest,
@@ -130,6 +132,7 @@ pub fn handle_update_settings<S: SettingsService>(
/// use a dedicated endpoint.
///
/// Flow: list slugs from service → map each to minimal MemoryResponse → return.
#[instrument(skip(service))]
pub fn handle_list_memories<M: MemoryService>(service: &M) -> Result<Vec<MemoryResponse>> {
tracing::debug!("handling GET /memories");
let slugs = service.list_memories().context("failed to list memories")?;
@@ -166,6 +169,7 @@ pub fn handle_list_memories<M: MemoryService>(service: &M) -> Result<Vec<MemoryR
/// ## Defaults
/// - `kind` defaults to "reference" if not specified
/// - `lifecycle` defaults to "new" if not specified
#[instrument(skip(service), fields(name = %req.name))]
pub fn handle_create_memory<M: MemoryService>(
service: &M,
req: MemoryCreateRequest,