fix(error): migrate session submodules + btmsg/bttask backends to AppError

- session/*.rs (sessions, layout, settings, ssh, agents, metrics, anchors)
  now return Result<T, AppError> internally, not just at command boundary
- btmsg.rs and bttask.rs backends migrated to AppError::Database
- 116 cargo tests passing
This commit is contained in:
Hibryda 2026-03-18 01:32:07 +01:00
parent eb04e7e5b5
commit f19b69f018
11 changed files with 264 additions and 255 deletions

View file

@ -3,6 +3,7 @@
use rusqlite::params;
use serde::{Deserialize, Serialize};
use super::SessionDb;
use crate::error::AppError;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SessionMetric {
@ -22,7 +23,7 @@ pub struct SessionMetric {
}
impl SessionDb {
pub fn save_session_metric(&self, metric: &SessionMetric) -> Result<(), String> {
pub fn save_session_metric(&self, metric: &SessionMetric) -> Result<(), AppError> {
let conn = self.conn.lock().unwrap();
conn.execute(
"INSERT INTO session_metrics (project_id, session_id, start_time, end_time, peak_tokens, turn_count, tool_call_count, cost_usd, model, status, error_message) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11)",
@ -39,22 +40,22 @@ impl SessionDb {
metric.status,
metric.error_message,
],
).map_err(|e| format!("Save session metric failed: {e}"))?;
).map_err(|e| AppError::database(format!("Save session metric failed: {e}")))?;
// Enforce retention: keep last 100 per project
conn.execute(
"DELETE FROM session_metrics WHERE project_id = ?1 AND id NOT IN (SELECT id FROM session_metrics WHERE project_id = ?1 ORDER BY end_time DESC LIMIT 100)",
params![metric.project_id],
).map_err(|e| format!("Prune session metrics failed: {e}"))?;
).map_err(|e| AppError::database(format!("Prune session metrics failed: {e}")))?;
Ok(())
}
pub fn load_session_metrics(&self, project_id: &str, limit: i64) -> Result<Vec<SessionMetric>, String> {
pub fn load_session_metrics(&self, project_id: &str, limit: i64) -> Result<Vec<SessionMetric>, AppError> {
let conn = self.conn.lock().unwrap();
let mut stmt = conn.prepare(
"SELECT id, project_id, session_id, start_time, end_time, peak_tokens, turn_count, tool_call_count, cost_usd, model, status, error_message FROM session_metrics WHERE project_id = ?1 ORDER BY end_time DESC LIMIT ?2"
).map_err(|e| format!("Query prepare failed: {e}"))?;
).map_err(|e| AppError::database(format!("Query prepare failed: {e}")))?;
let metrics = stmt.query_map(params![project_id, limit], |row| {
Ok(SessionMetric {
@ -71,9 +72,9 @@ impl SessionDb {
status: row.get(10)?,
error_message: row.get(11)?,
})
}).map_err(|e| format!("Query failed: {e}"))?
}).map_err(|e| AppError::database(format!("Query failed: {e}")))?
.collect::<Result<Vec<_>, _>>()
.map_err(|e| format!("Row read failed: {e}"))?;
.map_err(|e| AppError::database(format!("Row read failed: {e}")))?;
Ok(metrics)
}