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:
parent
eb04e7e5b5
commit
f19b69f018
11 changed files with 264 additions and 255 deletions
|
|
@ -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 Session {
|
||||
|
|
@ -20,11 +21,11 @@ pub struct Session {
|
|||
}
|
||||
|
||||
impl SessionDb {
|
||||
pub fn list_sessions(&self) -> Result<Vec<Session>, String> {
|
||||
pub fn list_sessions(&self) -> Result<Vec<Session>, AppError> {
|
||||
let conn = self.conn.lock().unwrap();
|
||||
let mut stmt = conn
|
||||
.prepare("SELECT id, type, title, shell, cwd, args, group_name, created_at, last_used_at FROM sessions ORDER BY last_used_at DESC")
|
||||
.map_err(|e| format!("Query prepare failed: {e}"))?;
|
||||
.map_err(|e| AppError::database(format!("Query prepare failed: {e}")))?;
|
||||
|
||||
let sessions = stmt
|
||||
.query_map([], |row| {
|
||||
|
|
@ -42,14 +43,14 @@ impl SessionDb {
|
|||
last_used_at: row.get(8)?,
|
||||
})
|
||||
})
|
||||
.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(sessions)
|
||||
}
|
||||
|
||||
pub fn save_session(&self, session: &Session) -> Result<(), String> {
|
||||
pub fn save_session(&self, session: &Session) -> Result<(), AppError> {
|
||||
let conn = self.conn.lock().unwrap();
|
||||
let args_json = session.args.as_ref().map(|a| serde_json::to_string(a).unwrap_or_default());
|
||||
conn.execute(
|
||||
|
|
@ -65,36 +66,36 @@ impl SessionDb {
|
|||
session.created_at,
|
||||
session.last_used_at,
|
||||
],
|
||||
).map_err(|e| format!("Insert failed: {e}"))?;
|
||||
).map_err(|e| AppError::database(format!("Insert failed: {e}")))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn delete_session(&self, id: &str) -> Result<(), String> {
|
||||
pub fn delete_session(&self, id: &str) -> Result<(), AppError> {
|
||||
let conn = self.conn.lock().unwrap();
|
||||
conn.execute("DELETE FROM sessions WHERE id = ?1", params![id])
|
||||
.map_err(|e| format!("Delete failed: {e}"))?;
|
||||
.map_err(|e| AppError::database(format!("Delete failed: {e}")))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn update_title(&self, id: &str, title: &str) -> Result<(), String> {
|
||||
pub fn update_title(&self, id: &str, title: &str) -> Result<(), AppError> {
|
||||
let conn = self.conn.lock().unwrap();
|
||||
conn.execute(
|
||||
"UPDATE sessions SET title = ?1 WHERE id = ?2",
|
||||
params![title, id],
|
||||
).map_err(|e| format!("Update failed: {e}"))?;
|
||||
).map_err(|e| AppError::database(format!("Update failed: {e}")))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn update_group(&self, id: &str, group_name: &str) -> Result<(), String> {
|
||||
pub fn update_group(&self, id: &str, group_name: &str) -> Result<(), AppError> {
|
||||
let conn = self.conn.lock().unwrap();
|
||||
conn.execute(
|
||||
"UPDATE sessions SET group_name = ?1 WHERE id = ?2",
|
||||
params![group_name, id],
|
||||
).map_err(|e| format!("Update group failed: {e}"))?;
|
||||
).map_err(|e| AppError::database(format!("Update group failed: {e}")))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn touch_session(&self, id: &str) -> Result<(), String> {
|
||||
pub fn touch_session(&self, id: &str) -> Result<(), AppError> {
|
||||
let conn = self.conn.lock().unwrap();
|
||||
let now = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
|
|
@ -103,7 +104,7 @@ impl SessionDb {
|
|||
conn.execute(
|
||||
"UPDATE sessions SET last_used_at = ?1 WHERE id = ?2",
|
||||
params![now, id],
|
||||
).map_err(|e| format!("Touch failed: {e}"))?;
|
||||
).map_err(|e| AppError::database(format!("Touch failed: {e}")))?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue