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
@@ -35,8 +35,7 @@ use std::collections::HashMap;
use std::io::Write;
use std::path::Path;
use anyhow::{Context, Result};
use crate::domain::error::RepositoryError;
use crate::domain::memory::Memory;
use crate::domain::repository::MemoryRepository;
@@ -173,7 +172,7 @@ impl MemoryRepository for MarkdownMemoryRepository {
///
/// Flow: read directory entries → filter `*.md` → strip extension → exclude MEMORY.md.
/// Returns empty Vec if the directory doesn't exist.
fn list(&self, memory_dir: &Path) -> Result<Vec<String>> {
fn list(&self, memory_dir: &Path) -> Result<Vec<String>, RepositoryError> {
let Ok(entries) = std::fs::read_dir(memory_dir) else {
return Ok(Vec::new());
};
@@ -196,21 +195,19 @@ impl MemoryRepository for MarkdownMemoryRepository {
/// Load a single `Memory` by name from `memory_dir`.
///
/// Flow: resolve file path → read file → parse frontmatter + body → return Memory.
fn load(&self, memory_dir: &Path, name: &str) -> Result<Memory> {
fn load(&self, memory_dir: &Path, name: &str) -> Result<Memory, RepositoryError> {
tracing::debug!("loading memory '{name}'");
let path = Memory::path(memory_dir, name);
let content = std::fs::read_to_string(&path)
.with_context(|| format!("failed to read memory '{name}' at '{}'", path.display()))?;
let content = std::fs::read_to_string(&path)?;
let memory = Self::parse(&content)
.map_err(|e| anyhow::anyhow!("failed to parse memory '{name}': {e}"))?;
.map_err(|e| RepositoryError::Other(format!("failed to parse memory '{name}': {e}")))?;
Ok(memory)
}
fn save(&self, memory_dir: &Path, memory: &Memory) -> Result<()> {
fn save(&self, memory_dir: &Path, memory: &Memory) -> Result<(), RepositoryError> {
let path = Memory::path(memory_dir, &memory.name);
let parent = path.parent().unwrap();
std::fs::create_dir_all(parent)
.with_context(|| format!("failed to create memory dir '{}'", parent.display()))?;
std::fs::create_dir_all(parent)?;
let frontmatter = Self::build_frontmatter(memory);
let content = format!("---\n{frontmatter}---\n\n{}", memory.content);
@@ -221,18 +218,11 @@ impl MemoryRepository for MarkdownMemoryRepository {
.create(true)
.truncate(true)
.write(true)
.open(&tmp)
.with_context(|| format!("failed to write temp file '{}'", tmp.display()))?;
.open(&tmp)?;
f.write_all(content.as_bytes())?;
f.sync_all()?;
}
std::fs::rename(&tmp, &path).with_context(|| {
format!(
"failed to rename '{}' -> '{}'",
tmp.display(),
path.display()
)
})?;
std::fs::rename(&tmp, &path)?;
if let Some(p) = path.parent() {
if let Ok(d) = std::fs::File::open(p) {
let _ = d.sync_all();
@@ -242,12 +232,10 @@ impl MemoryRepository for MarkdownMemoryRepository {
Ok(())
}
fn delete(&self, memory_dir: &Path, name: &str) -> Result<()> {
fn delete(&self, memory_dir: &Path, name: &str) -> Result<(), RepositoryError> {
let path = Memory::path(memory_dir, name);
if path.exists() {
std::fs::remove_file(&path).with_context(|| {
format!("failed to delete memory '{name}' at '{}'", path.display())
})?;
std::fs::remove_file(&path)?;
tracing::debug!("memory deleted: '{}'", path.display());
} else {
tracing::warn!(