update errors handle

This commit is contained in:
yggverse 2024-11-22 15:51:03 +02:00
parent 48ad344adc
commit efa3bc48c3
9 changed files with 61 additions and 74 deletions

View file

@ -22,35 +22,36 @@ impl Database {
// Getters
/// Get all records
pub fn records(&self) -> Vec<Table> {
pub fn records(&self) -> Result<Vec<Table>, Error> {
let readable = self.connection.read().unwrap();
let tx = readable.unchecked_transaction().unwrap();
select(&tx).unwrap()
let tx = readable.unchecked_transaction()?;
select(&tx)
}
/// Get active identity record if exist
pub fn active(&self) -> Option<Table> {
self.records().into_iter().find(|record| record.is_active)
pub fn active(&self) -> Result<Option<Table>, Error> {
let records = self.records()?;
Ok(records.into_iter().find(|record| record.is_active))
}
// Setters
/// Create new record in `Self` database connected
pub fn add(&self, profile_id: Rc<i64>, is_active: bool) -> Result<i64, ()> {
pub fn add(&self, profile_id: Rc<i64>, is_active: bool) -> Result<i64, Error> {
// Begin new transaction
let mut writable = self.connection.write().unwrap();
let tx = writable.transaction().unwrap();
let tx = writable.transaction()?;
// New record has active status
if is_active {
// Deactivate other records as only one profile should be active
for record in select(&tx).unwrap() {
let _ = update(&tx, record.profile_id, record.id, false);
for record in select(&tx)? {
update(&tx, record.profile_id, record.id, false)?;
}
}
// Create new record
insert(&tx, profile_id, is_active).unwrap();
insert(&tx, profile_id, is_active)?;
// Hold insert ID for result
let id = last_insert_id(&tx);
@ -58,7 +59,7 @@ impl Database {
// Done
match tx.commit() {
Ok(_) => Ok(id),
Err(_) => Err(()), // @TODO
Err(reason) => Err(reason),
}
}
}

View file

@ -1,5 +1,6 @@
#[derive(Debug)]
pub enum Error {
Database,
DatabaseActive(sqlite::Error),
DatabaseAdd(sqlite::Error),
GeminiInit(super::gemini::Error),
}

View file

@ -24,7 +24,7 @@ impl Memory {
/// * validate record with same key does not exist yet
pub fn add(&self, id: i64, pem: String) -> Result<(), Error> {
match self.index.borrow_mut().insert(id, pem) {
Some(_) => Err(Error::Overwrite(id)), // @TODO prevent?
Some(key) => Err(Error::Overwrite(key)), // @TODO prevent?
None => Ok(()),
}
}

View file

@ -1,5 +1,5 @@
#[derive(Debug)]
pub enum Error {
NotFound(i64),
Overwrite(i64),
Overwrite(String),
}