mirror of
https://github.com/YGGverse/Yoda.git
synced 2025-01-15 17:20:08 +00:00
update errors handle
This commit is contained in:
parent
48ad344adc
commit
efa3bc48c3
@ -59,25 +59,24 @@ impl Profile {
|
||||
// Init writable connection
|
||||
let mut connection = match connection.write() {
|
||||
Ok(connection) => connection,
|
||||
Err(e) => todo!("{e}"),
|
||||
Err(reason) => todo!("{reason}"),
|
||||
};
|
||||
|
||||
// Init new transaction
|
||||
let transaction = match connection.transaction() {
|
||||
Ok(transaction) => transaction,
|
||||
Err(e) => todo!("{e}"),
|
||||
Err(reason) => todo!("{reason}"),
|
||||
};
|
||||
|
||||
// Begin migration
|
||||
match migrate(&transaction) {
|
||||
Ok(_) => {
|
||||
// Confirm changes
|
||||
match transaction.commit() {
|
||||
Ok(_) => {} // @TODO
|
||||
Err(e) => todo!("{e}"),
|
||||
if let Err(reason) = transaction.commit() {
|
||||
todo!("{reason}")
|
||||
}
|
||||
}
|
||||
Err(e) => todo!("{e}"),
|
||||
Err(reason) => todo!("{reason}"),
|
||||
}
|
||||
} // unlock database
|
||||
|
||||
@ -85,18 +84,27 @@ impl Profile {
|
||||
let database = Rc::new(Database::new(connection.clone()));
|
||||
|
||||
// Get active profile or create new one
|
||||
let profile_id = Rc::new(match database.active() {
|
||||
let profile_id = Rc::new(match database.active().unwrap() {
|
||||
Some(profile) => profile.id,
|
||||
None => match database.add(true, DateTime::now_local().unwrap(), None) {
|
||||
Ok(id) => id,
|
||||
Err(_) => todo!(),
|
||||
Err(reason) => todo!("{:?}", reason),
|
||||
},
|
||||
});
|
||||
|
||||
// Init bookmark component @TODO handle errors
|
||||
let bookmark = Rc::new(Bookmark::new(connection.clone(), profile_id.clone()));
|
||||
|
||||
// Init identity component
|
||||
let identity = Rc::new(match Identity::new(connection, profile_id) {
|
||||
Ok(result) => result,
|
||||
Err(reason) => todo!("{:?}", reason),
|
||||
});
|
||||
|
||||
// Result
|
||||
Self {
|
||||
bookmark: Rc::new(Bookmark::new(connection.clone(), profile_id.clone())),
|
||||
identity: Rc::new(Identity::new(connection, profile_id).unwrap()), // @TODO handle
|
||||
bookmark,
|
||||
identity,
|
||||
database,
|
||||
config_path,
|
||||
}
|
||||
@ -105,8 +113,8 @@ impl Profile {
|
||||
|
||||
pub fn migrate(tx: &Transaction) -> Result<(), String> {
|
||||
// Migrate self components
|
||||
if let Err(e) = database::init(tx) {
|
||||
return Err(e.to_string());
|
||||
if let Err(reason) = database::init(tx) {
|
||||
return Err(reason.to_string());
|
||||
}
|
||||
|
||||
// Delegate migration to children components
|
||||
|
@ -24,7 +24,7 @@ impl Memory {
|
||||
/// * validate record with same key does not exist yet
|
||||
pub fn add(&self, request: String, id: i64) -> Result<(), Error> {
|
||||
match self.index.borrow_mut().insert(request, id) {
|
||||
Some(_) => Err(Error::Overwrite), // @TODO prevent?
|
||||
Some(key) => Err(Error::Overwrite(key)), // @TODO prevent?
|
||||
None => Ok(()),
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
NotFound,
|
||||
Overwrite,
|
||||
Overwrite(i64),
|
||||
}
|
||||
|
@ -24,71 +24,45 @@ 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 profile 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, is_active: bool, time: DateTime, name: Option<String>) -> Result<i64, ()> {
|
||||
pub fn add(&self, is_active: bool, time: DateTime, name: Option<String>) -> 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.id, false, record.time, record.name);
|
||||
for record in select(&tx)? {
|
||||
update(&tx, record.id, false, record.time, record.name)?;
|
||||
}
|
||||
}
|
||||
|
||||
// Create new record
|
||||
insert(&tx, is_active, time, name).unwrap();
|
||||
insert(&tx, is_active, time, name)?;
|
||||
|
||||
// Hold insert ID for result
|
||||
let id = last_insert_id(&tx);
|
||||
|
||||
// Done
|
||||
match tx.commit() {
|
||||
Ok(_) => Ok(id),
|
||||
Err(_) => Err(()), // @TODO
|
||||
}
|
||||
}
|
||||
tx.commit()?;
|
||||
|
||||
/* @TODO not in use
|
||||
/// Set `is_active` status `true` for the record with given profile ID
|
||||
/// * reset other records to `false`
|
||||
pub fn activate(&self, id: i64) -> Result<(), ()> {
|
||||
// Begin new transaction
|
||||
let mut writable = self.connection.write().unwrap();
|
||||
let tx = writable.transaction().unwrap();
|
||||
|
||||
// Deactivate other records as only one profile should be active
|
||||
for record in select(&tx).unwrap() {
|
||||
let _ = update(
|
||||
&tx,
|
||||
record.id,
|
||||
if record.id == id { true } else { false },
|
||||
record.time,
|
||||
record.name,
|
||||
);
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
// Done
|
||||
match tx.commit() {
|
||||
Ok(_) => Ok(()),
|
||||
Err(_) => Err(()),
|
||||
} // @TODO make sure ID exist and was changed
|
||||
} */
|
||||
}
|
||||
|
||||
// Low-level DB API
|
||||
|
@ -25,11 +25,14 @@ impl Identity {
|
||||
|
||||
// Get active identity set for profile or create new one
|
||||
let profile_identity_id = Rc::new(match database.active() {
|
||||
Ok(result) => match result {
|
||||
Some(identity) => identity.id,
|
||||
None => match database.add(profile_id, true) {
|
||||
Ok(id) => id,
|
||||
Err(_) => return Err(Error::Database),
|
||||
Err(reason) => return Err(Error::DatabaseAdd(reason)),
|
||||
},
|
||||
},
|
||||
Err(reason) => return Err(Error::DatabaseActive(reason)),
|
||||
});
|
||||
|
||||
// Init gemini component
|
||||
@ -63,8 +66,8 @@ impl Identity {
|
||||
|
||||
pub fn migrate(tx: &Transaction) -> Result<(), String> {
|
||||
// Migrate self components
|
||||
if let Err(e) = database::init(tx) {
|
||||
return Err(e.to_string());
|
||||
if let Err(reason) = database::init(tx) {
|
||||
return Err(reason.to_string());
|
||||
}
|
||||
|
||||
// Delegate migration to childs
|
||||
|
@ -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),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
Database,
|
||||
DatabaseActive(sqlite::Error),
|
||||
DatabaseAdd(sqlite::Error),
|
||||
GeminiInit(super::gemini::Error),
|
||||
}
|
||||
|
@ -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(()),
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
NotFound(i64),
|
||||
Overwrite(i64),
|
||||
Overwrite(String),
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user