mirror of
https://github.com/storytold/artcraft.git
synced 2026-11-04 17:38:10 +00:00
handle errors
This commit is contained in:
1 parent
f8e400019d
commit
78f692e2de
4 files changed
+18
-16
No files matched your search
+15
-13
@@ -1,3 +1,4 @@
|
||||
use crate::http_server::common_responses::common_web_error::CommonWebError;
|
||||
use log::{error, info};
|
||||
use mysql_queries::queries::wallets::refund::try_to_refund_ledger_entry::{try_to_refund_ledger_entry, WalletRefundOutcome};
|
||||
use sqlx::Acquire;
|
||||
@@ -6,17 +7,14 @@ use tokens::tokens::wallet_ledger_entries::WalletLedgerEntryToken;
|
||||
pub async fn refund_wallet_after_api_failure(
|
||||
ledger_entry_token: &WalletLedgerEntryToken,
|
||||
connection: &mut sqlx::pool::PoolConnection<sqlx::MySql>,
|
||||
) {
|
||||
let mut transaction = match connection.begin().await {
|
||||
Ok(tx) => tx,
|
||||
Err(err) => {
|
||||
error!(
|
||||
"Failed to begin refund transaction after API failure (ledger {}): {:?}",
|
||||
ledger_entry_token.as_str(), err
|
||||
);
|
||||
return;
|
||||
}
|
||||
};
|
||||
) -> Result<(), CommonWebError> {
|
||||
let mut transaction = connection.begin().await.map_err(|err| {
|
||||
error!(
|
||||
"Failed to begin refund transaction after API failure (ledger {}): {:?}",
|
||||
ledger_entry_token.as_str(), err
|
||||
);
|
||||
CommonWebError::ServerError
|
||||
})?;
|
||||
|
||||
match try_to_refund_ledger_entry(ledger_entry_token, &mut transaction).await {
|
||||
Ok(WalletRefundOutcome::Refunded(summary)) => {
|
||||
@@ -26,12 +24,13 @@ pub async fn refund_wallet_after_api_failure(
|
||||
ledger_entry_token.as_str(),
|
||||
summary.refund_ledger_entry_token.as_str(),
|
||||
);
|
||||
if let Err(err) = transaction.commit().await {
|
||||
transaction.commit().await.map_err(|err| {
|
||||
error!(
|
||||
"Failed to commit refund after API failure (ledger {}): {:?}",
|
||||
ledger_entry_token.as_str(), err
|
||||
);
|
||||
}
|
||||
CommonWebError::ServerError
|
||||
})?;
|
||||
}
|
||||
Ok(WalletRefundOutcome::AlreadyRefunded) => {
|
||||
info!(
|
||||
@@ -46,6 +45,9 @@ pub async fn refund_wallet_after_api_failure(
|
||||
ledger_entry_token.as_str(), err
|
||||
);
|
||||
let _ = transaction.rollback().await;
|
||||
return Err(CommonWebError::ServerError);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
+1
-1
@@ -155,7 +155,7 @@ pub async fn generate_worldlabs_marble_0p1_mini_splat_handler(
|
||||
Ok(result) => result,
|
||||
Err(err) => {
|
||||
warn!("World Labs generate_world error: {:?}", err);
|
||||
refund_wallet_after_api_failure(&wallet_deduction.ledger_entry_token, &mut mysql_connection).await;
|
||||
refund_wallet_after_api_failure(&wallet_deduction.ledger_entry_token, &mut mysql_connection).await?;
|
||||
return Err(CommonWebError::ServerError);
|
||||
}
|
||||
};
|
||||
|
||||
+1
-1
@@ -155,7 +155,7 @@ pub async fn generate_worldlabs_marble_0p1_plus_splat_handler(
|
||||
Ok(result) => result,
|
||||
Err(err) => {
|
||||
warn!("World Labs generate_world error: {:?}", err);
|
||||
refund_wallet_after_api_failure(&wallet_deduction.ledger_entry_token, &mut mysql_connection).await;
|
||||
refund_wallet_after_api_failure(&wallet_deduction.ledger_entry_token, &mut mysql_connection).await?;
|
||||
return Err(CommonWebError::ServerError);
|
||||
}
|
||||
};
|
||||
|
||||
+1
-1
@@ -221,7 +221,7 @@ pub async fn seedance_2p0_multi_function_video_gen_handler(
|
||||
Ok(resp) => resp,
|
||||
Err(err) => {
|
||||
warn!("Error calling seedance2pro generate_video: {:?}", err);
|
||||
refund_wallet_after_api_failure(&deduction_result.ledger_entry_token, &mut mysql_connection).await;
|
||||
refund_wallet_after_api_failure(&deduction_result.ledger_entry_token, &mut mysql_connection).await?;
|
||||
|
||||
// TODO: We need to start returning robust failure messages to the caller.
|
||||
return Err(CommonWebError::ServerError);
|
||||
|
||||
Reference in new issue
Block a user