Fix: vm creating error

catching Insufficient fund error from transaction
returning approprate error variant
This commit is contained in:
Noor 2025-05-20 20:20:02 +05:30
parent 6a85acda9e
commit 198f43f472
Signed by: noormohammedb
GPG Key ID: D83EFB8B3B967146
3 changed files with 23 additions and 3 deletions

@ -30,6 +30,8 @@ pub enum Error {
AppDaemonConnection(#[from] tokio::sync::mpsc::error::SendError<AppDaemonMsg>),
#[error("AppDaemon Error {0}")]
NewAppDaemonResp(String),
#[error("Insufficient funds, deposit more tokens")]
InsufficientFunds,
}
pub mod prelude {

@ -239,7 +239,18 @@ impl NewVmReq {
self.price_per_unit
);
//let _: Vec<Self> = db.insert(NEW_VM_REQ).relation(self).await?;
db.query(query).await?;
let mut query_resp = db.query(query).await?;
let resp_err = query_resp.take_errors();
if let Some(insufficient_funds_error) = resp_err.get(&1) {
if let surrealdb::Error::Api(surrealdb::error::Api::Query(tx_query_error)) =
insufficient_funds_error
{
log::error!("Transaction error: {}", tx_query_error);
return Err(Error::InsufficientFunds);
}
}
Ok(())
}
}
@ -276,6 +287,7 @@ impl WrappedMeasurement {
if let Some(args) = args {
return Ok(Self::Args(vm_id.to_string(), args));
}
log::trace!("listening for table: {table}");
tokio::time::timeout(Duration::from_secs(10), async {
loop {

@ -20,11 +20,17 @@ async fn test_vm_creation() {
let key = Key::new();
let _ = create_new_vm(&db, &key, &daemon_key, &brain_channel).await;
let new_vm_resp = create_new_vm(&db, &key, &daemon_key, &brain_channel).await;
assert!(new_vm_resp.is_err());
let grpc_error_message = new_vm_resp.err().unwrap().to_string();
assert!(grpc_error_message.contains("Insufficient funds"));
// TODO: Airdrop the user and try creating the VM again
}
#[tokio::test]
async fn test_vm_creation_timeout() {
async fn test_timeout_vm_creation() {
prepare_test_db().await.unwrap();
// env_logger::builder().filter_level(log::LevelFilter::Error).init();